Salome HOME
updated copyright message
[modules/kernel.git] / src / Basics / Basics_DirUtils.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, 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 #include <process.h>
43 #endif
44
45 #ifdef WIN32
46 # define _separator_ '\\'
47 #else
48 # define _separator_ '/'
49 #endif
50
51 #define _extension_ ".hdf"
52
53 namespace Kernel_Utils
54 {
55   std::string GetBaseName( const std::string& file_path, const bool with_extension )
56   {
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 ) : "";
61
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 ) : "";
65
66     return tmp_str;
67   }
68
69   std::string GetDirName( const std::string& file_path )
70   {
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(".");
75   }
76
77   std::string GetTmpDirByEnv( const std::string& tmp_path_env )
78   {
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) : "";
83 #else
84     char* val = getenv( tmp_path_env.c_str() );
85         std::string dir = val ? val : "";
86 #endif
87     return GetTmpDirByPath( dir );
88   }
89
90   std::string GetTmpDirByPath( const std::string& tmp_path )
91   {
92     std::string aTmpDir = tmp_path;
93     if ( aTmpDir == "" || !IsExists( aTmpDir ))
94     {
95 #ifdef WIN32
96 #ifdef UNICODE
97           wchar_t *wTmp_dir = _wgetenv( L"TEMP" );
98           if ( wTmp_dir == NULL ) {
99                         wTmp_dir = _wgetenv(L"TMP");
100                         if (wTmp_dir == NULL)
101                                 wTmp_dir = L"C:\\";
102           }
103           aTmpDir = utf8_encode_s( wTmp_dir );
104 #else
105       char *Tmp_dir = getenv("TEMP");
106       if ( Tmp_dir == NULL )
107       {
108         Tmp_dir = getenv("TMP");
109         if ( Tmp_dir == NULL )
110           aTmpDir = "C:\\";
111         else
112           aTmpDir = Tmp_dir;
113       }
114       else
115         aTmpDir = Tmp_dir;
116 #endif
117 #else
118       aTmpDir = "/tmp/";
119 #endif
120     }
121
122     if ( aTmpDir.back() != _separator_ )
123       aTmpDir += _separator_;
124
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
127     char buffer[127];
128     sprintf( buffer, "%d", aRND );
129     std::string aSubDir( buffer );
130     if ( aSubDir.size() <= 1 ) aSubDir = "123409876";
131
132     aTmpDir += aSubDir; //Get RND sub directory
133
134 #ifdef WIN32
135     int pid = _getpid();
136 #else
137     int pid = getpid();
138 #endif
139
140     std::string aDir = aTmpDir + std::to_string(pid) + "_";
141
142     for ( aRND = 0; IsExists( aDir ); aRND++ )
143     {
144       sprintf( buffer, "%d", aRND );
145       aDir = aTmpDir + buffer;  //Build a unique directory name
146     }
147
148     if ( aDir.back() != _separator_ ) aDir += _separator_;
149
150 #ifdef WIN32
151 #ifdef UNICODE
152         std::wstring aDirToCreate = utf8_decode_s(aDir);
153 #else
154         std::string aDirToCreate = aDir;
155 #endif
156     CreateDirectory( aDirToCreate.c_str(), NULL );
157 #else
158     mkdir( aDir.c_str(), 0x1ff );
159 #endif
160     return aDir;
161   }
162
163   //============================================================================
164   // function : GetTempDir
165   // purpose  : Returns a temp directory to store created files like "/tmp/sub_dir/"
166   //============================================================================
167   std::string GetTmpDir()
168   {
169     return GetTmpDirByPath( "" );
170   }
171
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()
177   {
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
183       char buffer[127];
184       sprintf(buffer, "%d", aRND);
185       std::string aSubDir(buffer);
186       if(aSubDir.size() <= 1) aSubDir = std::string("123409876");
187
188       aFilePath = tmpDir;
189       for(aRND = 0; IsExists(aFilePath); aRND++) {
190         sprintf(buffer, "%d", aRND);
191         aFilePath = tmpDir+buffer;  //Build a unique file name
192       }
193     }
194     return aFilePath;
195   }
196
197   std::string AddExtension( const std::string& name )
198   {
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_ );
203     return tmp_str;
204   }
205
206   //============================================================================
207   // function : IsExists
208   // purpose  : Returns True(False) if the path (not)exists
209   //============================================================================
210   bool IsExists(const std::string& thePath)
211   {
212 #if defined WIN32 && defined UNICODE
213         int status = _waccess( utf8_decode_s( thePath).c_str(), F_OK );
214 #else
215     int status = access ( thePath.c_str() , F_OK );
216 #endif
217     if (status != 0) return false;
218     return true;
219   }
220
221   //============================================================================
222   // function : IsWritable
223   // purpose  : Returns True(False) if the path is (not) writable
224   //============================================================================
225   bool IsWritable(const std::string& thePath)
226   {
227 #ifdef WIN32
228 #ifdef UNICODE
229           std::wstring aPathToCheck = utf8_decode_s( thePath );
230 #else
231           std::string aPathToCheck = thePath;
232 #endif
233     if (  GetFileAttributes ( aPathToCheck.c_str()  ) == 0xFFFFFFFF  ) {
234       if (  GetLastError () == FILE_ATTRIBUTE_READONLY ) {
235         return false;
236       }
237     }
238 #else
239     int status = access(thePath.c_str(),W_OK);
240     if (status != 0) return false;
241 #endif
242     return true;
243   }
244
245
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)
251   {
252     if (thePath.empty())
253       return "";
254     std::string path = thePath;
255     std::string::size_type length = path.length();
256
257     //detect all separators in Unix format
258     for ( unsigned int i = 0; i < length; i++ )
259     {
260       if( path[i] == '/' )
261         path[i] = '|';
262     }
263
264     //detect all separators in Windows format
265     for ( unsigned int i = 0; i < length; i++ )
266     {
267       if( path[i] == '\\' )
268         path[i] = '|';
269     }
270
271
272     std::string::size_type pos = path.rfind('|');
273     if ( pos == std::string::npos )
274     {
275 #ifdef WIN32
276       //check for disk letter ( C: )
277       if ( path.length() == 2 && path[1] == ':' )
278         path += _separator_;
279 #else
280       //not valid path
281       return "";
282 #endif
283     }
284     else
285     {
286       //remove right subdirectory or filename from path
287       path = path.substr( 0, pos );
288     }
289
290     length = path.length();
291     for ( unsigned int i = 0; i < length; i++ )
292     {
293       if( path[i] == '|' )
294         path[i] = _separator_;
295     }
296     return path;
297   }
298
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)
305   {
306     if ( thePath.empty() || !IsExists(thePath))
307       return false;
308
309     bool result = false;
310
311 #ifdef WIN32
312     WIN32_FIND_DATA aFileData;
313 #ifdef UNICODE
314         std::wstring aPathToCheck = utf8_decode_s(thePath);
315 #else
316         std::string aPathToCheck = thePath;
317 #endif
318
319     HANDLE hFile = FindFirstFile( aPathToCheck.c_str(), &aFileData );
320     if ( hFile == INVALID_HANDLE_VALUE )
321     {
322       //empty dir
323       result = true;
324     }
325     else
326     {
327       //close serching. path is not empty
328       FindClose( hFile );
329     }
330 #else
331     DIR *dp;
332     struct dirent *dirp;
333     if((dp  = opendir(thePath.c_str())) == NULL)
334     {
335       //Could not open directory
336       return false;
337     }
338     else
339     {
340       result = true; //empty if no file found
341       while ((dirp = readdir(dp)) != NULL && result )
342         {
343           std::string file_name(dirp->d_name);
344           result = file_name.empty() || file_name == "." || file_name == ".."; //if any file - break and return false
345         }
346         closedir(dp);
347     }
348 #endif
349     return result;
350   }
351
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(), '\\', '/');
359           return res;
360   }
361
362
363   //============================================================================
364   // function : BackSlashToSlash
365   // purpose  : Convert back slash to slash
366   //============================================================================
367   std::string HomePath() {
368 #ifdef WIN32
369     std::string homedir = getenv("USERPROFILE");
370 #else
371     std::string homedir = getenv("HOME");
372 #endif
373     return homedir;
374   }
375 }