Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_Tool.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 //  File      : SALOMEDSImpl_Tool.cxx
21 //  Created   : Mon Oct 21 16:24:34 2002
22 //  Author    : Sergey RUIN
23
24 //  Project   : SALOME
25 //  Module    : SALOMEDSImpl
26
27 #include "SALOMEDSImpl_Tool.hxx"
28
29 #include <stdio.h>
30 #include <iostream> 
31 #include <fstream>
32
33
34 #ifndef WNT
35 #include <sys/time.h>
36 #include <sys/stat.h>
37 #include <sys/types.h>
38 #include <pwd.h> 
39 #include <unistd.h>
40 #else
41 #include <time.h>
42 #include <lmcons.h>
43 #endif
44
45 #include <stdlib.h>
46
47 using namespace std;
48
49
50
51 bool Exists(const string thePath) 
52 {
53 #ifdef WNT 
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 string SALOMEDSImpl_Tool::GetTmpDir()
74 {
75   //Find a temporary directory to store a file
76
77   string aTmpDir;
78
79   char *Tmp_dir = getenv("SALOME_TMP_DIR");
80   if(Tmp_dir != NULL) {
81     aTmpDir = 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 = string("C:\\");
91 #else
92     aTmpDir = 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   string aSubDir(buffer);
101   if(aSubDir.size() <= 1) aSubDir = string("123409876");
102
103   aTmpDir += aSubDir; //Get RND sub directory
104
105   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 WNT
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 string& theDirectory, 
135                                              const vector<string>& theFiles,
136                                              const bool IsDirDeleted)
137 {
138   string aDirName = theDirectory;
139
140   int i, aLength = theFiles.size();
141   for(i=1; i<=aLength; i++) {
142     string aFile(aDirName);
143     aFile += theFiles[i-1];
144     if(!Exists(aFile)) continue;
145
146 #ifdef WNT
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 WNT
156       RemoveDirectory(aDireName.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 string SALOMEDSImpl_Tool::GetNameFromPath(const 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 string SALOMEDSImpl_Tool::GetDirFromPath(const string& thePath) {
185   if (thePath.empty()) return "";
186
187   int pos = thePath.rfind('/');
188   string path;
189   if(pos > 0) {
190     path = thePath.substr(0, pos+1);
191   }
192   if(path.empty()) {
193     pos = thePath.rfind('\\');
194     if(pos > 0) path = thePath.substr(0, pos+1); 
195   }
196   if(path.empty()) {
197     pos = thePath.rfind('|');
198     if(pos > 0) path = thePath.substr(0, pos+1); 
199   }
200   if(path.empty()) {
201     path = thePath+"/";
202   }
203   
204 #ifdef WNT  //Check if the only disk letter is given as path
205   if(path.size() == 2 && path[1] == ":") path +='\\';
206 #endif
207
208   for(int i = 0, len = path.size(); i<len; i++) 
209     if(path[i] == '|') path[i] = '/';
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 vector<string> SALOMEDSImpl_Tool::splitString(const string& theValue, char separator)
219 {
220   vector<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   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 void SALOMEDSImpl_Tool::GetSystemDate(int& year, int& month, int& day, int& hours, int& minutes, int& seconds)
241 {
242 #ifdef WNT
243   SYSTEMTIME    st;
244
245   GetLocalTime ( &st );
246
247   month = st.wMonth;
248   day = st.wDay;
249   year = st.wYear;
250   hours = st.wHour;
251   minutes = st.wMinute;
252   seconds = st.wSecond;
253 #else
254   struct tm transfert;
255   struct timeval tval;
256   struct timezone tzone;
257   int status;
258
259  status = gettimeofday( &tval, &tzone );
260  memcpy(&transfert, localtime((time_t *)&tval.tv_sec), sizeof(tm));
261
262  month    = transfert.tm_mon + 1;
263  day      = transfert.tm_mday;
264  year     = transfert.tm_year + 1900;
265  hours    = transfert.tm_hour;
266  minutes  = transfert.tm_min ;
267  seconds  = transfert.tm_sec ;
268 #endif
269 }
270
271 string SALOMEDSImpl_Tool::GetUserName()
272 {
273 #ifdef WNT
274   char*  pBuff = new char[UNLEN + 1];
275   DWORD  dwSize = UNLEN + 1;
276   string retVal;
277   GetUserName ( pBuff, &dwSize );
278   string theTmpUserName(pBuff,(int)dwSize -1 );
279   retVal = theTmpUserName;
280   delete [] pBuff;
281   return retVal;
282 #else
283  struct passwd *infos;
284  infos = getpwuid(getuid()); 
285  return string(infos->pw_name);
286 #endif
287 }
288
289
290
291
292
293