1 // Copyright (C) 2007-2019 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, 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"
31 # include <sys/stat.h>
37 #define access _access
43 # define _separator_ '\\'
45 # define _separator_ '/'
48 #define _extension_ ".hdf"
50 namespace Kernel_Utils
52 std::string GetBaseName( const std::string& file_path, const bool with_extension )
54 std::string tmp_str = file_path;
55 int pos = file_path.rfind( _separator_ );
57 tmp_str = pos < (int)file_path.size()-1 ? file_path.substr( pos+1 ) : "";
59 pos = tmp_str.rfind( _extension_ );
60 if( !with_extension && pos >= 0 )
61 tmp_str = pos < (int)tmp_str.size()-1 ? tmp_str.substr( 0, pos ) : "";
66 std::string GetDirName( const std::string& file_path )
68 int pos = file_path.rfind( _separator_ );
70 return pos < (int)file_path.size()-1 ? file_path.substr(0, pos ) : "";
71 return std::string(".");
74 std::string GetTmpDirByEnv( const std::string& tmp_path_env )
76 #if defined WIN32 && defined UNICODE
77 std::wstring w_tmp_path_env = utf8_decode_s( tmp_path_env );
78 wchar_t* val = _wgetenv( w_tmp_path_env.c_str() );
79 std::string dir = val ? utf8_encode_s(val) : "";
81 char* val = getenv( tmp_path_env.c_str() );
82 std::string dir = val ? val : "";
84 return GetTmpDirByPath( dir );
87 std::string GetTmpDirByPath( const std::string& tmp_path )
89 std::string aTmpDir = tmp_path;
90 if ( aTmpDir == "" || !IsExists( aTmpDir ))
94 wchar_t *wTmp_dir = _wgetenv( L"TEMP" );
95 if ( wTmp_dir == NULL ) {
96 wTmp_dir = _wgetenv(L"TMP");
100 aTmpDir = utf8_encode_s( wTmp_dir );
102 char *Tmp_dir = getenv("TEMP");
103 if ( Tmp_dir == NULL )
105 Tmp_dir = getenv("TMP");
106 if ( Tmp_dir == NULL )
119 if ( aTmpDir.back() != _separator_ )
120 aTmpDir += _separator_;
122 srand( (unsigned int)time( NULL ));
123 int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
125 sprintf( buffer, "%d", aRND );
126 std::string aSubDir( buffer );
127 if ( aSubDir.size() <= 1 ) aSubDir = "123409876";
129 aTmpDir += aSubDir; //Get RND sub directory
131 std::string aDir = aTmpDir;
133 for ( aRND = 0; IsExists( aDir ); aRND++ )
135 sprintf( buffer, "%d", aRND );
136 aDir = aTmpDir + buffer; //Build a unique directory name
139 if ( aDir.back() != _separator_ ) aDir += _separator_;
143 std::wstring aDirToCreate = utf8_decode_s(aDir);
145 std::string aDirToCreate = aDir;
147 CreateDirectory( aDirToCreate.c_str(), NULL );
149 mkdir( aDir.c_str(), 0x1ff );
154 //============================================================================
155 // function : GetTempDir
156 // purpose : Returns a temp directory to store created files like "/tmp/sub_dir/"
157 //============================================================================
158 std::string GetTmpDir()
160 return GetTmpDirByPath( "" );
163 //============================================================================
164 // function : GetTempFileName
165 // purpose : Returns the unique temporary file name without any extension /tmp/something/file for Unix or c:\something\file for WIN32
166 //============================================================================
167 std::string GetTmpFileName()
169 std::string tmpDir = GetTmpDir();
170 std::string aFilePath = "";
171 if(IsExists(tmpDir)) {
172 srand((unsigned int)time(NULL));
173 int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
175 sprintf(buffer, "%d", aRND);
176 std::string aSubDir(buffer);
177 if(aSubDir.size() <= 1) aSubDir = std::string("123409876");
180 for(aRND = 0; IsExists(aFilePath); aRND++) {
181 sprintf(buffer, "%d", aRND);
182 aFilePath = tmpDir+buffer; //Build a unique file name
188 std::string AddExtension( const std::string& name )
190 std::string tmp_str = name;
191 int pos = tmp_str.rfind( _extension_ );
193 return tmp_str.append( _extension_ );
197 //============================================================================
198 // function : IsExists
199 // purpose : Returns True(False) if the path (not)exists
200 //============================================================================
201 bool IsExists(const std::string& thePath)
203 #if defined WIN32 && defined UNICODE
204 int status = _waccess( utf8_decode_s( thePath).c_str(), F_OK );
206 int status = access ( thePath.c_str() , F_OK );
208 if (status != 0) return false;
212 //============================================================================
213 // function : IsWritable
214 // purpose : Returns True(False) if the path is (not) writable
215 //============================================================================
216 bool IsWritable(const std::string& thePath)
220 std::wstring aPathToCheck = utf8_decode_s( thePath );
222 std::string aPathToCheck = thePath;
224 if ( GetFileAttributes ( aPathToCheck.c_str() ) == 0xFFFFFFFF ) {
225 if ( GetLastError () == FILE_ATTRIBUTE_READONLY ) {
230 int status = access(thePath.c_str(),W_OK);
231 if (status != 0) return false;
237 //============================================================================
238 // function : GetDirByPath
239 // purpose : Returns directory by path and converts it to native system format
240 //============================================================================
241 std::string GetDirByPath(const std::string& thePath)
245 std::string path = thePath;
246 std::string::size_type length = path.length();
248 //detect all separators in Unix format
249 for ( unsigned int i = 0; i < length; i++ )
255 //detect all separators in Windows format
256 for ( unsigned int i = 0; i < length; i++ )
258 if( path[i] == '\\' )
263 std::string::size_type pos = path.rfind('|');
264 if ( pos == std::string::npos )
267 //check for disk letter ( C: )
268 if ( path.length() == 2 && path[1] == ':' )
277 //remove right subdirectory or filename from path
278 path = path.substr( 0, pos );
281 length = path.length();
282 for ( unsigned int i = 0; i < length; i++ )
285 path[i] = _separator_;
290 //============================================================================
291 // function : IsEmptyDir
292 // purpose : Returns True(False) if the path (not) empty
293 // Also returns False if the path is not valid
294 //============================================================================
295 bool IsEmptyDir(const std::string& thePath)
297 if ( thePath.empty() || !IsExists(thePath))
303 WIN32_FIND_DATA aFileData;
305 std::wstring aPathToCheck = utf8_decode_s(thePath);
307 std::string aPathToCheck = thePath;
310 HANDLE hFile = FindFirstFile( aPathToCheck.c_str(), &aFileData );
311 if ( hFile == INVALID_HANDLE_VALUE )
318 //close serching. path is not empty
324 if((dp = opendir(thePath.c_str())) == NULL)
326 //Could not open directory
331 result = true; //empty if no file found
332 while ((dirp = readdir(dp)) != NULL && result )
334 std::string file_name(dirp->d_name);
335 result = file_name.empty() || file_name == "." || file_name == ".."; //if any file - break and return false