Salome HOME
New implementation of SALOMEDSImpl package based on DF data structure instead of...
[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 #else
40 #include <time.h>
41 #include <lmcons.h>
42 #endif
43
44 #include <stdlib.h>
45
46 using namespace std;
47
48
49
50 bool Exists(const string thePath) 
51 {
52 #ifdef WNT 
53   if (  GetFileAttributes (  thePath.c_str()  ) == 0xFFFFFFFF  ) { 
54     if (  GetLastError () != ERROR_FILE_NOT_FOUND  ) {
55       return false;
56     }
57   }
58 #else 
59   int status = access ( thePath.c_str() , F_OK ); 
60   if (status != 0) return false;
61 #endif
62   return true;
63 }
64
65
66
67
68 //============================================================================
69 // function : GetTempDir
70 // purpose  : Return a temp directory to store created files like "/tmp/sub_dir/" 
71 //============================================================================ 
72 string SALOMEDSImpl_Tool::GetTmpDir()
73 {
74   //Find a temporary directory to store a file
75
76   string aTmpDir;
77
78   char *Tmp_dir = getenv("SALOME_TMP_DIR");
79   if(Tmp_dir != NULL) {
80     aTmpDir = string(Tmp_dir);
81 #ifdef WIN32
82     if(aTmpDir[aTmpDir.size()-1] != '\\') aTmpDir+='\\';
83 #else
84     if(aTmpDir[aTmpDir.size()-1] != '/') aTmpDir+='/';
85 #endif      
86   }
87   else {
88 #ifdef WIN32
89     aTmpDir = string("C:\\");
90 #else
91     aTmpDir = string("/tmp/");
92 #endif
93   }
94
95   srand((unsigned int)time(NULL));
96   int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
97   char buffer[127];
98   sprintf(buffer, "%d", aRND);
99   string aSubDir(buffer);
100   if(aSubDir.size() <= 1) aSubDir = string("123409876");
101
102   aTmpDir += aSubDir; //Get RND sub directory
103
104   string aDir = aTmpDir;
105   
106   if(Exists(aDir)) {
107     for(aRND = 0; Exists(aDir); aRND++) {
108       sprintf(buffer, "%d", aRND);
109       aDir = aTmpDir+buffer;  //Build a unique directory name
110     }
111   }
112
113 #ifdef WIN32
114   if(aDir[aTmpDir.size()-1] != '\\') aDir+='\\';
115 #else
116   if(aDir[aTmpDir.size()-1] != '/') aDir+='/';
117 #endif
118
119
120 #ifdef WNT
121   CreateDirectory(aDir.c_str(), NULL);
122 #else
123   mkdir(aDir.c_str(), 0x1ff); 
124 #endif
125
126   return aDir;
127 }
128
129 //============================================================================
130 // function : RemoveTemporaryFiles
131 // purpose  : Removes files listed in theFileList
132 //============================================================================
133 void SALOMEDSImpl_Tool::RemoveTemporaryFiles(const string& theDirectory, 
134                                              const vector<string>& theFiles,
135                                              const bool IsDirDeleted)
136 {
137   string aDirName = theDirectory;
138
139   int i, aLength = theFiles.size();
140   for(i=1; i<=aLength; i++) {
141     string aFile(aDirName);
142     aFile += theFiles[i-1];
143     if(!Exists(aFile)) continue;
144
145 #ifdef WNT
146     DeleteFile(aFile.c_str());
147 #else 
148     unlink(aFile.c_str());
149 #endif
150   }
151
152   if(IsDirDeleted) {
153     if(Exists(aDirName)) {
154 #ifdef WNT
155       RemoveDirectory(aDireName.c_str());
156 #else
157       rmdir(aDirName.c_str());
158 #endif
159     }
160   }
161
162 }
163
164 //============================================================================
165 // function : GetNameFromPath
166 // purpose  : Returns the name by the path
167 //============================================================================
168 string SALOMEDSImpl_Tool::GetNameFromPath(const string& thePath) {
169   if (thePath.empty()) return "";
170   int pos = thePath.rfind('/');
171   if(pos > 0) return thePath.substr(pos+1, thePath.size());
172   pos = thePath.rfind('\\'); 
173   if(pos > 0) return thePath.substr(pos+1, thePath.size()); 
174   pos = thePath.rfind('|');
175   if(pos > 0) return thePath.substr(pos+1, thePath.size()); 
176   return thePath;
177 }
178
179 //============================================================================
180 // function : GetDirFromPath
181 // purpose  : Returns the dir by the path
182 //============================================================================
183 string SALOMEDSImpl_Tool::GetDirFromPath(const string& thePath) {
184   if (thePath.empty()) return "";
185
186   int pos = thePath.rfind('/');
187   string path;
188   if(pos > 0) {
189     path = thePath.substr(0, pos+1);
190   }
191   if(path.empty()) {
192     pos = thePath.rfind('\\');
193     if(pos > 0) path = thePath.substr(0, pos+1); 
194   }
195   if(path.empty()) {
196     pos = thePath.rfind('|');
197     if(pos > 0) path = thePath.substr(0, pos+1); 
198   }
199   if(path.empty()) {
200     path = thePath+"/";
201   }
202   
203 #ifdef WNT  //Check if the only disk letter is given as path
204   if(path.size() == 2 && path[1] == ":") path +='\\';
205 #endif
206
207   for(int i = 0, len = path.size(); i<len; i++) 
208     if(path[i] == '|') path[i] = '/';
209   return path;
210 }
211
212 //============================================================================
213 // function : 
214 // purpose  : The functions returns a list of substring of initial string 
215 //            divided by given separator
216 //============================================================================
217 vector<string> SALOMEDSImpl_Tool::splitString(const string& theValue, char separator)
218 {
219   vector<string> vs;
220   if(theValue[0] == separator && theValue.size() == 1) return vs;
221   int pos = theValue.find(separator);
222   if(pos < 0) {
223     vs.push_back(theValue);
224     return vs;
225   }
226  
227   string s = theValue;
228   if(s[0] == separator) s = s.substr(1, s.size());
229   while((pos = s.find(separator)) >= 0) {
230     vs.push_back(s.substr(0, pos));
231     s = s.substr(pos+1, s.size());
232   }
233                
234   if(!s.empty() && s[0] != separator) vs.push_back(s);
235   return vs;
236 }
237
238
239 void SALOMEDSImpl_Tool::GetSystemDate(int& year, int& month, int& day, int& hours, int& minutes, int& seconds)
240 {
241 #ifdef WNT
242   SYSTEMTIME    st;
243
244   GetLocalTime ( &st );
245
246   month = st.wMonth;
247   day = st.wDay;
248   year = st.wYear;
249   hours = st.wHour;
250   minutes = st.wMinute;
251   seconds = st.wSecond;
252 #else
253   struct tm transfert;
254   struct timeval tval;
255   struct timezone tzone;
256   int status;
257
258  status = gettimeofday( &tval, &tzone );
259  memcpy(&transfert, localtime((time_t *)&tval.tv_sec), sizeof(tm));
260
261  month    = transfert.tm_mon + 1;
262  day      = transfert.tm_mday;
263  year     = transfert.tm_year + 1900;
264  hours    = transfert.tm_hour;
265  minutes  = transfert.tm_min ;
266  seconds  = transfert.tm_sec ;
267 #endif
268 }
269
270 string SALOMEDSImpl_Tool::GetUserName()
271 {
272 #ifdef WNT
273   char*  pBuff = new char[UNLEN + 1];
274   DWORD  dwSize = UNLEN + 1;
275   string retVal;
276   GetUserName ( pBuff, &dwSize );
277   string theTmpUserName(pBuff,(int)dwSize -1 );
278   retVal = theTmpUserName;
279   delete [] pBuff;
280   return retVal;
281 #else
282  struct passwd *infos;
283  infos = getpwuid(getuid()); 
284  return string(infos->pw_name);
285 #endif
286 }
287
288
289
290
291
292