]> SALOME platform Git repositories - modules/kernel.git/blob - src/SALOMEDSImpl/SALOMEDSImpl_Tool.cxx
Salome HOME
Merge from V5_1_4_BR (5_1_4rc2) 09/06/2010
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_Tool.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  File      : SALOMEDSImpl_Tool.cxx
24 //  Created   : Mon Oct 21 16:24:34 2002
25 //  Author    : Sergey RUIN
26 //  Project   : SALOME
27 //  Module    : SALOMEDSImpl
28 //
29 #include <stdio.h>
30 #include <iostream> 
31 #include <fstream>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "SALOMEDSImpl_Tool.hxx"
36
37 #ifndef WIN32
38 #include <sys/time.h>
39 #include <sys/stat.h>
40 #include <sys/types.h>
41 #include <pwd.h> 
42 #include <unistd.h>
43 #else
44 #include <time.h>
45 #include <lmcons.h>
46 //#include <winbase.h>
47 #include <windows.h>
48 #endif
49
50
51 bool Exists(const std::string thePath) 
52 {
53 #ifdef WIN32 
54   if (  GetFileAttributes (  thePath.c_str()  ) == 0xFFFFFFFF  ) { 
55     if (  GetLastError () == ERROR_FILE_NOT_FOUND  ) {
56       return false;
57     }
58   }
59 #else 
60   int status = access ( thePath.c_str() , F_OK ); 
61   if (status != 0) return false;
62 #endif
63   return true;
64 }
65
66
67
68
69 //============================================================================
70 // function : GetTempDir
71 // purpose  : Return a temp directory to store created files like "/tmp/sub_dir/" 
72 //============================================================================ 
73 std::string SALOMEDSImpl_Tool::GetTmpDir()
74 {
75   //Find a temporary directory to store a file
76
77   std::string aTmpDir;
78
79   char *Tmp_dir = getenv("SALOME_TMP_DIR");
80   if(Tmp_dir != NULL) {
81     aTmpDir = std::string(Tmp_dir);
82 #ifdef WIN32
83     if(aTmpDir[aTmpDir.size()-1] != '\\') aTmpDir+='\\';
84 #else
85     if(aTmpDir[aTmpDir.size()-1] != '/') aTmpDir+='/';
86 #endif      
87   }
88   else {
89 #ifdef WIN32
90     aTmpDir = std::string("C:\\");
91 #else
92     aTmpDir = std::string("/tmp/");
93 #endif
94   }
95
96   srand((unsigned int)time(NULL));
97   int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
98   char buffer[127];
99   sprintf(buffer, "%d", aRND);
100   std::string aSubDir(buffer);
101   if(aSubDir.size() <= 1) aSubDir = std::string("123409876");
102
103   aTmpDir += aSubDir; //Get RND sub directory
104
105   std::string aDir = aTmpDir;
106   
107   if(Exists(aDir)) {
108     for(aRND = 0; Exists(aDir); aRND++) {
109       sprintf(buffer, "%d", aRND);
110       aDir = aTmpDir+buffer;  //Build a unique directory name
111     }
112   }
113
114 #ifdef WIN32
115   if(aDir[aTmpDir.size()-1] != '\\') aDir+='\\';
116 #else
117   if(aDir[aTmpDir.size()-1] != '/') aDir+='/';
118 #endif
119
120
121 #ifdef WIN32
122   CreateDirectory(aDir.c_str(), NULL);
123 #else
124   mkdir(aDir.c_str(), 0x1ff); 
125 #endif
126
127   return aDir;
128 }
129
130 //============================================================================
131 // function : RemoveTemporaryFiles
132 // purpose  : Removes files listed in theFileList
133 //============================================================================
134 void SALOMEDSImpl_Tool::RemoveTemporaryFiles(const std::string& theDirectory, 
135                                              const std::vector<std::string>& theFiles,
136                                              const bool IsDirDeleted)
137 {
138   std::string aDirName = theDirectory;
139
140   int i, aLength = theFiles.size();
141   for(i=1; i<=aLength; i++) {
142     std::string aFile(aDirName);
143     aFile += theFiles[i-1];
144     if(!Exists(aFile)) continue;
145
146 #ifdef WIN32
147     DeleteFile(aFile.c_str());
148 #else 
149     unlink(aFile.c_str());
150 #endif
151   }
152
153   if(IsDirDeleted) {
154     if(Exists(aDirName)) {
155 #ifdef WIN32
156       RemoveDirectory(aDirName.c_str());
157 #else
158       rmdir(aDirName.c_str());
159 #endif
160     }
161   }
162
163 }
164
165 //============================================================================
166 // function : GetNameFromPath
167 // purpose  : Returns the name by the path
168 //============================================================================
169 std::string SALOMEDSImpl_Tool::GetNameFromPath(const std::string& thePath) {
170   if (thePath.empty()) return "";
171   int pos = thePath.rfind('/');
172   if(pos > 0) return thePath.substr(pos+1, thePath.size());
173   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   return thePath;
178 }
179
180 //============================================================================
181 // function : GetDirFromPath
182 // purpose  : Returns the dir by the path
183 //============================================================================
184 std::string SALOMEDSImpl_Tool::GetDirFromPath(const std::string& thePath) {
185 #ifdef WIN32
186   std::string separator = "\\";
187 #else
188   std::string separator = "/";
189 #endif
190
191   std::string path;
192   if (!thePath.empty()) {
193     int pos = thePath.rfind('/');
194     if (pos < 0) pos = thePath.rfind('\\');
195     if (pos < 0) pos = thePath.rfind('|');
196     
197     if (pos > 0)
198       path = thePath.substr(0, pos+1);
199     else
200       path = std::string(".") + separator;
201
202 #ifdef WIN32  //Check if the only disk letter is given as path
203     if (path.size() == 2 && path[1] == ':') path += separator;
204 #endif
205     
206     while ( (pos=path.find('|')) >= 0 )
207       path.replace(pos, 1, separator);
208   }
209   
210   return path;
211 }
212
213 //============================================================================
214 // function : 
215 // purpose  : The functions returns a list of substring of initial string 
216 //            divided by given separator
217 //============================================================================
218 std::vector<std::string> SALOMEDSImpl_Tool::splitString(const std::string& theValue, char separator)
219 {
220   std::vector<std::string> vs;
221   if(theValue[0] == separator && theValue.size() == 1) return vs;
222   int pos = theValue.find(separator);
223   if(pos < 0) {
224     vs.push_back(theValue);
225     return vs;
226   }
227  
228   std::string s = theValue;
229   if(s[0] == separator) s = s.substr(1, s.size());
230   while((pos = s.find(separator)) >= 0) {
231     vs.push_back(s.substr(0, pos));
232     s = s.substr(pos+1, s.size());
233   }
234                
235   if(!s.empty() && s[0] != separator) vs.push_back(s);
236   return vs;
237 }
238
239 //============================================================================
240 // function : 
241 // purpose  : The functions returns a list of substring of initial string 
242 //            divided by given separator include empty strings
243 //============================================================================
244 std::vector<std::string> SALOMEDSImpl_Tool::splitStringWithEmpty(const std::string& theValue, char sep)
245 {
246   std::vector<std::string> aResult;
247   if(theValue[0] == sep ) aResult.push_back(std::string());
248   int pos = theValue.find(sep);
249   if(pos < 0 ) {
250     aResult.push_back(theValue);
251     return aResult;
252   }
253
254   std::string s = theValue;
255   if(s[0] == sep) s = s.substr(1, s.size());
256   while((pos = s.find(sep)) >= 0) {
257     aResult.push_back(s.substr(0, pos));
258     s = s.substr(pos+1, s.size());
259   }
260
261   if(!s.empty() && s[0] != sep) aResult.push_back(s);
262   if(theValue[theValue.size()-1] == sep) aResult.push_back(std::string());
263
264   return aResult;
265 }
266
267 //============================================================================
268 // function : 
269 // purpose  : The functions returns a list of lists of substrings of initial string 
270 //            divided by two given separators include empty strings
271 //============================================================================
272 std::vector< std::vector<std::string> > SALOMEDSImpl_Tool::splitStringWithEmpty(const std::string& theValue, char sep1, char sep2)
273 {
274   std::vector< std::vector<std::string> > aResult;
275   if(theValue.size() > 0) {
276     std::vector<std::string> aSections = splitStringWithEmpty( theValue, sep1 );
277     for( int i = 0, n = aSections.size(); i < n; i++ )
278       aResult.push_back( splitStringWithEmpty( aSections[i], sep2 ) );
279   }
280   return aResult;
281 }
282
283
284 void SALOMEDSImpl_Tool::GetSystemDate(int& year, int& month, int& day, int& hours, int& minutes, int& seconds)
285 {
286 #ifdef WIN32
287   SYSTEMTIME    st;
288
289   GetLocalTime ( &st );
290
291   month = st.wMonth;
292   day = st.wDay;
293   year = st.wYear;
294   hours = st.wHour;
295   minutes = st.wMinute;
296   seconds = st.wSecond;
297 #else
298   struct tm transfert;
299   struct timeval tval;
300   struct timezone tzone;
301   int status;
302
303  status = gettimeofday( &tval, &tzone );
304  memcpy(&transfert, localtime((time_t *)&tval.tv_sec), sizeof(tm));
305
306  month    = transfert.tm_mon + 1;
307  day      = transfert.tm_mday;
308  year     = transfert.tm_year + 1900;
309  hours    = transfert.tm_hour;
310  minutes  = transfert.tm_min ;
311  seconds  = transfert.tm_sec ;
312 #endif
313 }
314
315 //Warning undef of Ascii Winwows define
316 #ifdef WIN32
317 # undef GetUserName
318 #endif
319 std::string SALOMEDSImpl_Tool::GetUserName()
320 {
321 #ifdef WIN32
322   char*  pBuff = new char[UNLEN + 1];
323   DWORD  dwSize = UNLEN + 1;
324   std::string retVal;
325   ::GetUserNameA( pBuff, &dwSize );
326   std::string theTmpUserName(pBuff,(int)dwSize -1 );
327   retVal = theTmpUserName;
328   delete [] pBuff;
329   return retVal;
330 #else
331  struct passwd *infos;
332  infos = getpwuid(getuid()); 
333  return std::string(infos->pw_name);
334 #endif
335 }
336
337
338
339
340
341