1 // Copyright (C) 2007-2011 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.
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>
38 # define _separator_ '\\'
40 # define _separator_ '/'
43 namespace Kernel_Utils
45 std::string GetBaseName( const std::string& file_path )
47 int pos = file_path.rfind( _separator_ );
49 return pos < (int)file_path.size()-1 ? file_path.substr( pos+1 ) : "";
53 std::string GetDirName( const std::string& file_path )
55 int pos = file_path.rfind( _separator_ );
57 return pos < (int)file_path.size()-1 ? file_path.substr(0, pos ) : "";
58 return std::string(".");
61 std::string GetTmpDirByEnv( const std::string& tmp_path_env )
63 char* val = getenv( tmp_path_env.c_str() );
64 std::string dir = val ? val : "";
65 return GetTmpDirByPath( dir );
68 std::string GetTmpDirByPath( const std::string& tmp_path )
70 std::string aTmpDir = tmp_path;
74 char *Tmp_dir = getenv("TEMP");
77 Tmp_dir = getenv("TMP");
79 aTmpDir = std::string("C:\\");
81 aTmpDir = std::string(Tmp_dir);
84 aTmpDir = std::string(Tmp_dir);
86 aTmpDir = std::string("/tmp/");
90 if(aTmpDir[aTmpDir.size()-1] != _separator_)
93 srand((unsigned int)time(NULL));
94 int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
96 sprintf(buffer, "%d", aRND);
97 std::string aSubDir(buffer);
98 if(aSubDir.size() <= 1) aSubDir = std::string("123409876");
100 aTmpDir += aSubDir; //Get RND sub directory
102 std::string aDir = aTmpDir;
105 for(aRND = 0; IsExists(aDir); aRND++) {
106 sprintf(buffer, "%d", aRND);
107 aDir = aTmpDir+buffer; //Build a unique directory name
111 if(aDir[aDir.size()-1] != _separator_) aDir += _separator_;
114 CreateDirectory(aDir.c_str(), NULL);
116 mkdir(aDir.c_str(), 0x1ff);
122 //============================================================================
123 // function : GetTempDir
124 // purpose : Returns a temp directory to store created files like "/tmp/sub_dir/"
125 //============================================================================
126 std::string GetTmpDir()
128 return GetTmpDirByPath( "" );
131 //============================================================================
132 // function : GetTempFileName
133 // purpose : Returns the unique temporary file name without any extension /tmp/something/file for Unix or c:\something\file for WIN32
134 //============================================================================
135 std::string GetTmpFileName()
137 std::string tmpDir = GetTmpDir();
138 std::string aFilePath = "";
139 if(IsExists(tmpDir)) {
140 srand((unsigned int)time(NULL));
141 int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
143 sprintf(buffer, "%d", aRND);
144 std::string aSubDir(buffer);
145 if(aSubDir.size() <= 1) aSubDir = std::string("123409876");
148 for(aRND = 0; IsExists(aFilePath); aRND++) {
149 sprintf(buffer, "%d", aRND);
150 aFilePath = tmpDir+buffer; //Build a unique file name
156 //============================================================================
157 // function : IsExists
158 // purpose : Returns True(False) if the path (not)exists
159 //============================================================================
160 bool IsExists(const std::string& thePath)
163 if ( GetFileAttributes ( thePath.c_str() ) == 0xFFFFFFFF ) {
164 if ( GetLastError () == ERROR_FILE_NOT_FOUND ) {
169 int status = access ( thePath.c_str() , F_OK );
170 if (status != 0) return false;
175 //============================================================================
176 // function : IsWritable
177 // purpose : Returns True(False) if the path is (not) writable
178 //============================================================================
179 bool IsWritable(const std::string& thePath)
182 if ( GetFileAttributes ( thePath.c_str() ) == 0xFFFFFFFF ) {
183 if ( GetLastError () == FILE_ATTRIBUTE_READONLY ) {
188 int status = access(thePath.c_str(),W_OK);
189 if (status != 0) return false;
195 //============================================================================
196 // function : GetDirByPath
197 // purpose : Returns directory by path and converts it to native system format
198 //============================================================================
199 std::string GetDirByPath(const std::string& thePath)
203 std::string path = thePath;
204 std::string::size_type length = path.length();
206 //detect all separators in Unix format
207 for ( unsigned int i = 0; i < length; i++ )
213 //detect all separators in Windows format
214 for ( unsigned int i = 0; i < length; i++ )
216 if( path[i] == '\\' )
221 std::string::size_type pos = path.rfind('|');
222 if ( pos == std::string::npos )
225 //check for disk letter ( C: )
226 if ( path.length() == 2 && path[1] == ':' )
235 //remove right subdirectory or filename from path
236 path = path.substr( 0, pos );
239 length = path.length();
240 for ( unsigned int i = 0; i < length; i++ )
243 path[i] = _separator_;
248 //============================================================================
249 // function : IsEmptyDir
250 // purpose : Returns True(False) if the path (not) empty
251 // Also returns False if the path is not valid
252 //============================================================================
253 bool IsEmptyDir(const std::string& thePath)
255 if ( thePath.empty() || !IsExists(thePath))
261 WIN32_FIND_DATA aFileData;
262 HANDLE hFile = FindFirstFile( thePath.c_str(), &aFileData );
263 if ( hFile == INVALID_HANDLE_VALUE )
270 //close serching. path is not empty
276 if((dp = opendir(thePath.c_str())) == NULL)
278 //Could not open directory
283 result = true; //empty if no file found
284 while ((dirp = readdir(dp)) != NULL && result )
286 std::string file_name(dirp->d_name);
287 result = file_name.empty() || file_name == "." || file_name == ".."; //if any file - break and return false