Back to index

salome-kernel  6.5.0
SALOMEDSImpl_Tool.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  File      : SALOMEDSImpl_Tool.cxx
00024 //  Created   : Mon Oct 21 16:24:34 2002
00025 //  Author    : Sergey RUIN
00026 //  Project   : SALOME
00027 //  Module    : SALOMEDSImpl
00028 //
00029 #include <stdio.h>
00030 #include <iostream> 
00031 #include <fstream>
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <iterator>
00035 #include <sstream>
00036 
00037 #include "SALOMEDSImpl_Tool.hxx"
00038 
00039 #ifndef WIN32
00040 #include <sys/time.h>
00041 #include <sys/stat.h>
00042 #include <sys/types.h>
00043 #include <pwd.h> 
00044 #include <unistd.h>
00045 #else
00046 #include <time.h>
00047 #include <lmcons.h>
00048 //#include <winbase.h>
00049 #include <windows.h>
00050 #endif
00051 
00052 
00053 bool Exists(const std::string thePath) 
00054 {
00055 #ifdef WIN32 
00056   if (  GetFileAttributes (  thePath.c_str()  ) == 0xFFFFFFFF  ) { 
00057     if (  GetLastError () == ERROR_FILE_NOT_FOUND  ) {
00058       return false;
00059     }
00060   }
00061 #else 
00062   int status = access ( thePath.c_str() , F_OK ); 
00063   if (status != 0) return false;
00064 #endif
00065   return true;
00066 }
00067 
00068 
00069 
00070 
00071 //============================================================================
00072 // function : GetTempDir
00073 // purpose  : Return a temp directory to store created files like "/tmp/sub_dir/" 
00074 //============================================================================ 
00075 std::string SALOMEDSImpl_Tool::GetTmpDir()
00076 {
00077   //Find a temporary directory to store a file
00078 
00079   std::string aTmpDir;
00080 
00081   char *Tmp_dir = getenv("SALOME_TMP_DIR");
00082   if(Tmp_dir != NULL) {
00083     aTmpDir = std::string(Tmp_dir);
00084 #ifdef WIN32
00085     if(aTmpDir[aTmpDir.size()-1] != '\\') aTmpDir+='\\';
00086 #else
00087     if(aTmpDir[aTmpDir.size()-1] != '/') aTmpDir+='/';
00088 #endif      
00089   }
00090   else {
00091 #ifdef WIN32
00092     aTmpDir = std::string("C:\\");
00093 #else
00094     aTmpDir = std::string("/tmp/");
00095 #endif
00096   }
00097 
00098   srand((unsigned int)time(NULL));
00099   int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
00100   char buffer[127];
00101   sprintf(buffer, "%d", aRND);
00102   std::string aSubDir(buffer);
00103   if(aSubDir.size() <= 1) aSubDir = std::string("123409876");
00104 
00105   aTmpDir += aSubDir; //Get RND sub directory
00106 
00107   std::string aDir = aTmpDir;
00108   
00109   if(Exists(aDir)) {
00110     for(aRND = 0; Exists(aDir); aRND++) {
00111       sprintf(buffer, "%d", aRND);
00112       aDir = aTmpDir+buffer;  //Build a unique directory name
00113     }
00114   }
00115 
00116 #ifdef WIN32
00117   if(aDir[aTmpDir.size()-1] != '\\') aDir+='\\';
00118 #else
00119   if(aDir[aTmpDir.size()-1] != '/') aDir+='/';
00120 #endif
00121 
00122 
00123 #ifdef WIN32
00124   CreateDirectory(aDir.c_str(), NULL);
00125 #else
00126   mkdir(aDir.c_str(), 0x1ff); 
00127 #endif
00128 
00129   return aDir;
00130 }
00131 
00132 //============================================================================
00133 // function : RemoveTemporaryFiles
00134 // purpose  : Removes files listed in theFileList
00135 //============================================================================
00136 void SALOMEDSImpl_Tool::RemoveTemporaryFiles(const std::string& theDirectory, 
00137                                              const std::vector<std::string>& theFiles,
00138                                              const bool IsDirDeleted)
00139 {
00140   std::string aDirName = theDirectory;
00141 
00142   int i, aLength = theFiles.size();
00143   for(i=1; i<=aLength; i++) {
00144     std::string aFile(aDirName);
00145     aFile += theFiles[i-1];
00146     if(!Exists(aFile)) continue;
00147 
00148 #ifdef WIN32
00149     DeleteFile(aFile.c_str());
00150 #else 
00151     unlink(aFile.c_str());
00152 #endif
00153   }
00154 
00155   if(IsDirDeleted) {
00156     if(Exists(aDirName)) {
00157 #ifdef WIN32
00158       RemoveDirectory(aDirName.c_str());
00159 #else
00160       rmdir(aDirName.c_str());
00161 #endif
00162     }
00163   }
00164 
00165 }
00166 
00167 //============================================================================
00168 // function : GetNameFromPath
00169 // purpose  : Returns the name by the path
00170 //============================================================================
00171 std::string SALOMEDSImpl_Tool::GetNameFromPath(const std::string& thePath) {
00172   if (thePath.empty()) return "";
00173   int pos = thePath.rfind('/');
00174   if(pos >= 0) return thePath.substr(pos+1, thePath.size());
00175   pos = thePath.rfind('\\'); 
00176   if(pos >= 0) return thePath.substr(pos+1, thePath.size()); 
00177   pos = thePath.rfind('|');
00178   if(pos >= 0) return thePath.substr(pos+1, thePath.size()); 
00179   return thePath;
00180 }
00181 
00182 //============================================================================
00183 // function : GetDirFromPath
00184 // purpose  : Returns the dir by the path
00185 //============================================================================
00186 std::string SALOMEDSImpl_Tool::GetDirFromPath(const std::string& thePath) {
00187 #ifdef WIN32
00188   std::string separator = "\\";
00189 #else
00190   std::string separator = "/";
00191 #endif
00192 
00193   std::string path;
00194   if (!thePath.empty()) {
00195     int pos = thePath.rfind('/');
00196     if (pos < 0) pos = thePath.rfind('\\');
00197     if (pos < 0) pos = thePath.rfind('|');
00198     
00199     if (pos >= 0)
00200       path = thePath.substr(0, pos+1);
00201     else
00202       path = std::string(".") + separator;
00203 
00204 #ifdef WIN32  //Check if the only disk letter is given as path
00205     if (path.size() == 2 && path[1] == ':') path += separator;
00206 #endif
00207     
00208     while ( (pos=path.find('|')) >= 0 )
00209       path.replace(pos, 1, separator);
00210   }
00211   
00212   return path;
00213 }
00214 
00215 //============================================================================
00216 // function : 
00217 // purpose  : The functions returns a list of substring of initial string 
00218 //            divided by given separator
00219 //============================================================================
00220 std::vector<std::string> SALOMEDSImpl_Tool::splitString(const std::string& theValue, char separator)
00221 {
00222   std::vector<std::string> vs;
00223   if(theValue[0] == separator && theValue.size() == 1) return vs;
00224   int pos = theValue.find(separator);
00225   if(pos < 0) {
00226     vs.push_back(theValue);
00227     return vs;
00228   }
00229  
00230   std::string s = theValue;
00231   if(s[0] == separator) s = s.substr(1, s.size());
00232   while((pos = s.find(separator)) >= 0) {
00233     vs.push_back(s.substr(0, pos));
00234     s = s.substr(pos+1, s.size());
00235   }
00236                
00237   if(!s.empty() && s[0] != separator) vs.push_back(s);
00238   return vs;
00239 }
00240 
00241 //============================================================================
00242 // function : 
00243 // purpose  : The functions returns a list of substring of initial string 
00244 //            divided by given separator include empty strings
00245 //============================================================================
00246 
00247 std::vector<std::string> treatRepetation(const std::string& theValue);
00248 
00249 std::vector<std::string> treatRepetation(const std::string& theValue)
00250 {
00251   std::vector<std::string> aResult;
00252   int pos = theValue.find(";*=");
00253   if(pos < 0 )
00254     {
00255       aResult.push_back(theValue);
00256       return aResult;
00257     }
00258   std::string val(theValue.substr(0, pos));
00259   std::string suffix(theValue.substr(pos+3));
00260   int nb;
00261   std::istringstream tmp(suffix);
00262   tmp >> nb;
00263   for(int i=0; i<nb; i++)
00264     aResult.push_back(val);
00265   return aResult;
00266 }
00267 
00268 std::vector<std::string> SALOMEDSImpl_Tool::splitStringWithEmpty(const std::string& theValue, char sep)
00269 {
00270   std::vector<std::string> aResult;
00271   if(theValue[0] == sep ) aResult.push_back(std::string());
00272   int pos = theValue.find(sep);
00273   if(pos < 0 ) {
00274     if(sep == '|')
00275       {
00276         std::vector<std::string> tmp = treatRepetation(theValue);
00277         std::copy(tmp.begin(), tmp.end(), std::back_insert_iterator< std::vector<std::string> >(aResult));
00278       }
00279     else
00280       aResult.push_back(theValue);
00281     return aResult;
00282   }
00283 
00284   std::string s = theValue;
00285   if(s[0] == sep) s = s.substr(1, s.size());
00286   while((pos = s.find(sep)) >= 0) {
00287     if(sep == '|')
00288       {
00289         std::vector<std::string> tmp = treatRepetation(s.substr(0, pos));
00290         std::copy(tmp.begin(), tmp.end(), std::back_insert_iterator< std::vector<std::string> >(aResult));
00291       }
00292     else
00293       aResult.push_back(s.substr(0, pos));
00294     s = s.substr(pos+1, s.size());
00295   }
00296 
00297   if(!s.empty() && s[0] != sep) {
00298     if(sep == '|')
00299       {
00300         std::vector<std::string> tmp = treatRepetation(s);
00301         std::copy(tmp.begin(), tmp.end(), std::back_insert_iterator< std::vector<std::string> >(aResult));
00302       }
00303     else
00304       aResult.push_back(s);
00305   }
00306   if(theValue[theValue.size()-1] == sep) aResult.push_back(std::string());
00307 
00308   return aResult;
00309 }
00310 
00311 //============================================================================
00312 // function : 
00313 // purpose  : The functions returns a list of lists of substrings of initial string 
00314 //            divided by two given separators include empty strings
00315 //============================================================================
00316 std::vector< std::vector<std::string> > SALOMEDSImpl_Tool::splitStringWithEmpty(const std::string& theValue, char sep1, char sep2)
00317 {
00318   std::vector< std::vector<std::string> > aResult;
00319   if(theValue.size() > 0) {
00320     std::vector<std::string> aSections = splitStringWithEmpty( theValue, sep1 );
00321     for( int i = 0, n = aSections.size(); i < n; i++ )
00322       aResult.push_back( splitStringWithEmpty( aSections[i], sep2 ) );
00323   }
00324   return aResult;
00325 }
00326 
00327 
00328 void SALOMEDSImpl_Tool::GetSystemDate(int& year, int& month, int& day, int& hours, int& minutes, int& seconds)
00329 {
00330 #ifdef WIN32
00331   SYSTEMTIME    st;
00332 
00333   GetLocalTime ( &st );
00334 
00335   month = st.wMonth;
00336   day = st.wDay;
00337   year = st.wYear;
00338   hours = st.wHour;
00339   minutes = st.wMinute;
00340   seconds = st.wSecond;
00341 #else
00342   struct tm transfert;
00343   struct timeval tval;
00344   struct timezone tzone;
00345   int status;
00346 
00347  status = gettimeofday( &tval, &tzone );
00348  memcpy(&transfert, localtime((time_t *)&tval.tv_sec), sizeof(tm));
00349 
00350  month    = transfert.tm_mon + 1;
00351  day      = transfert.tm_mday;
00352  year     = transfert.tm_year + 1900;
00353  hours    = transfert.tm_hour;
00354  minutes  = transfert.tm_min ;
00355  seconds  = transfert.tm_sec ;
00356 #endif
00357 }
00358 
00359 //Warning undef of Ascii Winwows define
00360 #ifdef WIN32
00361 # undef GetUserName
00362 #endif
00363 std::string SALOMEDSImpl_Tool::GetUserName()
00364 {
00365 #ifdef WIN32
00366   char*  pBuff = new char[UNLEN + 1];
00367   DWORD  dwSize = UNLEN + 1;
00368   std::string retVal;
00369   ::GetUserNameA( pBuff, &dwSize );
00370   std::string theTmpUserName(pBuff,(int)dwSize -1 );
00371   retVal = theTmpUserName;
00372   delete [] pBuff;
00373   return retVal;
00374 #else
00375  struct passwd *infos;
00376  infos = getpwuid(getuid()); 
00377  return std::string(infos->pw_name);
00378 #endif
00379 }
00380 
00381 
00382 
00383 
00384 
00385