1 // Copyright (C) 2007-2019 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, or (at your option) any later version.
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
23 // File : SALOMEDSImpl_Tool.cxx
24 // Created : Mon Oct 21 16:24:34 2002
25 // Author : Sergey RUIN
27 // Module : SALOMEDSImpl
37 #include "Basics_DirUtils.hxx"
38 #include "Basics_Utils.hxx"
40 #include "SALOMEDSImpl_Tool.hxx"
45 #include <sys/types.h>
51 //#include <winbase.h>
56 bool SALOMEDS_Exists(const std::string thePath)
58 return Kernel_Utils::IsExists( thePath );
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()
67 return Kernel_Utils::GetTmpDirByEnv("SALOME_TMP_DIR");
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)
78 std::string aDirName = theDirectory;
80 int 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;
88 std::wstring aFileToDelete = Kernel_Utils::utf8_decode_s(aFile);
90 std::string aFileToDelete = aFile;
92 DeleteFile( aFileToDelete.c_str() );
94 unlink(aFile.c_str());
99 if(SALOMEDS_Exists(aDirName)) {
102 std::wstring aDirToDelete = Kernel_Utils::utf8_decode_s(aDirName);
104 std::string aDirToDelete = aDirName;
106 RemoveDirectory(aDirToDelete.c_str());
108 rmdir(aDirName.c_str());
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 = thePath.rfind('/');
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());
130 //============================================================================
131 // function : GetDirFromPath
132 // purpose : Returns the dir by the path
133 //============================================================================
134 std::string SALOMEDSImpl_Tool::GetDirFromPath(const std::string& thePath) {
136 std::string separator = "\\";
138 std::string separator = "/";
142 if (!thePath.empty()) {
143 int pos = thePath.rfind('/');
144 if (pos < 0) pos = thePath.rfind('\\');
145 if (pos < 0) pos = thePath.rfind('|');
148 path = thePath.substr(0, pos+1);
150 path = std::string(".") + separator;
152 #ifdef WIN32 //Check if the only disk letter is given as path
153 if (path.size() == 2 && path[1] == ':') path += separator;
156 while ( (pos=path.find('|')) >= 0 )
157 path.replace(pos, 1, separator);
163 //============================================================================
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)
170 std::vector<std::string> vs;
171 if(theValue[0] == separator && theValue.size() == 1) return vs;
172 int pos = theValue.find(separator);
174 vs.push_back(theValue);
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());
185 if(!s.empty() && s[0] != separator) vs.push_back(s);
189 //============================================================================
191 // purpose : The functions returns a list of substring of initial string
192 // divided by given separator include empty strings
193 //============================================================================
195 std::vector<std::string> treatRepetation(const std::string& theValue);
197 std::vector<std::string> treatRepetation(const std::string& theValue)
199 std::vector<std::string> aResult;
200 int pos = theValue.find(";*=");
203 aResult.push_back(theValue);
206 std::string val(theValue.substr(0, pos));
207 std::string suffix(theValue.substr(pos+3));
209 std::istringstream tmp(suffix);
211 for(int i=0; i<nb; i++)
212 aResult.push_back(val);
216 std::vector<std::string> SALOMEDSImpl_Tool::splitStringWithEmpty(const std::string& theValue, char sep)
218 std::vector<std::string> aResult;
219 if(theValue[0] == sep ) aResult.push_back(std::string());
220 int pos = theValue.find(sep);
224 std::vector<std::string> tmp = treatRepetation(theValue);
225 std::copy(tmp.begin(), tmp.end(), std::back_insert_iterator< std::vector<std::string> >(aResult));
228 aResult.push_back(theValue);
232 std::string s = theValue;
233 if(s[0] == sep) s = s.substr(1, s.size());
234 while((pos = s.find(sep)) >= 0) {
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));
241 aResult.push_back(s.substr(0, pos));
242 s = s.substr(pos+1, s.size());
245 if(!s.empty() && s[0] != sep) {
248 std::vector<std::string> tmp = treatRepetation(s);
249 std::copy(tmp.begin(), tmp.end(), std::back_insert_iterator< std::vector<std::string> >(aResult));
252 aResult.push_back(s);
254 if(theValue[theValue.size()-1] == sep) aResult.push_back(std::string());
259 //============================================================================
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)
266 std::vector< std::vector<std::string> > aResult;
267 if(theValue.size() > 0) {
268 std::vector<std::string> aSections = splitStringWithEmpty( theValue, sep1 );
269 for( int i = 0, n = aSections.size(); i < n; i++ )
270 aResult.push_back( splitStringWithEmpty( aSections[i], sep2 ) );
276 void SALOMEDSImpl_Tool::GetSystemDate(int& year, int& month, int& day, int& hours, int& minutes, int& seconds)
281 GetLocalTime ( &st );
287 minutes = st.wMinute;
288 seconds = st.wSecond;
292 struct timezone tzone;
295 /*status = */ gettimeofday( &tval, &tzone );
296 memcpy(&transfert, localtime((time_t *)&tval.tv_sec), sizeof(tm));
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 ;
307 //Warning undef of Ascii Winwows define
311 std::string SALOMEDSImpl_Tool::GetUserName()
314 char* pBuff = new char[UNLEN + 1];
315 DWORD dwSize = UNLEN + 1;
317 ::GetUserNameA( pBuff, &dwSize );
318 std::string theTmpUserName(pBuff,(int)dwSize -1 );
319 retVal = theTmpUserName;
323 struct passwd *infos;
324 infos = getpwuid(getuid());
325 return std::string(infos->pw_name);