1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : Basics_DirUtils.cxx
23 // Autor : Alexander A. BORODIN
26 #include "Basics_DirUtils.hxx"
32 # include <sys/stat.h>
42 # define _separator_ '\\'
44 # define _separator_ '/'
47 namespace Kernel_Utils
49 string GetBaseName( const std::string& file_path )
51 int pos = file_path.rfind( _separator_ );
53 return pos < file_path.size()-1 ? file_path.substr( pos+1 ) : "";
57 string GetTmpDirByEnv( const std::string& tmp_path_env )
60 char* val = getenv( tmp_path_env.c_str() );
61 val ? dir = string( val ) : "";
62 return GetTmpDirByPath( dir );
65 string GetTmpDirByPath( const std::string& tmp_path )
67 string aTmpDir = tmp_path;
71 char *Tmp_dir = getenv("TEMP");
74 Tmp_dir = getenv("TMP");
76 aTmpDir = string("C:\\");
78 aTmpDir = string(Tmp_dir);
81 aTmpDir = string(Tmp_dir);
83 aTmpDir = string("/tmp/");
87 if(aTmpDir[aTmpDir.size()-1] != _separator_)
90 srand((unsigned int)time(NULL));
91 int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
93 sprintf(buffer, "%d", aRND);
94 string aSubDir(buffer);
95 if(aSubDir.size() <= 1) aSubDir = string("123409876");
97 aTmpDir += aSubDir; //Get RND sub directory
99 string aDir = aTmpDir;
102 for(aRND = 0; IsExists(aDir); aRND++) {
103 sprintf(buffer, "%d", aRND);
104 aDir = aTmpDir+buffer; //Build a unique directory name
108 if(aDir[aDir.size()-1] != _separator_) aDir += _separator_;
111 CreateDirectory(aDir.c_str(), NULL);
113 mkdir(aDir.c_str(), 0x1ff);
119 //============================================================================
120 // function : GetTempDir
121 // purpose : Returns a temp directory to store created files like "/tmp/sub_dir/"
122 //============================================================================
125 return GetTmpDirByPath( "" );
128 //============================================================================
129 // function : GetTempFileName
130 // purpose : Returns the unique temporary file name without any extension /tmp/something/file for Unix or c:\something\file for WIN32
131 //============================================================================
132 string GetTmpFileName()
134 string tmpDir = GetTmpDir();
135 string aFilePath = "";
136 if(IsExists(tmpDir)) {
137 srand((unsigned int)time(NULL));
138 int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
140 sprintf(buffer, "%d", aRND);
141 string aSubDir(buffer);
142 if(aSubDir.size() <= 1) aSubDir = string("123409876");
145 for(aRND = 0; IsExists(aFilePath); aRND++) {
146 sprintf(buffer, "%d", aRND);
147 aFilePath = tmpDir+buffer; //Build a unique file name
153 //============================================================================
154 // function : IsExists
155 // purpose : Returns True(False) if the path (not)exists
156 //============================================================================
157 bool IsExists(const string& thePath)
160 if ( GetFileAttributes ( thePath.c_str() ) == 0xFFFFFFFF ) {
161 if ( GetLastError () == ERROR_FILE_NOT_FOUND ) {
166 int status = access ( thePath.c_str() , F_OK );
167 if (status != 0) return false;
172 //============================================================================
173 // function : GetDirByPath
174 // purpose : Returns directory by path and converts it to native system format
175 //============================================================================
176 string GetDirByPath(const string& thePath)
180 string path = thePath;
181 string::size_type length = path.length();
183 //detect all separators in Unix format
184 for ( int i = 0; i < length; i++ )
190 //detect all separators in Windows format
191 for ( int i = 0; i < length; i++ )
193 if( path[i] == '\\' )
198 string::size_type pos = path.rfind('|');
199 if ( pos == string::npos )
202 //check for disk letter ( C: )
203 if ( path.length() == 2 && path[1] == ':' )
212 //remove right subdirectory or filename from path
213 path = path.substr( 0, pos );
216 length = path.length();
217 for ( int i = 0; i < length; i++ )
220 path[i] = _separator_;
225 //============================================================================
226 // function : IsEmptyDir
227 // purpose : Returns True(False) if the path (not) empty
228 // Also returns False if the path is not valid
229 //============================================================================
230 bool IsEmptyDir(const string& thePath)
232 if ( thePath.empty() || !IsExists(thePath))
238 WIN32_FIND_DATA aFileData;
239 HANDLE hFile = FindFirstFile( thePath.c_str(), &aFileData );
240 if ( hFile == INVALID_HANDLE_VALUE )
247 //close serching. path is not empty
253 if((dp = opendir(thePath.c_str())) == NULL)
255 //Could not open directory
260 result = true; //empty if no file found
261 while ((dirp = readdir(dp)) != NULL && result )
263 string file_name(dirp->d_name);
264 result = file_name.empty() || file_name == "." || file_name == ".."; //if any file - break and return false