1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : Basics_DirUtils.cxx
21 // Autor : Alexander A. BORODIN
24 #include "Basics_DirUtils.hxx"
30 # include <sys/stat.h>
39 # define _separator_ '\\'
41 # define _separator_ '/'
44 namespace Kernel_Utils
46 std::string GetBaseName( const std::string& file_path )
48 int pos = file_path.rfind( _separator_ );
50 return pos < (int)file_path.size()-1 ? file_path.substr( pos+1 ) : "";
54 std::string GetDirName( const std::string& file_path )
56 int pos = file_path.rfind( _separator_ );
58 return pos < (int)file_path.size()-1 ? file_path.substr(0, pos ) : "";
59 return std::string(".");
62 std::string GetTmpDirByEnv( const std::string& tmp_path_env )
64 char* val = getenv( tmp_path_env.c_str() );
65 std::string dir = val ? val : "";
66 return GetTmpDirByPath( dir );
69 std::string GetTmpDirByPath( const std::string& tmp_path )
71 std::string aTmpDir = tmp_path;
75 char *Tmp_dir = getenv("TEMP");
78 Tmp_dir = getenv("TMP");
80 aTmpDir = std::string("C:\\");
82 aTmpDir = std::string(Tmp_dir);
85 aTmpDir = std::string(Tmp_dir);
87 aTmpDir = std::string("/tmp/");
91 if(aTmpDir[aTmpDir.size()-1] != _separator_)
94 srand((unsigned int)time(NULL));
95 int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
97 sprintf(buffer, "%d", aRND);
98 std::string aSubDir(buffer);
99 if(aSubDir.size() <= 1) aSubDir = std::string("123409876");
101 aTmpDir += aSubDir; //Get RND sub directory
103 std::string aDir = aTmpDir;
106 for(aRND = 0; IsExists(aDir); aRND++) {
107 sprintf(buffer, "%d", aRND);
108 aDir = aTmpDir+buffer; //Build a unique directory name
112 if(aDir[aDir.size()-1] != _separator_) aDir += _separator_;
115 CreateDirectory(aDir.c_str(), NULL);
117 mkdir(aDir.c_str(), 0x1ff);
123 //============================================================================
124 // function : GetTempDir
125 // purpose : Returns a temp directory to store created files like "/tmp/sub_dir/"
126 //============================================================================
127 std::string GetTmpDir()
129 return GetTmpDirByPath( "" );
132 //============================================================================
133 // function : GetTempFileName
134 // purpose : Returns the unique temporary file name without any extension /tmp/something/file for Unix or c:\something\file for WIN32
135 //============================================================================
136 std::string GetTmpFileName()
138 std::string tmpDir = GetTmpDir();
139 std::string aFilePath = "";
140 if(IsExists(tmpDir)) {
141 srand((unsigned int)time(NULL));
142 int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
144 sprintf(buffer, "%d", aRND);
145 std::string aSubDir(buffer);
146 if(aSubDir.size() <= 1) aSubDir = std::string("123409876");
149 for(aRND = 0; IsExists(aFilePath); aRND++) {
150 sprintf(buffer, "%d", aRND);
151 aFilePath = tmpDir+buffer; //Build a unique file name
157 //============================================================================
158 // function : IsExists
159 // purpose : Returns True(False) if the path (not)exists
160 //============================================================================
161 bool IsExists(const std::string& thePath)
164 if ( GetFileAttributes ( thePath.c_str() ) == 0xFFFFFFFF ) {
165 if ( GetLastError () == ERROR_FILE_NOT_FOUND ) {
170 int status = access ( thePath.c_str() , F_OK );
171 if (status != 0) return false;
176 //============================================================================
177 // function : IsWritable
178 // purpose : Returns True(False) if the path is (not) writable
179 //============================================================================
180 bool IsWritable(const std::string& thePath)
183 if ( GetFileAttributes ( thePath.c_str() ) == 0xFFFFFFFF ) {
184 if ( GetLastError () == FILE_ATTRIBUTE_READONLY ) {
189 int status = access(thePath.c_str(),W_OK);
190 if (status != 0) return false;
196 //============================================================================
197 // function : GetDirByPath
198 // purpose : Returns directory by path and converts it to native system format
199 //============================================================================
200 std::string GetDirByPath(const std::string& thePath)
204 std::string path = thePath;
205 std::string::size_type length = path.length();
207 //detect all separators in Unix format
208 for ( unsigned int i = 0; i < length; i++ )
214 //detect all separators in Windows format
215 for ( unsigned int i = 0; i < length; i++ )
217 if( path[i] == '\\' )
222 std::string::size_type pos = path.rfind('|');
223 if ( pos == std::string::npos )
226 //check for disk letter ( C: )
227 if ( path.length() == 2 && path[1] == ':' )
236 //remove right subdirectory or filename from path
237 path = path.substr( 0, pos );
240 length = path.length();
241 for ( unsigned int i = 0; i < length; i++ )
244 path[i] = _separator_;
249 //============================================================================
250 // function : IsEmptyDir
251 // purpose : Returns True(False) if the path (not) empty
252 // Also returns False if the path is not valid
253 //============================================================================
254 bool IsEmptyDir(const std::string& thePath)
256 if ( thePath.empty() || !IsExists(thePath))
262 WIN32_FIND_DATA aFileData;
263 HANDLE hFile = FindFirstFile( thePath.c_str(), &aFileData );
264 if ( hFile == INVALID_HANDLE_VALUE )
271 //close serching. path is not empty
277 if((dp = opendir(thePath.c_str())) == NULL)
279 //Could not open directory
284 result = true; //empty if no file found
285 while ((dirp = readdir(dp)) != NULL && result )
287 std::string file_name(dirp->d_name);
288 result = file_name.empty() || file_name == "." || file_name == ".."; //if any file - break and return false