Salome HOME
Merge branch 'V9_7_BR'
[modules/kernel.git] / src / Basics / Basics_DirUtils.cxx
1 // Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 //  File   : Basics_DirUtils.cxx
21 //  Author  : Alexander A. BORODIN
22 //  Module : SALOME
23 //
24 #include "Basics_DirUtils.hxx"
25 #include "Basics_Utils.hxx"
26 #include <stdio.h>
27 #include <errno.h>
28 #include <stdlib.h>
29
30 #include <algorithm>
31
32 #ifndef WIN32
33 # include <sys/stat.h>
34 # include <dirent.h>
35 # include <unistd.h>
36 #else
37 #include <io.h>
38 #define F_OK 0
39 #define access _access
40 # include <windows.h>
41 # include <time.h>
42 #endif
43
44 #ifdef WIN32
45 # define _separator_ '\\'
46 #else
47 # define _separator_ '/'
48 #endif
49
50 #define _extension_ ".hdf"
51
52 namespace Kernel_Utils
53 {
54   std::string GetBaseName( const std::string& file_path, const bool with_extension )
55   {
56     std::string tmp_str = file_path;
57     auto pos = file_path.rfind( _separator_ );
58     if ( pos != std::string::npos )
59       tmp_str = pos < file_path.size()-1 ? file_path.substr( pos+1 ) : "";
60
61     pos = tmp_str.rfind( _extension_ );
62     if( !with_extension && pos != std::string::npos )
63       tmp_str = pos < tmp_str.size()-1 ? tmp_str.substr( 0, pos ) : "";
64
65     return tmp_str;
66   }
67
68   std::string GetDirName( const std::string& file_path )
69   {
70     auto pos = file_path.rfind( _separator_ );
71     if ( pos != std::string::npos )
72       return pos < file_path.size()-1 ? file_path.substr(0, pos ) : "";
73     return std::string(".");
74   }
75
76   std::string GetTmpDirByEnv( const std::string& tmp_path_env )
77   {
78 #if defined WIN32 && defined UNICODE
79    std::wstring w_tmp_path_env = utf8_decode_s( tmp_path_env );
80    wchar_t* val = _wgetenv( w_tmp_path_env.c_str() );
81    std::string dir = val ? utf8_encode_s(val) : "";
82 #else 
83     char* val = getenv( tmp_path_env.c_str() );
84         std::string dir = val ? val : "";
85 #endif
86     return GetTmpDirByPath( dir );
87   }
88
89   std::string GetTmpDirByPath( const std::string& tmp_path )
90   {
91     std::string aTmpDir = tmp_path;
92     if ( aTmpDir == "" || !IsExists( aTmpDir ))
93     {
94 #ifdef WIN32
95 #ifdef UNICODE
96           wchar_t *wTmp_dir = _wgetenv( L"TEMP" );
97           if ( wTmp_dir == NULL ) {
98                         wTmp_dir = _wgetenv(L"TMP");
99                         if (wTmp_dir == NULL)
100                                 wTmp_dir = L"C:\\";
101           }
102           aTmpDir = utf8_encode_s( wTmp_dir );
103 #else
104       char *Tmp_dir = getenv("TEMP");
105       if ( Tmp_dir == NULL )
106       {
107         Tmp_dir = getenv("TMP");
108         if ( Tmp_dir == NULL )
109           aTmpDir = "C:\\";
110         else
111           aTmpDir = Tmp_dir;
112       }
113       else
114         aTmpDir = Tmp_dir;
115 #endif
116 #else
117       aTmpDir = "/tmp/";
118 #endif
119     }
120
121     if ( aTmpDir.back() != _separator_ )
122       aTmpDir += _separator_;
123
124     srand( (unsigned int)time( NULL ));
125     int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
126     char buffer[127];
127     sprintf( buffer, "%d", aRND );
128     std::string aSubDir( buffer );
129     if ( aSubDir.size() <= 1 ) aSubDir = "123409876";
130
131     aTmpDir += aSubDir; //Get RND sub directory
132
133     std::string aDir = aTmpDir;
134
135     for ( aRND = 0; IsExists( aDir ); aRND++ )
136     {
137       sprintf( buffer, "%d", aRND );
138       aDir = aTmpDir + buffer;  //Build a unique directory name
139     }
140
141     if ( aDir.back() != _separator_ ) aDir += _separator_;
142
143 #ifdef WIN32    
144 #ifdef UNICODE
145         std::wstring aDirToCreate = utf8_decode_s(aDir);
146 #else
147         std::string aDirToCreate = aDir;
148 #endif
149     CreateDirectory( aDirToCreate.c_str(), NULL );
150 #else
151     mkdir( aDir.c_str(), 0x1ff );
152 #endif
153     return aDir;
154   }
155
156   //============================================================================
157   // function : GetTempDir
158   // purpose  : Returns a temp directory to store created files like "/tmp/sub_dir/"
159   //============================================================================
160   std::string GetTmpDir()
161   {
162     return GetTmpDirByPath( "" );
163   }
164
165   //============================================================================
166   // function : GetTempFileName
167   // purpose  : Returns the unique temporary file name without any extension /tmp/something/file for Unix or c:\something\file for WIN32
168   //============================================================================ 
169   std::string GetTmpFileName()
170   {
171     std::string tmpDir = GetTmpDir();
172     std::string aFilePath = "";
173     if(IsExists(tmpDir)) {
174       srand((unsigned int)time(NULL));
175       int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
176       char buffer[127];
177       sprintf(buffer, "%d", aRND);
178       std::string aSubDir(buffer);
179       if(aSubDir.size() <= 1) aSubDir = std::string("123409876");
180       
181       aFilePath = tmpDir;
182       for(aRND = 0; IsExists(aFilePath); aRND++) {
183         sprintf(buffer, "%d", aRND);
184         aFilePath = tmpDir+buffer;  //Build a unique file name
185       }
186     }
187     return aFilePath;
188   }
189   
190   std::string AddExtension( const std::string& name )
191   {
192     std::string tmp_str = name;
193     auto pos = tmp_str.rfind( _extension_ );
194     if( pos == std::string::npos )
195       return tmp_str.append( _extension_ );
196     return tmp_str;
197   }
198
199   //============================================================================
200   // function : IsExists
201   // purpose  : Returns True(False) if the path (not)exists
202   //============================================================================ 
203   bool IsExists(const std::string& thePath) 
204   {
205 #if defined WIN32 && defined UNICODE
206         int status = _waccess( utf8_decode_s( thePath).c_str(), F_OK );
207 #else
208     int status = access ( thePath.c_str() , F_OK ); 
209 #endif
210     if (status != 0) return false;
211     return true;
212   }
213
214   //============================================================================
215   // function : IsWritable
216   // purpose  : Returns True(False) if the path is (not) writable
217   //============================================================================ 
218   bool IsWritable(const std::string& thePath)
219   {
220 #ifdef WIN32
221 #ifdef UNICODE
222           std::wstring aPathToCheck = utf8_decode_s( thePath );
223 #else
224           std::string aPathToCheck = thePath;
225 #endif
226     if (  GetFileAttributes ( aPathToCheck.c_str()  ) == 0xFFFFFFFF  ) {
227       if (  GetLastError () == FILE_ATTRIBUTE_READONLY ) {
228         return false;
229       }
230     }
231 #else 
232     int status = access(thePath.c_str(),W_OK); 
233     if (status != 0) return false;
234 #endif
235     return true;
236   }
237
238
239   //============================================================================
240   // function : GetDirByPath
241   // purpose  : Returns directory by path and converts it to native system format
242   //============================================================================ 
243   std::string GetDirByPath(const std::string& thePath)
244   {
245     if (thePath.empty())
246       return "";
247     std::string path = thePath;
248     std::string::size_type length = path.length();
249
250     //detect all separators in Unix format
251     for ( unsigned int i = 0; i < length; i++ )
252     {
253       if( path[i] == '/' )
254         path[i] = '|';
255     }
256
257     //detect all separators in Windows format
258     for ( unsigned int i = 0; i < length; i++ )
259     {
260       if( path[i] == '\\' )
261         path[i] = '|';
262     }
263
264
265     std::string::size_type pos = path.rfind('|');
266     if ( pos == std::string::npos )
267     {
268 #ifdef WIN32
269       //check for disk letter ( C: )
270       if ( path.length() == 2 && path[1] == ':' )
271         path += _separator_;
272 #else
273       //not valid path
274       return "";
275 #endif
276     }
277     else
278     {
279       //remove right subdirectory or filename from path
280       path = path.substr( 0, pos );
281     }
282
283     length = path.length();
284     for ( unsigned int i = 0; i < length; i++ )
285     {
286       if( path[i] == '|' )
287         path[i] = _separator_;
288     }
289     return path;
290   }
291
292   //============================================================================
293   // function : IsEmptyDir
294   // purpose  : Returns True(False) if the path (not) empty
295   //            Also returns False if the path is not valid
296   //============================================================================ 
297   bool IsEmptyDir(const std::string& thePath) 
298   {
299     if ( thePath.empty() || !IsExists(thePath))
300       return false;
301
302     bool result = false;
303
304 #ifdef WIN32
305     WIN32_FIND_DATA aFileData;
306 #ifdef UNICODE
307         std::wstring aPathToCheck = utf8_decode_s(thePath);
308 #else
309         std::string aPathToCheck = thePath;
310 #endif
311
312     HANDLE hFile = FindFirstFile( aPathToCheck.c_str(), &aFileData );
313     if ( hFile == INVALID_HANDLE_VALUE )
314     {
315       //empty dir
316       result = true;
317     }
318     else
319     {
320       //close serching. path is not empty
321       FindClose( hFile );
322     }
323 #else
324     DIR *dp;
325     struct dirent *dirp;
326     if((dp  = opendir(thePath.c_str())) == NULL)
327     {
328       //Could not open directory
329       return false;
330     }
331     else
332     {
333       result = true; //empty if no file found
334       while ((dirp = readdir(dp)) != NULL && result )
335         {
336           std::string file_name(dirp->d_name);
337           result = file_name.empty() || file_name == "." || file_name == ".."; //if any file - break and return false
338         }
339         closedir(dp);
340     }
341 #endif
342     return result;
343   }
344
345   //============================================================================
346   // function : BackSlashToSlash  
347   // purpose  : Convert back slash to slash
348   //============================================================================ 
349   std::string BackSlashToSlash(const std::string& path) {
350           std::string res = path;
351           std::replace(res.begin(), res.end(), '\\', '/');
352           return res;
353   }
354
355
356   //============================================================================
357   // function : BackSlashToSlash  
358   // purpose  : Convert back slash to slash
359   //============================================================================ 
360   std::string HomePath() {
361 #ifdef WIN32
362     std::string homedir = getenv("USERPROFILE");
363 #else
364     std::string homedir = getenv("HOME");       
365 #endif
366     return homedir;
367   }
368 }