1 // Copyright (C) 2007-2008 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.
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
22 // File : SALOMEDSImpl_Tool.cxx
23 // Created : Mon Oct 21 16:24:34 2002
24 // Author : Sergey RUIN
26 // Module : SALOMEDSImpl
34 #include "SALOMEDSImpl_Tool.hxx"
39 #include <sys/types.h>
45 //#include <winbase.h>
53 bool Exists(const 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 string SALOMEDSImpl_Tool::GetTmpDir()
77 //Find a temporary directory to store a file
81 char *Tmp_dir = getenv("SALOME_TMP_DIR");
83 aTmpDir = string(Tmp_dir);
85 if(aTmpDir[aTmpDir.size()-1] != '\\') aTmpDir+='\\';
87 if(aTmpDir[aTmpDir.size()-1] != '/') aTmpDir+='/';
92 aTmpDir = string("C:\\");
94 aTmpDir = 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 string aSubDir(buffer);
103 if(aSubDir.size() <= 1) aSubDir = string("123409876");
105 aTmpDir += aSubDir; //Get RND sub directory
107 string aDir = aTmpDir;
110 for(aRND = 0; 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 string& theDirectory,
137 const vector<string>& theFiles,
138 const bool IsDirDeleted)
140 string aDirName = theDirectory;
142 int i, aLength = theFiles.size();
143 for(i=1; i<=aLength; i++) {
144 string aFile(aDirName);
145 aFile += theFiles[i-1];
146 if(!Exists(aFile)) continue;
149 DeleteFile(aFile.c_str());
151 unlink(aFile.c_str());
156 if(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 string SALOMEDSImpl_Tool::GetNameFromPath(const 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 string SALOMEDSImpl_Tool::GetDirFromPath(const string& thePath) {
187 if (thePath.empty()) return "";
189 int pos = thePath.rfind('/');
192 path = thePath.substr(0, pos+1);
195 pos = thePath.rfind('\\');
196 if(pos > 0) path = thePath.substr(0, pos+1);
199 pos = thePath.rfind('|');
200 if(pos > 0) path = thePath.substr(0, pos+1);
206 #ifdef WIN32 //Check if the only disk letter is given as path
207 if(path.size() == 2 && path[1] == ':') path +='\\';
210 for(int i = 0, len = path.size(); i<len; i++)
211 if(path[i] == '|') path[i] = '/';
215 //============================================================================
217 // purpose : The functions returns a list of substring of initial string
218 // divided by given separator
219 //============================================================================
220 vector<string> SALOMEDSImpl_Tool::splitString(const string& theValue, char separator)
223 if(theValue[0] == separator && theValue.size() == 1) return vs;
224 int pos = theValue.find(separator);
226 vs.push_back(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 //============================================================================
246 vector<string> SALOMEDSImpl_Tool::splitStringWithEmpty(const string& theValue, char sep)
248 vector<string> aResult;
249 if(theValue[0] == sep ) aResult.push_back(string());
250 int pos = theValue.find(sep);
252 aResult.push_back(theValue);
257 if(s[0] == sep) s = s.substr(1, s.size());
258 while((pos = s.find(sep)) >= 0) {
259 aResult.push_back(s.substr(0, pos));
260 s = s.substr(pos+1, s.size());
263 if(!s.empty() && s[0] != sep) aResult.push_back(s);
264 if(theValue[theValue.size()-1] == sep) aResult.push_back(string());
269 //============================================================================
271 // purpose : The functions returns a list of lists of substrings of initial string
272 // divided by two given separators include empty strings
273 //============================================================================
274 vector< vector<string> > SALOMEDSImpl_Tool::splitStringWithEmpty(const string& theValue, char sep1, char sep2)
276 vector< vector<string> > aResult;
277 if(theValue.size() > 0) {
278 vector<string> aSections = splitStringWithEmpty( theValue, sep1 );
279 for( int i = 0, n = aSections.size(); i < n; i++ )
280 aResult.push_back( splitStringWithEmpty( aSections[i], sep2 ) );
286 void SALOMEDSImpl_Tool::GetSystemDate(int& year, int& month, int& day, int& hours, int& minutes, int& seconds)
291 GetLocalTime ( &st );
297 minutes = st.wMinute;
298 seconds = st.wSecond;
302 struct timezone tzone;
305 status = gettimeofday( &tval, &tzone );
306 memcpy(&transfert, localtime((time_t *)&tval.tv_sec), sizeof(tm));
308 month = transfert.tm_mon + 1;
309 day = transfert.tm_mday;
310 year = transfert.tm_year + 1900;
311 hours = transfert.tm_hour;
312 minutes = transfert.tm_min ;
313 seconds = transfert.tm_sec ;
317 //Warning undef of Ascii Winwows define
321 string SALOMEDSImpl_Tool::GetUserName()
324 char* pBuff = new char[UNLEN + 1];
325 DWORD dwSize = UNLEN + 1;
327 ::GetUserNameA( pBuff, &dwSize );
328 string theTmpUserName(pBuff,(int)dwSize -1 );
329 retVal = theTmpUserName;
333 struct passwd *infos;
334 infos = getpwuid(getuid());
335 return string(infos->pw_name);