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>
50 bool Exists(const string thePath)
53 if ( GetFileAttributes ( thePath.c_str() ) == 0xFFFFFFFF ) {
54 if ( GetLastError () != ERROR_FILE_NOT_FOUND ) {
59 int status = access ( thePath.c_str() , F_OK );
60 if (status != 0) return false;
68 //============================================================================
69 // function : GetTempDir
70 // purpose : Return a temp directory to store created files like "/tmp/sub_dir/"
71 //============================================================================
72 string SALOMEDSImpl_Tool::GetTmpDir()
74 //Find a temporary directory to store a file
78 char *Tmp_dir = getenv("SALOME_TMP_DIR");
80 aTmpDir = string(Tmp_dir);
82 if(aTmpDir[aTmpDir.size()-1] != '\\') aTmpDir+='\\';
84 if(aTmpDir[aTmpDir.size()-1] != '/') aTmpDir+='/';
89 aTmpDir = string("C:\\");
91 aTmpDir = string("/tmp/");
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
98 sprintf(buffer, "%d", aRND);
99 string aSubDir(buffer);
100 if(aSubDir.size() <= 1) aSubDir = string("123409876");
102 aTmpDir += aSubDir; //Get RND sub directory
104 string aDir = aTmpDir;
107 for(aRND = 0; Exists(aDir); aRND++) {
108 sprintf(buffer, "%d", aRND);
109 aDir = aTmpDir+buffer; //Build a unique directory name
114 if(aDir[aTmpDir.size()-1] != '\\') aDir+='\\';
116 if(aDir[aTmpDir.size()-1] != '/') aDir+='/';
121 CreateDirectory(aDir.c_str(), NULL);
123 mkdir(aDir.c_str(), 0x1ff);
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)
137 string aDirName = theDirectory;
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;
146 DeleteFile(aFile.c_str());
148 unlink(aFile.c_str());
153 if(Exists(aDirName)) {
155 RemoveDirectory(aDireName.c_str());
157 rmdir(aDirName.c_str());
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());
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 "";
186 int pos = thePath.rfind('/');
189 path = thePath.substr(0, pos+1);
192 pos = thePath.rfind('\\');
193 if(pos > 0) path = thePath.substr(0, pos+1);
196 pos = thePath.rfind('|');
197 if(pos > 0) path = thePath.substr(0, pos+1);
203 #ifdef WNT //Check if the only disk letter is given as path
204 if(path.size() == 2 && path[1] == ":") path +='\\';
207 for(int i = 0, len = path.size(); i<len; i++)
208 if(path[i] == '|') path[i] = '/';
212 //============================================================================
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)
220 if(theValue[0] == separator && theValue.size() == 1) return vs;
221 int pos = theValue.find(separator);
223 vs.push_back(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());
234 if(!s.empty() && s[0] != separator) vs.push_back(s);
239 void SALOMEDSImpl_Tool::GetSystemDate(int& year, int& month, int& day, int& hours, int& minutes, int& seconds)
244 GetLocalTime ( &st );
250 minutes = st.wMinute;
251 seconds = st.wSecond;
255 struct timezone tzone;
258 status = gettimeofday( &tval, &tzone );
259 memcpy(&transfert, localtime((time_t *)&tval.tv_sec), sizeof(tm));
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 ;
270 string SALOMEDSImpl_Tool::GetUserName()
273 char* pBuff = new char[UNLEN + 1];
274 DWORD dwSize = UNLEN + 1;
276 GetUserName ( pBuff, &dwSize );
277 string theTmpUserName(pBuff,(int)dwSize -1 );
278 retVal = theTmpUserName;
282 struct passwd *infos;
283 infos = getpwuid(getuid());
284 return string(infos->pw_name);