1 // Copyright (C) 2007-2014 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");
83 aTmpDir = std::string(Tmp_dir);
85 if(aTmpDir[aTmpDir.size()-1] != '\\') aTmpDir+='\\';
87 if(aTmpDir[aTmpDir.size()-1] != '/') aTmpDir+='/';
92 aTmpDir = std::string("C:\\");
94 aTmpDir = std::string("/tmp/");
98 srand((unsigned int)time(NULL));
99 int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
101 sprintf(buffer, "%d", aRND);
102 std::string aSubDir(buffer);
103 if(aSubDir.size() <= 1) aSubDir = std::string("123409876");
105 aTmpDir += aSubDir; //Get RND sub directory
107 std::string aDir = aTmpDir;
109 if(SALOMEDS_Exists(aDir)) {
110 for(aRND = 0; SALOMEDS_Exists(aDir); aRND++) {
111 sprintf(buffer, "%d", aRND);
112 aDir = aTmpDir+buffer; //Build a unique directory name
117 if(aDir[aTmpDir.size()-1] != '\\') aDir+='\\';
119 if(aDir[aTmpDir.size()-1] != '/') aDir+='/';
124 CreateDirectory(aDir.c_str(), NULL);
126 mkdir(aDir.c_str(), 0x1ff);
132 //============================================================================
133 // function : RemoveTemporaryFiles
134 // purpose : Removes files listed in theFileList
135 //============================================================================
136 void SALOMEDSImpl_Tool::RemoveTemporaryFiles(const std::string& theDirectory,
137 const std::vector<std::string>& theFiles,
138 const bool IsDirDeleted)
140 std::string aDirName = theDirectory;
142 int i, aLength = theFiles.size();
143 for(i=1; i<=aLength; i++) {
144 std::string aFile(aDirName);
145 aFile += theFiles[i-1];
146 if(!SALOMEDS_Exists(aFile)) continue;
149 DeleteFile(aFile.c_str());
151 unlink(aFile.c_str());
156 if(SALOMEDS_Exists(aDirName)) {
158 RemoveDirectory(aDirName.c_str());
160 rmdir(aDirName.c_str());
167 //============================================================================
168 // function : GetNameFromPath
169 // purpose : Returns the name by the path
170 //============================================================================
171 std::string SALOMEDSImpl_Tool::GetNameFromPath(const std::string& thePath) {
172 if (thePath.empty()) return "";
173 int pos = thePath.rfind('/');
174 if(pos >= 0) return thePath.substr(pos+1, thePath.size());
175 pos = thePath.rfind('\\');
176 if(pos >= 0) return thePath.substr(pos+1, thePath.size());
177 pos = thePath.rfind('|');
178 if(pos >= 0) return thePath.substr(pos+1, thePath.size());
182 //============================================================================
183 // function : GetDirFromPath
184 // purpose : Returns the dir by the path
185 //============================================================================
186 std::string SALOMEDSImpl_Tool::GetDirFromPath(const std::string& thePath) {
188 std::string separator = "\\";
190 std::string separator = "/";
194 if (!thePath.empty()) {
195 int pos = thePath.rfind('/');
196 if (pos < 0) pos = thePath.rfind('\\');
197 if (pos < 0) pos = thePath.rfind('|');
200 path = thePath.substr(0, pos+1);
202 path = std::string(".") + separator;
204 #ifdef WIN32 //Check if the only disk letter is given as path
205 if (path.size() == 2 && path[1] == ':') path += separator;
208 while ( (pos=path.find('|')) >= 0 )
209 path.replace(pos, 1, separator);
215 //============================================================================
217 // purpose : The functions returns a list of substring of initial string
218 // divided by given separator
219 //============================================================================
220 std::vector<std::string> SALOMEDSImpl_Tool::splitString(const std::string& theValue, char separator)
222 std::vector<std::string> vs;
223 if(theValue[0] == separator && theValue.size() == 1) return vs;
224 int pos = theValue.find(separator);
226 vs.push_back(theValue);
230 std::string s = theValue;
231 if(s[0] == separator) s = s.substr(1, s.size());
232 while((pos = s.find(separator)) >= 0) {
233 vs.push_back(s.substr(0, pos));
234 s = s.substr(pos+1, s.size());
237 if(!s.empty() && s[0] != separator) vs.push_back(s);
241 //============================================================================
243 // purpose : The functions returns a list of substring of initial string
244 // divided by given separator include empty strings
245 //============================================================================
247 std::vector<std::string> treatRepetation(const std::string& theValue);
249 std::vector<std::string> treatRepetation(const std::string& theValue)
251 std::vector<std::string> aResult;
252 int pos = theValue.find(";*=");
255 aResult.push_back(theValue);
258 std::string val(theValue.substr(0, pos));
259 std::string suffix(theValue.substr(pos+3));
261 std::istringstream tmp(suffix);
263 for(int i=0; i<nb; i++)
264 aResult.push_back(val);
268 std::vector<std::string> SALOMEDSImpl_Tool::splitStringWithEmpty(const std::string& theValue, char sep)
270 std::vector<std::string> aResult;
271 if(theValue[0] == sep ) aResult.push_back(std::string());
272 int pos = theValue.find(sep);
276 std::vector<std::string> tmp = treatRepetation(theValue);
277 std::copy(tmp.begin(), tmp.end(), std::back_insert_iterator< std::vector<std::string> >(aResult));
280 aResult.push_back(theValue);
284 std::string s = theValue;
285 if(s[0] == sep) s = s.substr(1, s.size());
286 while((pos = s.find(sep)) >= 0) {
289 std::vector<std::string> tmp = treatRepetation(s.substr(0, pos));
290 std::copy(tmp.begin(), tmp.end(), std::back_insert_iterator< std::vector<std::string> >(aResult));
293 aResult.push_back(s.substr(0, pos));
294 s = s.substr(pos+1, s.size());
297 if(!s.empty() && s[0] != sep) {
300 std::vector<std::string> tmp = treatRepetation(s);
301 std::copy(tmp.begin(), tmp.end(), std::back_insert_iterator< std::vector<std::string> >(aResult));
304 aResult.push_back(s);
306 if(theValue[theValue.size()-1] == sep) aResult.push_back(std::string());
311 //============================================================================
313 // purpose : The functions returns a list of lists of substrings of initial string
314 // divided by two given separators include empty strings
315 //============================================================================
316 std::vector< std::vector<std::string> > SALOMEDSImpl_Tool::splitStringWithEmpty(const std::string& theValue, char sep1, char sep2)
318 std::vector< std::vector<std::string> > aResult;
319 if(theValue.size() > 0) {
320 std::vector<std::string> aSections = splitStringWithEmpty( theValue, sep1 );
321 for( int i = 0, n = aSections.size(); i < n; i++ )
322 aResult.push_back( splitStringWithEmpty( aSections[i], sep2 ) );
328 void SALOMEDSImpl_Tool::GetSystemDate(int& year, int& month, int& day, int& hours, int& minutes, int& seconds)
333 GetLocalTime ( &st );
339 minutes = st.wMinute;
340 seconds = st.wSecond;
344 struct timezone tzone;
347 status = gettimeofday( &tval, &tzone );
348 memcpy(&transfert, localtime((time_t *)&tval.tv_sec), sizeof(tm));
350 month = transfert.tm_mon + 1;
351 day = transfert.tm_mday;
352 year = transfert.tm_year + 1900;
353 hours = transfert.tm_hour;
354 minutes = transfert.tm_min ;
355 seconds = transfert.tm_sec ;
359 //Warning undef of Ascii Winwows define
363 std::string SALOMEDSImpl_Tool::GetUserName()
366 char* pBuff = new char[UNLEN + 1];
367 DWORD dwSize = UNLEN + 1;
369 ::GetUserNameA( pBuff, &dwSize );
370 std::string theTmpUserName(pBuff,(int)dwSize -1 );
371 retVal = theTmpUserName;
375 struct passwd *infos;
376 infos = getpwuid(getuid());
377 return std::string(infos->pw_name);