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 GetTmpDirByEnv( const std::string& tmp_path_env )
55 char* val = getenv( tmp_path_env.c_str() );
56 std::string dir = val ? val : "";
57 return GetTmpDirByPath( dir );
60 std::string GetTmpDirByPath( const std::string& tmp_path )
62 std::string aTmpDir = tmp_path;
66 char *Tmp_dir = getenv("TEMP");
69 Tmp_dir = getenv("TMP");
71 aTmpDir = std::string("C:\\");
73 aTmpDir = std::string(Tmp_dir);
76 aTmpDir = std::string(Tmp_dir);
78 aTmpDir = std::string("/tmp/");
82 if(aTmpDir[aTmpDir.size()-1] != _separator_)
85 srand((unsigned int)time(NULL));
86 int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
88 sprintf(buffer, "%d", aRND);
89 std::string aSubDir(buffer);
90 if(aSubDir.size() <= 1) aSubDir = std::string("123409876");
92 aTmpDir += aSubDir; //Get RND sub directory
94 std::string aDir = aTmpDir;
97 for(aRND = 0; IsExists(aDir); aRND++) {
98 sprintf(buffer, "%d", aRND);
99 aDir = aTmpDir+buffer; //Build a unique directory name
103 if(aDir[aDir.size()-1] != _separator_) aDir += _separator_;
106 CreateDirectory(aDir.c_str(), NULL);
108 mkdir(aDir.c_str(), 0x1ff);
114 //============================================================================
115 // function : GetTempDir
116 // purpose : Returns a temp directory to store created files like "/tmp/sub_dir/"
117 //============================================================================
118 std::string GetTmpDir()
120 return GetTmpDirByPath( "" );
123 //============================================================================
124 // function : GetTempFileName
125 // purpose : Returns the unique temporary file name without any extension /tmp/something/file for Unix or c:\something\file for WIN32
126 //============================================================================
127 std::string GetTmpFileName()
129 std::string tmpDir = GetTmpDir();
130 std::string aFilePath = "";
131 if(IsExists(tmpDir)) {
132 srand((unsigned int)time(NULL));
133 int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
135 sprintf(buffer, "%d", aRND);
136 std::string aSubDir(buffer);
137 if(aSubDir.size() <= 1) aSubDir = std::string("123409876");
140 for(aRND = 0; IsExists(aFilePath); aRND++) {
141 sprintf(buffer, "%d", aRND);
142 aFilePath = tmpDir+buffer; //Build a unique file name
148 //============================================================================
149 // function : IsExists
150 // purpose : Returns True(False) if the path (not)exists
151 //============================================================================
152 bool IsExists(const std::string& thePath)
155 if ( GetFileAttributes ( thePath.c_str() ) == 0xFFFFFFFF ) {
156 if ( GetLastError () == ERROR_FILE_NOT_FOUND ) {
161 int status = access ( thePath.c_str() , F_OK );
162 if (status != 0) return false;
167 //============================================================================
168 // function : GetDirByPath
169 // purpose : Returns directory by path and converts it to native system format
170 //============================================================================
171 std::string GetDirByPath(const std::string& thePath)
175 std::string path = thePath;
176 std::string::size_type length = path.length();
178 //detect all separators in Unix format
179 for ( unsigned int i = 0; i < length; i++ )
185 //detect all separators in Windows format
186 for ( unsigned int i = 0; i < length; i++ )
188 if( path[i] == '\\' )
193 std::string::size_type pos = path.rfind('|');
194 if ( pos == std::string::npos )
197 //check for disk letter ( C: )
198 if ( path.length() == 2 && path[1] == ':' )
207 //remove right subdirectory or filename from path
208 path = path.substr( 0, pos );
211 length = path.length();
212 for ( unsigned int i = 0; i < length; i++ )
215 path[i] = _separator_;
220 //============================================================================
221 // function : IsEmptyDir
222 // purpose : Returns True(False) if the path (not) empty
223 // Also returns False if the path is not valid
224 //============================================================================
225 bool IsEmptyDir(const std::string& thePath)
227 if ( thePath.empty() || !IsExists(thePath))
233 WIN32_FIND_DATA aFileData;
234 HANDLE hFile = FindFirstFile( thePath.c_str(), &aFileData );
235 if ( hFile == INVALID_HANDLE_VALUE )
242 //close serching. path is not empty
248 if((dp = opendir(thePath.c_str())) == NULL)
250 //Could not open directory
255 result = true; //empty if no file found
256 while ((dirp = readdir(dp)) != NULL && result )
258 std::string file_name(dirp->d_name);
259 result = file_name.empty() || file_name == "." || file_name == ".."; //if any file - break and return false