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