1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : Basics_DirUtils.cxx
21 // Author : Alexander A. BORODIN
24 #include "Basics_DirUtils.hxx"
30 # include <sys/stat.h>
36 #define access _access
42 # define _separator_ '\\'
44 # define _separator_ '/'
47 namespace Kernel_Utils
49 std::string GetBaseName( const std::string& file_path )
51 int pos = file_path.rfind( _separator_ );
53 return pos < (int)file_path.size()-1 ? file_path.substr( pos+1 ) : "";
57 std::string GetDirName( const std::string& file_path )
59 int pos = file_path.rfind( _separator_ );
61 return pos < (int)file_path.size()-1 ? file_path.substr(0, pos ) : "";
62 return std::string(".");
65 std::string GetTmpDirByEnv( const std::string& tmp_path_env )
67 char* val = getenv( tmp_path_env.c_str() );
68 std::string dir = val ? val : "";
69 return GetTmpDirByPath( dir );
72 std::string GetTmpDirByPath( const std::string& tmp_path )
74 std::string aTmpDir = tmp_path;
78 char *Tmp_dir = getenv("TEMP");
81 Tmp_dir = getenv("TMP");
83 aTmpDir = std::string("C:\\");
85 aTmpDir = std::string(Tmp_dir);
88 aTmpDir = std::string(Tmp_dir);
90 aTmpDir = std::string("/tmp/");
94 if(aTmpDir[aTmpDir.size()-1] != _separator_)
97 srand((unsigned int)time(NULL));
98 int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
100 sprintf(buffer, "%d", aRND);
101 std::string aSubDir(buffer);
102 if(aSubDir.size() <= 1) aSubDir = std::string("123409876");
104 aTmpDir += aSubDir; //Get RND sub directory
106 std::string aDir = aTmpDir;
109 for(aRND = 0; IsExists(aDir); aRND++) {
110 sprintf(buffer, "%d", aRND);
111 aDir = aTmpDir+buffer; //Build a unique directory name
115 if(aDir[aDir.size()-1] != _separator_) aDir += _separator_;
118 CreateDirectory(aDir.c_str(), NULL);
120 mkdir(aDir.c_str(), 0x1ff);
126 //============================================================================
127 // function : GetTempDir
128 // purpose : Returns a temp directory to store created files like "/tmp/sub_dir/"
129 //============================================================================
130 std::string GetTmpDir()
132 return GetTmpDirByPath( "" );
135 //============================================================================
136 // function : GetTempFileName
137 // purpose : Returns the unique temporary file name without any extension /tmp/something/file for Unix or c:\something\file for WIN32
138 //============================================================================
139 std::string GetTmpFileName()
141 std::string tmpDir = GetTmpDir();
142 std::string aFilePath = "";
143 if(IsExists(tmpDir)) {
144 srand((unsigned int)time(NULL));
145 int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
147 sprintf(buffer, "%d", aRND);
148 std::string aSubDir(buffer);
149 if(aSubDir.size() <= 1) aSubDir = std::string("123409876");
152 for(aRND = 0; IsExists(aFilePath); aRND++) {
153 sprintf(buffer, "%d", aRND);
154 aFilePath = tmpDir+buffer; //Build a unique file name
160 //============================================================================
161 // function : IsExists
162 // purpose : Returns True(False) if the path (not)exists
163 //============================================================================
164 bool IsExists(const std::string& thePath)
166 int status = access ( thePath.c_str() , F_OK );
167 if (status != 0) return false;
171 //============================================================================
172 // function : IsWritable
173 // purpose : Returns True(False) if the path is (not) writable
174 //============================================================================
175 bool IsWritable(const std::string& thePath)
178 if ( GetFileAttributes ( thePath.c_str() ) == 0xFFFFFFFF ) {
179 if ( GetLastError () == FILE_ATTRIBUTE_READONLY ) {
184 int status = access(thePath.c_str(),W_OK);
185 if (status != 0) return false;
191 //============================================================================
192 // function : GetDirByPath
193 // purpose : Returns directory by path and converts it to native system format
194 //============================================================================
195 std::string GetDirByPath(const std::string& thePath)
199 std::string path = thePath;
200 std::string::size_type length = path.length();
202 //detect all separators in Unix format
203 for ( unsigned int i = 0; i < length; i++ )
209 //detect all separators in Windows format
210 for ( unsigned int i = 0; i < length; i++ )
212 if( path[i] == '\\' )
217 std::string::size_type pos = path.rfind('|');
218 if ( pos == std::string::npos )
221 //check for disk letter ( C: )
222 if ( path.length() == 2 && path[1] == ':' )
231 //remove right subdirectory or filename from path
232 path = path.substr( 0, pos );
235 length = path.length();
236 for ( unsigned int i = 0; i < length; i++ )
239 path[i] = _separator_;
244 //============================================================================
245 // function : IsEmptyDir
246 // purpose : Returns True(False) if the path (not) empty
247 // Also returns False if the path is not valid
248 //============================================================================
249 bool IsEmptyDir(const std::string& thePath)
251 if ( thePath.empty() || !IsExists(thePath))
257 WIN32_FIND_DATA aFileData;
258 HANDLE hFile = FindFirstFile( thePath.c_str(), &aFileData );
259 if ( hFile == INVALID_HANDLE_VALUE )
266 //close serching. path is not empty
272 if((dp = opendir(thePath.c_str())) == NULL)
274 //Could not open directory
279 result = true; //empty if no file found
280 while ((dirp = readdir(dp)) != NULL && result )
282 std::string file_name(dirp->d_name);
283 result = file_name.empty() || file_name == "." || file_name == ".."; //if any file - break and return false