1 // Copyright (C) 2007-2010 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.
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
35 #include "SALOMEDSImpl_Tool.hxx"
40 #include <sys/types.h>
46 //#include <winbase.h>
51 bool Exists(const std::string thePath)
54 if ( GetFileAttributes ( thePath.c_str() ) == 0xFFFFFFFF ) {
55 if ( GetLastError () == ERROR_FILE_NOT_FOUND ) {
60 int status = access ( thePath.c_str() , F_OK );
61 if (status != 0) return false;
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()
75 //Find a temporary directory to store a file
79 char *Tmp_dir = getenv("SALOME_TMP_DIR");
81 aTmpDir = std::string(Tmp_dir);
83 if(aTmpDir[aTmpDir.size()-1] != '\\') aTmpDir+='\\';
85 if(aTmpDir[aTmpDir.size()-1] != '/') aTmpDir+='/';
90 aTmpDir = std::string("C:\\");
92 aTmpDir = std::string("/tmp/");
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
99 sprintf(buffer, "%d", aRND);
100 std::string aSubDir(buffer);
101 if(aSubDir.size() <= 1) aSubDir = std::string("123409876");
103 aTmpDir += aSubDir; //Get RND sub directory
105 std::string aDir = aTmpDir;
108 for(aRND = 0; Exists(aDir); aRND++) {
109 sprintf(buffer, "%d", aRND);
110 aDir = aTmpDir+buffer; //Build a unique directory name
115 if(aDir[aTmpDir.size()-1] != '\\') aDir+='\\';
117 if(aDir[aTmpDir.size()-1] != '/') aDir+='/';
122 CreateDirectory(aDir.c_str(), NULL);
124 mkdir(aDir.c_str(), 0x1ff);
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)
138 std::string aDirName = theDirectory;
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;
147 DeleteFile(aFile.c_str());
149 unlink(aFile.c_str());
154 if(Exists(aDirName)) {
156 RemoveDirectory(aDirName.c_str());
158 rmdir(aDirName.c_str());
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());
180 //============================================================================
181 // function : GetDirFromPath
182 // purpose : Returns the dir by the path
183 //============================================================================
184 std::string SALOMEDSImpl_Tool::GetDirFromPath(const std::string& thePath) {
186 std::string separator = "\\";
188 std::string separator = "/";
192 if (!thePath.empty()) {
193 int pos = thePath.rfind('/');
194 if (pos < 0) pos = thePath.rfind('\\');
195 if (pos < 0) pos = thePath.rfind('|');
198 path = thePath.substr(0, pos+1);
200 path = std::string(".") + separator;
202 #ifdef WIN32 //Check if the only disk letter is given as path
203 if (path.size() == 2 && path[1] == ':') path += separator;
206 while ( (pos=path.find('|')) >= 0 )
207 path.replace(pos, 1, separator);
213 //============================================================================
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)
220 std::vector<std::string> vs;
221 if(theValue[0] == separator && theValue.size() == 1) return vs;
222 int pos = theValue.find(separator);
224 vs.push_back(theValue);
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());
235 if(!s.empty() && s[0] != separator) vs.push_back(s);
239 //============================================================================
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)
246 std::vector<std::string> aResult;
247 if(theValue[0] == sep ) aResult.push_back(std::string());
248 int pos = theValue.find(sep);
250 aResult.push_back(theValue);
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());
261 if(!s.empty() && s[0] != sep) aResult.push_back(s);
262 if(theValue[theValue.size()-1] == sep) aResult.push_back(std::string());
267 //============================================================================
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)
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 ) );
284 void SALOMEDSImpl_Tool::GetSystemDate(int& year, int& month, int& day, int& hours, int& minutes, int& seconds)
289 GetLocalTime ( &st );
295 minutes = st.wMinute;
296 seconds = st.wSecond;
300 struct timezone tzone;
303 status = gettimeofday( &tval, &tzone );
304 memcpy(&transfert, localtime((time_t *)&tval.tv_sec), sizeof(tm));
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 ;
315 //Warning undef of Ascii Winwows define
319 std::string SALOMEDSImpl_Tool::GetUserName()
322 char* pBuff = new char[UNLEN + 1];
323 DWORD dwSize = UNLEN + 1;
325 ::GetUserNameA( pBuff, &dwSize );
326 std::string theTmpUserName(pBuff,(int)dwSize -1 );
327 retVal = theTmpUserName;
331 struct passwd *infos;
332 infos = getpwuid(getuid());
333 return std::string(infos->pw_name);