Salome HOME
Merge branch 'rnv/unicode_path'
[modules/kernel.git] / src / Basics / Basics_DirUtils.cxx
1 // Copyright (C) 2007-2019  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 #ifndef WIN32
31 # include <sys/stat.h>
32 # include <dirent.h>
33 # include <unistd.h>
34 #else
35 #include <io.h>
36 #define F_OK 0
37 #define access _access
38 # include <windows.h>
39 # include <time.h>
40 #endif
41
42 #ifdef WIN32
43 # define _separator_ '\\'
44 #else
45 # define _separator_ '/'
46 #endif
47
48 #define _extension_ ".hdf"
49
50 namespace Kernel_Utils
51 {
52   std::string GetBaseName( const std::string& file_path, const bool with_extension )
53   {
54     std::string tmp_str = file_path;
55     int pos = file_path.rfind( _separator_ );
56     if ( pos >= 0 )
57       tmp_str = pos < (int)file_path.size()-1 ? file_path.substr( pos+1 ) : "";
58
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 ) : "";
62
63     return tmp_str;
64   }
65
66   std::string GetDirName( const std::string& file_path )
67   {
68     int pos = file_path.rfind( _separator_ );
69     if ( pos >= 0 )
70       return pos < (int)file_path.size()-1 ? file_path.substr(0, pos ) : "";
71     return std::string(".");
72   }
73
74   std::string GetTmpDirByEnv( const std::string& tmp_path_env )
75   {
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) : "";
80 #else 
81     char* val = getenv( tmp_path_env.c_str() );
82         std::string dir = val ? val : "";
83 #endif
84     return GetTmpDirByPath( dir );
85   }
86
87   std::string GetTmpDirByPath( const std::string& tmp_path )
88   {
89     std::string aTmpDir = tmp_path;
90     if ( aTmpDir == "" || !IsExists( aTmpDir ))
91     {
92 #ifdef WIN32
93 #ifdef UNICODE
94           wchar_t *wTmp_dir = _wgetenv( L"TEMP" );
95           if ( wTmp_dir == NULL ) {
96                         wTmp_dir = _wgetenv(L"TMP");
97                         if (wTmp_dir == NULL)
98                                 wTmp_dir = L"C:\\";
99           }
100           aTmpDir = utf8_encode_s( wTmp_dir );
101 #else
102       char *Tmp_dir = getenv("TEMP");
103       if ( Tmp_dir == NULL )
104       {
105         Tmp_dir = getenv("TMP");
106         if ( Tmp_dir == NULL )
107           aTmpDir = "C:\\";
108         else
109           aTmpDir = Tmp_dir;
110       }
111       else
112         aTmpDir = Tmp_dir;
113 #endif
114 #else
115       aTmpDir = "/tmp/";
116 #endif
117     }
118
119     if ( aTmpDir.back() != _separator_ )
120       aTmpDir += _separator_;
121
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
124     char buffer[127];
125     sprintf( buffer, "%d", aRND );
126     std::string aSubDir( buffer );
127     if ( aSubDir.size() <= 1 ) aSubDir = "123409876";
128
129     aTmpDir += aSubDir; //Get RND sub directory
130
131     std::string aDir = aTmpDir;
132
133     for ( aRND = 0; IsExists( aDir ); aRND++ )
134     {
135       sprintf( buffer, "%d", aRND );
136       aDir = aTmpDir + buffer;  //Build a unique directory name
137     }
138
139     if ( aDir.back() != _separator_ ) aDir += _separator_;
140
141 #ifdef WIN32    
142 #ifdef UNICODE
143         std::wstring aDirToCreate = utf8_decode_s(aDir);
144 #else
145         std::string aDirToCreate = aDir;
146 #endif
147     CreateDirectory( aDirToCreate.c_str(), NULL );
148 #else
149     mkdir( aDir.c_str(), 0x1ff );
150 #endif
151     return aDir;
152   }
153
154   //============================================================================
155   // function : GetTempDir
156   // purpose  : Returns a temp directory to store created files like "/tmp/sub_dir/"
157   //============================================================================
158   std::string GetTmpDir()
159   {
160     return GetTmpDirByPath( "" );
161   }
162
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()
168   {
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
174       char buffer[127];
175       sprintf(buffer, "%d", aRND);
176       std::string aSubDir(buffer);
177       if(aSubDir.size() <= 1) aSubDir = std::string("123409876");
178       
179       aFilePath = tmpDir;
180       for(aRND = 0; IsExists(aFilePath); aRND++) {
181         sprintf(buffer, "%d", aRND);
182         aFilePath = tmpDir+buffer;  //Build a unique file name
183       }
184     }
185     return aFilePath;
186   }
187   
188   std::string AddExtension( const std::string& name )
189   {
190     std::string tmp_str = name;
191     int pos = tmp_str.rfind( _extension_ );
192     if( pos < 0 )
193       return tmp_str.append( _extension_ );
194     return tmp_str;
195   }
196
197   //============================================================================
198   // function : IsExists
199   // purpose  : Returns True(False) if the path (not)exists
200   //============================================================================ 
201   bool IsExists(const std::string& thePath) 
202   {
203 #if defined WIN32 && defined UNICODE
204         int status = _waccess( utf8_decode_s( thePath).c_str(), F_OK );
205 #else
206     int status = access ( thePath.c_str() , F_OK ); 
207 #endif
208     if (status != 0) return false;
209     return true;
210   }
211
212   //============================================================================
213   // function : IsWritable
214   // purpose  : Returns True(False) if the path is (not) writable
215   //============================================================================ 
216   bool IsWritable(const std::string& thePath)
217   {
218 #ifdef WIN32
219 #ifdef UNICODE
220           std::wstring aPathToCheck = utf8_decode_s( thePath );
221 #else
222           std::string aPathToCheck = thePath;
223 #endif
224     if (  GetFileAttributes ( aPathToCheck.c_str()  ) == 0xFFFFFFFF  ) {
225       if (  GetLastError () == FILE_ATTRIBUTE_READONLY ) {
226         return false;
227       }
228     }
229 #else 
230     int status = access(thePath.c_str(),W_OK); 
231     if (status != 0) return false;
232 #endif
233     return true;
234   }
235
236
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)
242   {
243     if (thePath.empty())
244       return "";
245     std::string path = thePath;
246     std::string::size_type length = path.length();
247
248     //detect all separators in Unix format
249     for ( unsigned int i = 0; i < length; i++ )
250     {
251       if( path[i] == '/' )
252         path[i] = '|';
253     }
254
255     //detect all separators in Windows format
256     for ( unsigned int i = 0; i < length; i++ )
257     {
258       if( path[i] == '\\' )
259         path[i] = '|';
260     }
261
262
263     std::string::size_type pos = path.rfind('|');
264     if ( pos == std::string::npos )
265     {
266 #ifdef WIN32
267       //check for disk letter ( C: )
268       if ( path.length() == 2 && path[1] == ':' )
269         path += _separator_;
270 #else
271       //not valid path
272       return "";
273 #endif
274     }
275     else
276     {
277       //remove right subdirectory or filename from path
278       path = path.substr( 0, pos );
279     }
280
281     length = path.length();
282     for ( unsigned int i = 0; i < length; i++ )
283     {
284       if( path[i] == '|' )
285         path[i] = _separator_;
286     }
287     return path;
288   }
289
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) 
296   {
297     if ( thePath.empty() || !IsExists(thePath))
298       return false;
299
300     bool result = false;
301
302 #ifdef WIN32
303     WIN32_FIND_DATA aFileData;
304 #ifdef UNICODE
305         std::wstring aPathToCheck = utf8_decode_s(thePath);
306 #else
307         std::string aPathToCheck = thePath;
308 #endif
309
310     HANDLE hFile = FindFirstFile( aPathToCheck.c_str(), &aFileData );
311     if ( hFile == INVALID_HANDLE_VALUE )
312     {
313       //empty dir
314       result = true;
315     }
316     else
317     {
318       //close serching. path is not empty
319       FindClose( hFile );
320     }
321 #else
322     DIR *dp;
323     struct dirent *dirp;
324     if((dp  = opendir(thePath.c_str())) == NULL)
325     {
326       //Could not open directory
327       return false;
328     }
329     else
330     {
331       result = true; //empty if no file found
332       while ((dirp = readdir(dp)) != NULL && result )
333         {
334           std::string file_name(dirp->d_name);
335           result = file_name.empty() || file_name == "." || file_name == ".."; //if any file - break and return false
336         }
337         closedir(dp);
338     }
339 #endif
340     return result;
341   }
342 }