1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : SALOMEDSImpl_Tool.cxx
24 // Created : Mon Oct 21 16:24:34 2002
25 // Author : Sergey RUIN
27 // Module : SALOMEDSImpl
37 #include "SALOMEDSImpl_Tool.hxx"
42 #include <sys/types.h>
48 //#include <winbase.h>
53 bool SALOMEDS_Exists(const std::string thePath)
56 if ( GetFileAttributes ( thePath.c_str() ) == 0xFFFFFFFF ) {
57 if ( GetLastError () == ERROR_FILE_NOT_FOUND ) {
62 int status = access ( thePath.c_str() , F_OK );
63 if (status != 0) return false;
71 //============================================================================
72 // function : GetTempDir
73 // purpose : Return a temp directory to store created files like "/tmp/sub_dir/"
74 //============================================================================
75 std::string SALOMEDSImpl_Tool::GetTmpDir()
77 //Find a temporary directory to store a file
81 char *Tmp_dir = getenv("SALOME_TMP_DIR");
82 if ( Tmp_dir != NULL && SALOMEDS_Exists( Tmp_dir ))
86 if ( aTmpDir.back() != '\\') aTmpDir += '\\';
88 if ( aTmpDir.back() != '/') aTmpDir += '/';
100 srand( (unsigned int)time( NULL ));
101 int aRND = 999 + (int) (100000.0*rand() / (RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
103 sprintf( buffer, "%d", aRND );
104 std::string aSubDir( buffer );
105 if ( aSubDir.size() <= 1 ) aSubDir = "123409876";
107 aTmpDir += aSubDir; //Get RND sub directory
109 std::string aDir = aTmpDir;
111 for ( aRND = 0; SALOMEDS_Exists( aDir ); aRND++ )
113 sprintf(buffer, "%d", aRND);
114 aDir = aTmpDir + buffer; //Build a unique directory name
118 if ( aDir.back() != '\\') aDir += '\\';
120 if ( aDir.back() != '/' ) aDir += '/';
125 CreateDirectory( aDir.c_str(), NULL );
127 mkdir( aDir.c_str(), 0x1ff );
133 //============================================================================
134 // function : RemoveTemporaryFiles
135 // purpose : Removes files listed in theFileList
136 //============================================================================
137 void SALOMEDSImpl_Tool::RemoveTemporaryFiles(const std::string& theDirectory,
138 const std::vector<std::string>& theFiles,
139 const bool IsDirDeleted)
141 std::string aDirName = theDirectory;
143 int i, aLength = theFiles.size();
144 for(i=1; i<=aLength; i++) {
145 std::string aFile(aDirName);
146 aFile += theFiles[i-1];
147 if(!SALOMEDS_Exists(aFile)) continue;
150 DeleteFile(aFile.c_str());
152 unlink(aFile.c_str());
157 if(SALOMEDS_Exists(aDirName)) {
159 RemoveDirectory(aDirName.c_str());
161 rmdir(aDirName.c_str());
168 //============================================================================
169 // function : GetNameFromPath
170 // purpose : Returns the name by the path
171 //============================================================================
172 std::string SALOMEDSImpl_Tool::GetNameFromPath(const std::string& thePath) {
173 if (thePath.empty()) return "";
174 int pos = thePath.rfind('/');
175 if(pos >= 0) return thePath.substr(pos+1, thePath.size());
176 pos = thePath.rfind('\\');
177 if(pos >= 0) return thePath.substr(pos+1, thePath.size());
178 pos = thePath.rfind('|');
179 if(pos >= 0) return thePath.substr(pos+1, thePath.size());
183 //============================================================================
184 // function : GetDirFromPath
185 // purpose : Returns the dir by the path
186 //============================================================================
187 std::string SALOMEDSImpl_Tool::GetDirFromPath(const std::string& thePath) {
189 std::string separator = "\\";
191 std::string separator = "/";
195 if (!thePath.empty()) {
196 int pos = thePath.rfind('/');
197 if (pos < 0) pos = thePath.rfind('\\');
198 if (pos < 0) pos = thePath.rfind('|');
201 path = thePath.substr(0, pos+1);
203 path = std::string(".") + separator;
205 #ifdef WIN32 //Check if the only disk letter is given as path
206 if (path.size() == 2 && path[1] == ':') path += separator;
209 while ( (pos=path.find('|')) >= 0 )
210 path.replace(pos, 1, separator);
216 //============================================================================
218 // purpose : The functions returns a list of substring of initial string
219 // divided by given separator
220 //============================================================================
221 std::vector<std::string> SALOMEDSImpl_Tool::splitString(const std::string& theValue, char separator)
223 std::vector<std::string> vs;
224 if(theValue[0] == separator && theValue.size() == 1) return vs;
225 int pos = theValue.find(separator);
227 vs.push_back(theValue);
231 std::string s = theValue;
232 if(s[0] == separator) s = s.substr(1, s.size());
233 while((pos = s.find(separator)) >= 0) {
234 vs.push_back(s.substr(0, pos));
235 s = s.substr(pos+1, s.size());
238 if(!s.empty() && s[0] != separator) vs.push_back(s);
242 //============================================================================
244 // purpose : The functions returns a list of substring of initial string
245 // divided by given separator include empty strings
246 //============================================================================
248 std::vector<std::string> treatRepetation(const std::string& theValue);
250 std::vector<std::string> treatRepetation(const std::string& theValue)
252 std::vector<std::string> aResult;
253 int pos = theValue.find(";*=");
256 aResult.push_back(theValue);
259 std::string val(theValue.substr(0, pos));
260 std::string suffix(theValue.substr(pos+3));
262 std::istringstream tmp(suffix);
264 for(int i=0; i<nb; i++)
265 aResult.push_back(val);
269 std::vector<std::string> SALOMEDSImpl_Tool::splitStringWithEmpty(const std::string& theValue, char sep)
271 std::vector<std::string> aResult;
272 if(theValue[0] == sep ) aResult.push_back(std::string());
273 int pos = theValue.find(sep);
277 std::vector<std::string> tmp = treatRepetation(theValue);
278 std::copy(tmp.begin(), tmp.end(), std::back_insert_iterator< std::vector<std::string> >(aResult));
281 aResult.push_back(theValue);
285 std::string s = theValue;
286 if(s[0] == sep) s = s.substr(1, s.size());
287 while((pos = s.find(sep)) >= 0) {
290 std::vector<std::string> tmp = treatRepetation(s.substr(0, pos));
291 std::copy(tmp.begin(), tmp.end(), std::back_insert_iterator< std::vector<std::string> >(aResult));
294 aResult.push_back(s.substr(0, pos));
295 s = s.substr(pos+1, s.size());
298 if(!s.empty() && s[0] != sep) {
301 std::vector<std::string> tmp = treatRepetation(s);
302 std::copy(tmp.begin(), tmp.end(), std::back_insert_iterator< std::vector<std::string> >(aResult));
305 aResult.push_back(s);
307 if(theValue[theValue.size()-1] == sep) aResult.push_back(std::string());
312 //============================================================================
314 // purpose : The functions returns a list of lists of substrings of initial string
315 // divided by two given separators include empty strings
316 //============================================================================
317 std::vector< std::vector<std::string> > SALOMEDSImpl_Tool::splitStringWithEmpty(const std::string& theValue, char sep1, char sep2)
319 std::vector< std::vector<std::string> > aResult;
320 if(theValue.size() > 0) {
321 std::vector<std::string> aSections = splitStringWithEmpty( theValue, sep1 );
322 for( int i = 0, n = aSections.size(); i < n; i++ )
323 aResult.push_back( splitStringWithEmpty( aSections[i], sep2 ) );
329 void SALOMEDSImpl_Tool::GetSystemDate(int& year, int& month, int& day, int& hours, int& minutes, int& seconds)
334 GetLocalTime ( &st );
340 minutes = st.wMinute;
341 seconds = st.wSecond;
345 struct timezone tzone;
348 /*status = */ gettimeofday( &tval, &tzone );
349 memcpy(&transfert, localtime((time_t *)&tval.tv_sec), sizeof(tm));
351 month = transfert.tm_mon + 1;
352 day = transfert.tm_mday;
353 year = transfert.tm_year + 1900;
354 hours = transfert.tm_hour;
355 minutes = transfert.tm_min ;
356 seconds = transfert.tm_sec ;
360 //Warning undef of Ascii Winwows define
364 std::string SALOMEDSImpl_Tool::GetUserName()
367 char* pBuff = new char[UNLEN + 1];
368 DWORD dwSize = UNLEN + 1;
370 ::GetUserNameA( pBuff, &dwSize );
371 std::string theTmpUserName(pBuff,(int)dwSize -1 );
372 retVal = theTmpUserName;
376 struct passwd *infos;
377 infos = getpwuid(getuid());
378 return std::string(infos->pw_name);