Salome HOME
updated copyright message
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_Tool.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, 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, or (at your option) any later version.
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 #include <iterator>
35 #include <sstream>
36
37 #include "Basics_DirUtils.hxx"
38 #include "Basics_Utils.hxx"
39
40 #include "SALOMEDSImpl_Tool.hxx"
41
42 #ifndef WIN32
43 #include <sys/time.h>
44 #include <sys/stat.h>
45 #include <sys/types.h>
46 #include <pwd.h> 
47 #include <unistd.h>
48 #else
49 #include <time.h>
50 #include <lmcons.h>
51 //#include <winbase.h>
52 #include <windows.h>
53 #endif
54
55
56 bool SALOMEDS_Exists(const std::string thePath)
57 {
58         return Kernel_Utils::IsExists( thePath );
59 }
60
61 //============================================================================
62 // function : GetTempDir
63 // purpose  : Return a temp directory to store created files like "/tmp/sub_dir/"
64 //============================================================================
65 std::string SALOMEDSImpl_Tool::GetTmpDir()
66 {
67   return Kernel_Utils::GetTmpDirByEnv("SALOME_TMP_DIR");
68 }
69
70 //============================================================================
71 // function : RemoveTemporaryFiles
72 // purpose  : Removes files listed in theFileList
73 //============================================================================
74 void SALOMEDSImpl_Tool::RemoveTemporaryFiles(const std::string& theDirectory,
75                                              const std::vector<std::string>& theFiles,
76                                              const bool IsDirDeleted)
77 {
78   std::string aDirName = theDirectory;
79
80   size_t i, aLength = theFiles.size(); 
81   for(i=1; i<=aLength; i++) {
82     std::string aFile(aDirName);
83     aFile += theFiles[i-1];
84     if(!SALOMEDS_Exists(aFile)) continue;
85
86 #ifdef WIN32
87 #if defined(UNICODE)
88         std::wstring aFileToDelete = Kernel_Utils::utf8_decode_s(aFile);
89 #else
90         std::string aFileToDelete = aFile;
91 #endif
92     DeleteFile( aFileToDelete.c_str() );
93 #else 
94     unlink(aFile.c_str());
95 #endif
96   }
97
98   if(IsDirDeleted) {
99     if(SALOMEDS_Exists(aDirName)) {
100 #ifdef WIN32
101 #if defined(UNICODE)
102                 std::wstring aDirToDelete = Kernel_Utils::utf8_decode_s(aDirName);
103 #else
104                 std::string aDirToDelete = aDirName;
105 #endif
106       RemoveDirectory(aDirToDelete.c_str());
107 #else
108       rmdir(aDirName.c_str());
109 #endif
110     }
111   }
112
113 }
114
115 //============================================================================
116 // function : GetNameFromPath
117 // purpose  : Returns the name by the path
118 //============================================================================
119 std::string SALOMEDSImpl_Tool::GetNameFromPath(const std::string& thePath) {
120   if (thePath.empty()) return "";
121   int pos = (int)thePath.rfind('/'); //TODO: conversion from size_t to int
122   if(pos >= 0) return thePath.substr(pos+1, thePath.size());
123   pos = thePath.rfind('\\'); 
124   if(pos >= 0) return thePath.substr(pos+1, thePath.size()); 
125   pos = thePath.rfind('|');
126   if(pos >= 0) return thePath.substr(pos+1, thePath.size()); 
127   return thePath;
128 }
129
130 //============================================================================
131 // function : GetDirFromPath
132 // purpose  : Returns the dir by the path
133 //============================================================================
134 std::string SALOMEDSImpl_Tool::GetDirFromPath(const std::string& thePath) {
135 #ifdef WIN32
136   std::string separator = "\\";
137 #else
138   std::string separator = "/";
139 #endif
140
141   std::string path;
142   if (!thePath.empty()) {
143     int pos = (int)thePath.rfind('/'); //TODO: conversion from size_t to int
144     if (pos < 0) pos = thePath.rfind('\\');
145     if (pos < 0) pos = thePath.rfind('|');
146     
147     if (pos >= 0)
148       path = thePath.substr(0, pos+1);
149     else
150       path = std::string(".") + separator;
151
152 #ifdef WIN32  //Check if the only disk letter is given as path
153     if (path.size() == 2 && path[1] == ':') path += separator;
154 #endif
155     
156     while ( (pos=path.find('|')) >= 0 )
157       path.replace(pos, 1, separator);
158   }
159   
160   return path;
161 }
162
163 //============================================================================
164 // function : 
165 // purpose  : The functions returns a list of substring of initial string 
166 //            divided by given separator
167 //============================================================================
168 std::vector<std::string> SALOMEDSImpl_Tool::splitString(const std::string& theValue, char separator)
169 {
170   std::vector<std::string> vs;
171   if(theValue[0] == separator && theValue.size() == 1) return vs;
172   int pos = (int)theValue.find(separator); //TODO: conversion from size_t to int
173   if(pos < 0) {
174     vs.push_back(theValue);
175     return vs;
176   }
177  
178   std::string s = theValue;
179   if(s[0] == separator) s = s.substr(1, s.size());
180   while((pos = s.find(separator)) >= 0) {
181     vs.push_back(s.substr(0, pos));
182     s = s.substr(pos+1, s.size());
183   }
184                
185   if(!s.empty() && s[0] != separator) vs.push_back(s);
186   return vs;
187 }
188
189 //============================================================================
190 // function : 
191 // purpose  : The functions returns a list of substring of initial string 
192 //            divided by given separator include empty strings
193 //============================================================================
194
195 std::vector<std::string> treatRepetation(const std::string& theValue);
196
197 std::vector<std::string> treatRepetation(const std::string& theValue)
198 {
199   std::vector<std::string> aResult;
200   int pos = (int)theValue.find(";*="); //TODO: conversion from size_t to int
201   if(pos < 0 )
202     {
203       aResult.push_back(theValue);
204       return aResult;
205     }
206   std::string val(theValue.substr(0, pos));
207   std::string suffix(theValue.substr(pos+3));
208   int nb;
209   std::istringstream tmp(suffix);
210   tmp >> nb;
211   for(int i=0; i<nb; i++)
212     aResult.push_back(val);
213   return aResult;
214 }
215
216 std::vector<std::string> SALOMEDSImpl_Tool::splitStringWithEmpty(const std::string& theValue, char sep)
217 {
218   std::vector<std::string> aResult;
219   if(theValue[0] == sep ) aResult.push_back(std::string());
220   int pos = (int)theValue.find(sep); //TODO: conversion from size_t to int
221   if(pos < 0 ) {
222     if(sep == '|')
223       {
224         std::vector<std::string> tmp = treatRepetation(theValue);
225         std::copy(tmp.begin(), tmp.end(), std::back_insert_iterator< std::vector<std::string> >(aResult));
226       }
227     else
228       aResult.push_back(theValue);
229     return aResult;
230   }
231
232   std::string s = theValue;
233   if(s[0] == sep) s = s.substr(1, s.size());
234   while((pos = s.find(sep)) >= 0) {
235     if(sep == '|')
236       {
237         std::vector<std::string> tmp = treatRepetation(s.substr(0, pos));
238         std::copy(tmp.begin(), tmp.end(), std::back_insert_iterator< std::vector<std::string> >(aResult));
239       }
240     else
241       aResult.push_back(s.substr(0, pos));
242     s = s.substr(pos+1, s.size());
243   }
244
245   if(!s.empty() && s[0] != sep) {
246     if(sep == '|')
247       {
248         std::vector<std::string> tmp = treatRepetation(s);
249         std::copy(tmp.begin(), tmp.end(), std::back_insert_iterator< std::vector<std::string> >(aResult));
250       }
251     else
252       aResult.push_back(s);
253   }
254   if(theValue[theValue.size()-1] == sep) aResult.push_back(std::string());
255
256   return aResult;
257 }
258
259 //============================================================================
260 // function : 
261 // purpose  : The functions returns a list of lists of substrings of initial string 
262 //            divided by two given separators include empty strings
263 //============================================================================
264 std::vector< std::vector<std::string> > SALOMEDSImpl_Tool::splitStringWithEmpty(const std::string& theValue, char sep1, char sep2)
265 {
266   std::vector< std::vector<std::string> > aResult;
267   if(theValue.size() > 0) {
268     std::vector<std::string> aSections = splitStringWithEmpty( theValue, sep1 );
269     for( size_t i = 0, n = aSections.size(); i < n; i++ )
270       aResult.push_back( splitStringWithEmpty( aSections[i], sep2 ) );
271   }
272   return aResult;
273 }
274
275
276 void SALOMEDSImpl_Tool::GetSystemDate(int& year, int& month, int& day, int& hours, int& minutes, int& seconds)
277 {
278 #ifdef WIN32
279   SYSTEMTIME    st;
280
281   GetLocalTime ( &st );
282
283   month = st.wMonth;
284   day = st.wDay;
285   year = st.wYear;
286   hours = st.wHour;
287   minutes = st.wMinute;
288   seconds = st.wSecond;
289 #else
290   struct tm transfert;
291   struct timeval tval;
292   struct timezone tzone;
293   //int status;
294
295   /*status = */ gettimeofday( &tval, &tzone );
296   memcpy(&transfert, localtime((time_t *)&tval.tv_sec), sizeof(tm));
297
298   month    = transfert.tm_mon + 1;
299   day      = transfert.tm_mday;
300   year     = transfert.tm_year + 1900;
301   hours    = transfert.tm_hour;
302   minutes  = transfert.tm_min ;
303   seconds  = transfert.tm_sec ;
304 #endif
305 }
306
307 //Warning undef of Ascii Winwows define
308 #ifdef WIN32
309 # undef GetUserName
310 #endif
311 std::string SALOMEDSImpl_Tool::GetUserName()
312 {
313 #ifdef WIN32
314   char*  pBuff = new char[UNLEN + 1];
315   DWORD  dwSize = UNLEN + 1;
316   std::string retVal;
317   ::GetUserNameA( pBuff, &dwSize );
318   std::string theTmpUserName(pBuff,(int)dwSize -1 );
319   retVal = theTmpUserName;
320   delete [] pBuff;
321   return retVal;
322 #else
323  struct passwd *infos;
324  infos = getpwuid(getuid()); 
325  return std::string(infos->pw_name);
326 #endif
327 }
328