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