1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : SALOMEDSImpl_Tool.cxx
21 // Created : Mon Oct 21 16:24:34 2002
22 // Author : Sergey RUIN
25 // Module : SALOMEDSImpl
27 #include "SALOMEDSImpl_Tool.hxx"
37 #include <sys/types.h>
51 bool Exists(const 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 string SALOMEDSImpl_Tool::GetTmpDir()
75 //Find a temporary directory to store a file
79 char *Tmp_dir = getenv("SALOME_TMP_DIR");
81 aTmpDir = string(Tmp_dir);
83 if(aTmpDir[aTmpDir.size()-1] != '\\') aTmpDir+='\\';
85 if(aTmpDir[aTmpDir.size()-1] != '/') aTmpDir+='/';
90 aTmpDir = string("C:\\");
92 aTmpDir = 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 string aSubDir(buffer);
101 if(aSubDir.size() <= 1) aSubDir = string("123409876");
103 aTmpDir += aSubDir; //Get RND sub directory
105 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 string& theDirectory,
135 const vector<string>& theFiles,
136 const bool IsDirDeleted)
138 string aDirName = theDirectory;
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;
147 DeleteFile(aFile.c_str());
149 unlink(aFile.c_str());
154 if(Exists(aDirName)) {
156 RemoveDirectory(aDireName.c_str());
158 rmdir(aDirName.c_str());
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());
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 "";
187 int pos = thePath.rfind('/');
190 path = thePath.substr(0, pos+1);
193 pos = thePath.rfind('\\');
194 if(pos > 0) path = thePath.substr(0, pos+1);
197 pos = thePath.rfind('|');
198 if(pos > 0) path = thePath.substr(0, pos+1);
204 #ifdef WNT //Check if the only disk letter is given as path
205 if(path.size() == 2 && path[1] == ":") path +='\\';
208 for(int i = 0, len = path.size(); i<len; i++)
209 if(path[i] == '|') path[i] = '/';
213 //============================================================================
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)
221 if(theValue[0] == separator && theValue.size() == 1) return vs;
222 int pos = theValue.find(separator);
224 vs.push_back(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);
240 void SALOMEDSImpl_Tool::GetSystemDate(int& year, int& month, int& day, int& hours, int& minutes, int& seconds)
245 GetLocalTime ( &st );
251 minutes = st.wMinute;
252 seconds = st.wSecond;
256 struct timezone tzone;
259 status = gettimeofday( &tval, &tzone );
260 memcpy(&transfert, localtime((time_t *)&tval.tv_sec), sizeof(tm));
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 ;
271 string SALOMEDSImpl_Tool::GetUserName()
274 char* pBuff = new char[UNLEN + 1];
275 DWORD dwSize = UNLEN + 1;
277 GetUserName ( pBuff, &dwSize );
278 string theTmpUserName(pBuff,(int)dwSize -1 );
279 retVal = theTmpUserName;
283 struct passwd *infos;
284 infos = getpwuid(getuid());
285 return string(infos->pw_name);