1 // Copyright (C) 2007-2024 CEA, EDF, 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 // Author : Alexander A. BORODIN
24 #include "Basics_DirUtils.hxx"
25 #include "Basics_Utils.hxx"
39 #define access _access
46 # define _separator_ '\\'
48 # define _separator_ '/'
51 #define _extension_ ".hdf"
53 namespace Kernel_Utils
55 std::string GetBaseName( const std::string& file_path, const bool with_extension )
57 std::string tmp_str = file_path;
58 auto pos = file_path.rfind( _separator_ );
59 if ( pos != std::string::npos )
60 tmp_str = pos < file_path.size()-1 ? file_path.substr( pos+1 ) : "";
62 pos = tmp_str.rfind( _extension_ );
63 if( !with_extension && pos != std::string::npos )
64 tmp_str = pos < tmp_str.size()-1 ? tmp_str.substr( 0, pos ) : "";
69 std::string GetDirName( const std::string& file_path )
71 auto pos = file_path.rfind( _separator_ );
72 if ( pos != std::string::npos )
73 return pos < file_path.size()-1 ? file_path.substr(0, pos ) : "";
74 return std::string(".");
77 std::string GetTmpDirByEnv( const std::string& tmp_path_env )
79 #if defined WIN32 && defined UNICODE
80 std::wstring w_tmp_path_env = utf8_decode_s( tmp_path_env );
81 wchar_t* val = _wgetenv( w_tmp_path_env.c_str() );
82 std::string dir = val ? utf8_encode_s(val) : "";
84 char* val = getenv( tmp_path_env.c_str() );
85 std::string dir = val ? val : "";
87 return GetTmpDirByPath( dir );
90 std::string GetTmpDirByPath( const std::string& tmp_path )
92 std::string aTmpDir = tmp_path;
93 if ( aTmpDir == "" || !IsExists( aTmpDir ))
97 wchar_t *wTmp_dir = _wgetenv( L"TEMP" );
98 if ( wTmp_dir == NULL ) {
99 wTmp_dir = _wgetenv(L"TMP");
100 if (wTmp_dir == NULL)
103 aTmpDir = utf8_encode_s( wTmp_dir );
105 char *Tmp_dir = getenv("TEMP");
106 if ( Tmp_dir == NULL )
108 Tmp_dir = getenv("TMP");
109 if ( Tmp_dir == NULL )
122 if ( aTmpDir.back() != _separator_ )
123 aTmpDir += _separator_;
125 srand( (unsigned int)time( NULL ));
126 int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
128 sprintf( buffer, "%d", aRND );
129 std::string aSubDir( buffer );
130 if ( aSubDir.size() <= 1 ) aSubDir = "123409876";
132 aTmpDir += aSubDir; //Get RND sub directory
140 std::string aDir = aTmpDir + std::to_string(pid) + "_";
142 for ( aRND = 0; IsExists( aDir ); aRND++ )
144 sprintf( buffer, "%d", aRND );
145 aDir = aTmpDir + buffer; //Build a unique directory name
148 if ( aDir.back() != _separator_ ) aDir += _separator_;
152 std::wstring aDirToCreate = utf8_decode_s(aDir);
154 std::string aDirToCreate = aDir;
156 CreateDirectory( aDirToCreate.c_str(), NULL );
158 mkdir( aDir.c_str(), 0x1ff );
163 //============================================================================
164 // function : GetTempDir
165 // purpose : Returns a temp directory to store created files like "/tmp/sub_dir/"
166 //============================================================================
167 std::string GetTmpDir()
169 return GetTmpDirByPath( "" );
172 //============================================================================
173 // function : GetTempFileName
174 // purpose : Returns the unique temporary file name without any extension /tmp/something/file for Unix or c:\something\file for WIN32
175 //============================================================================
176 std::string GetTmpFileName()
178 std::string tmpDir = GetTmpDir();
179 std::string aFilePath = "";
180 if(IsExists(tmpDir)) {
181 srand((unsigned int)time(NULL));
182 int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
184 sprintf(buffer, "%d", aRND);
185 std::string aSubDir(buffer);
186 if(aSubDir.size() <= 1) aSubDir = std::string("123409876");
189 for(aRND = 0; IsExists(aFilePath); aRND++) {
190 sprintf(buffer, "%d", aRND);
191 aFilePath = tmpDir+buffer; //Build a unique file name
197 std::string AddExtension( const std::string& name )
199 std::string tmp_str = name;
200 auto pos = tmp_str.rfind( _extension_ );
201 if( pos == std::string::npos )
202 return tmp_str.append( _extension_ );
206 //============================================================================
207 // function : IsExists
208 // purpose : Returns True(False) if the path (not)exists
209 //============================================================================
210 bool IsExists(const std::string& thePath)
212 #if defined WIN32 && defined UNICODE
213 int status = _waccess( utf8_decode_s( thePath).c_str(), F_OK );
215 int status = access ( thePath.c_str() , F_OK );
217 if (status != 0) return false;
221 //============================================================================
222 // function : IsWritable
223 // purpose : Returns True(False) if the path is (not) writable
224 //============================================================================
225 bool IsWritable(const std::string& thePath)
229 std::wstring aPathToCheck = utf8_decode_s( thePath );
231 std::string aPathToCheck = thePath;
233 if ( GetFileAttributes ( aPathToCheck.c_str() ) == 0xFFFFFFFF ) {
234 if ( GetLastError () == FILE_ATTRIBUTE_READONLY ) {
239 int status = access(thePath.c_str(),W_OK);
240 if (status != 0) return false;
246 //============================================================================
247 // function : GetDirByPath
248 // purpose : Returns directory by path and converts it to native system format
249 //============================================================================
250 std::string GetDirByPath(const std::string& thePath)
254 std::string path = thePath;
255 std::string::size_type length = path.length();
257 //detect all separators in Unix format
258 for ( unsigned int i = 0; i < length; i++ )
264 //detect all separators in Windows format
265 for ( unsigned int i = 0; i < length; i++ )
267 if( path[i] == '\\' )
272 std::string::size_type pos = path.rfind('|');
273 if ( pos == std::string::npos )
276 //check for disk letter ( C: )
277 if ( path.length() == 2 && path[1] == ':' )
286 //remove right subdirectory or filename from path
287 path = path.substr( 0, pos );
290 length = path.length();
291 for ( unsigned int i = 0; i < length; i++ )
294 path[i] = _separator_;
299 //============================================================================
300 // function : IsEmptyDir
301 // purpose : Returns True(False) if the path (not) empty
302 // Also returns False if the path is not valid
303 //============================================================================
304 bool IsEmptyDir(const std::string& thePath)
306 if ( thePath.empty() || !IsExists(thePath))
312 WIN32_FIND_DATA aFileData;
314 std::wstring aPathToCheck = utf8_decode_s(thePath);
316 std::string aPathToCheck = thePath;
319 HANDLE hFile = FindFirstFile( aPathToCheck.c_str(), &aFileData );
320 if ( hFile == INVALID_HANDLE_VALUE )
327 //close serching. path is not empty
333 if((dp = opendir(thePath.c_str())) == NULL)
335 //Could not open directory
340 result = true; //empty if no file found
341 while ((dirp = readdir(dp)) != NULL && result )
343 std::string file_name(dirp->d_name);
344 result = file_name.empty() || file_name == "." || file_name == ".."; //if any file - break and return false
352 //============================================================================
353 // function : BackSlashToSlash
354 // purpose : Convert back slash to slash
355 //============================================================================
356 std::string BackSlashToSlash(const std::string& path) {
357 std::string res = path;
358 std::replace(res.begin(), res.end(), '\\', '/');
363 //============================================================================
364 // function : BackSlashToSlash
365 // purpose : Convert back slash to slash
366 //============================================================================
367 std::string HomePath() {
369 std::string homedir = getenv("USERPROFILE");
371 std::string homedir = getenv("HOME");