1 // Copyright (C) 2007-2010 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 GetTmpDirByEnv( const std::string& tmp_path_env )
56 char* val = getenv( tmp_path_env.c_str() );
57 val ? dir = std::string( val ) : "";
58 return GetTmpDirByPath( dir );
61 std::string GetTmpDirByPath( const std::string& tmp_path )
63 std::string aTmpDir = tmp_path;
67 char *Tmp_dir = getenv("TEMP");
70 Tmp_dir = getenv("TMP");
72 aTmpDir = std::string("C:\\");
74 aTmpDir = std::string(Tmp_dir);
77 aTmpDir = std::string(Tmp_dir);
79 aTmpDir = std::string("/tmp/");
83 if(aTmpDir[aTmpDir.size()-1] != _separator_)
86 srand((unsigned int)time(NULL));
87 int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
89 sprintf(buffer, "%d", aRND);
90 std::string aSubDir(buffer);
91 if(aSubDir.size() <= 1) aSubDir = std::string("123409876");
93 aTmpDir += aSubDir; //Get RND sub directory
95 std::string aDir = aTmpDir;
98 for(aRND = 0; IsExists(aDir); aRND++) {
99 sprintf(buffer, "%d", aRND);
100 aDir = aTmpDir+buffer; //Build a unique directory name
104 if(aDir[aDir.size()-1] != _separator_) aDir += _separator_;
107 CreateDirectory(aDir.c_str(), NULL);
109 mkdir(aDir.c_str(), 0x1ff);
115 //============================================================================
116 // function : GetTempDir
117 // purpose : Returns a temp directory to store created files like "/tmp/sub_dir/"
118 //============================================================================
119 std::string GetTmpDir()
121 return GetTmpDirByPath( "" );
124 //============================================================================
125 // function : GetTempFileName
126 // purpose : Returns the unique temporary file name without any extension /tmp/something/file for Unix or c:\something\file for WIN32
127 //============================================================================
128 std::string GetTmpFileName()
130 std::string tmpDir = GetTmpDir();
131 std::string aFilePath = "";
132 if(IsExists(tmpDir)) {
133 srand((unsigned int)time(NULL));
134 int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
136 sprintf(buffer, "%d", aRND);
137 std::string aSubDir(buffer);
138 if(aSubDir.size() <= 1) aSubDir = std::string("123409876");
141 for(aRND = 0; IsExists(aFilePath); aRND++) {
142 sprintf(buffer, "%d", aRND);
143 aFilePath = tmpDir+buffer; //Build a unique file name
149 //============================================================================
150 // function : IsExists
151 // purpose : Returns True(False) if the path (not)exists
152 //============================================================================
153 bool IsExists(const std::string& thePath)
156 if ( GetFileAttributes ( thePath.c_str() ) == 0xFFFFFFFF ) {
157 if ( GetLastError () == ERROR_FILE_NOT_FOUND ) {
162 int status = access ( thePath.c_str() , F_OK );
163 if (status != 0) return false;
168 //============================================================================
169 // function : GetDirByPath
170 // purpose : Returns directory by path and converts it to native system format
171 //============================================================================
172 std::string GetDirByPath(const std::string& thePath)
176 std::string path = thePath;
177 std::string::size_type length = path.length();
179 //detect all separators in Unix format
180 for ( unsigned int i = 0; i < length; i++ )
186 //detect all separators in Windows format
187 for ( unsigned int i = 0; i < length; i++ )
189 if( path[i] == '\\' )
194 std::string::size_type pos = path.rfind('|');
195 if ( pos == std::string::npos )
198 //check for disk letter ( C: )
199 if ( path.length() == 2 && path[1] == ':' )
208 //remove right subdirectory or filename from path
209 path = path.substr( 0, pos );
212 length = path.length();
213 for ( unsigned int i = 0; i < length; i++ )
216 path[i] = _separator_;
221 //============================================================================
222 // function : IsEmptyDir
223 // purpose : Returns True(False) if the path (not) empty
224 // Also returns False if the path is not valid
225 //============================================================================
226 bool IsEmptyDir(const std::string& thePath)
228 if ( thePath.empty() || !IsExists(thePath))
234 WIN32_FIND_DATA aFileData;
235 HANDLE hFile = FindFirstFile( thePath.c_str(), &aFileData );
236 if ( hFile == INVALID_HANDLE_VALUE )
243 //close serching. path is not empty
249 if((dp = opendir(thePath.c_str())) == NULL)
251 //Could not open directory
256 result = true; //empty if no file found
257 while ((dirp = readdir(dp)) != NULL && result )
259 std::string file_name(dirp->d_name);
260 result = file_name.empty() || file_name == "." || file_name == ".."; //if any file - break and return false