Salome HOME
Updated copyright comment
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_Tool.cxx
index 46529552b6b34ef94a93d4bd6e9f02504d0b93c3..b7825756c085ff915e8527e9e8b1a0eed70739f3 100644 (file)
-// Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
+//
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
-// 
+//
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either 
-// version 2.1 of the License.
-// 
-// This library is distributed in the hope that it will be useful 
-// but WITHOUT ANY WARRANTY; without even the implied warranty of 
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 // Lesser General Public License for more details.
 //
-// You should have received a copy of the GNU Lesser General Public  
-// License along with this library; if not, write to the Free Software 
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 //  File      : SALOMEDSImpl_Tool.cxx
 //  Created   : Mon Oct 21 16:24:34 2002
 //  Author    : Sergey RUIN
-
 //  Project   : SALOME
 //  Module    : SALOMEDSImpl
-
-#include "SALOMEDSImpl_Tool.hxx"
-
+//
 #include <stdio.h>
 #include <iostream> 
 #include <fstream>
-#include <OSD_Path.hxx>
-#include <OSD_File.hxx>
-#include <OSD_Directory.hxx>
-#include <OSD_Process.hxx>
-#include <OSD_Directory.hxx>
-#include <OSD_Protection.hxx>
-#include <OSD_SingleProtection.hxx>
-#include <OSD_FileIterator.hxx>
-
-#ifndef WNT
+#include <stdlib.h>
+#include <string.h>
+#include <iterator>
+#include <sstream>
+
+#include "Basics_DirUtils.hxx"
+#include "Basics_Utils.hxx"
+
+#include "SALOMEDSImpl_Tool.hxx"
+
+#ifndef WIN32
 #include <sys/time.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <pwd.h> 
+#include <unistd.h>
 #else
 #include <time.h>
+#include <lmcons.h>
+//#include <winbase.h>
+#include <windows.h>
 #endif
-#include <stdlib.h>
 
-using namespace std;
 
+bool SALOMEDS_Exists(const std::string thePath)
+{
+       return Kernel_Utils::IsExists( thePath );
+}
 
 //============================================================================
 // function : GetTempDir
-// purpose  : Return a temp directory to store created files like "/tmp/sub_dir/" 
-//============================================================================ 
-TCollection_AsciiString SALOMEDSImpl_Tool::GetTmpDir()
+// purpose  : Return a temp directory to store created files like "/tmp/sub_dir/"
+//============================================================================
+std::string SALOMEDSImpl_Tool::GetTmpDir()
+{
+  return Kernel_Utils::GetTmpDirByEnv("SALOME_TMP_DIR");
+}
+
+//============================================================================
+// function : RemoveTemporaryFiles
+// purpose  : Removes files listed in theFileList
+//============================================================================
+void SALOMEDSImpl_Tool::RemoveTemporaryFiles(const std::string& theDirectory,
+                                             const std::vector<std::string>& theFiles,
+                                             const bool IsDirDeleted)
 {
-  //Find a temporary directory to store a file
+  std::string aDirName = theDirectory;
 
-  TCollection_AsciiString aTmpDir;
+  size_t i, aLength = theFiles.size(); 
+  for(i=1; i<=aLength; i++) {
+    std::string aFile(aDirName);
+    aFile += theFiles[i-1];
+    if(!SALOMEDS_Exists(aFile)) continue;
 
-  char *Tmp_dir = getenv("SALOME_TMP_DIR");
-  if(Tmp_dir != NULL) {
-    aTmpDir = TCollection_AsciiString(Tmp_dir);
 #ifdef WIN32
-    if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
+#if defined(UNICODE)
+       std::wstring aFileToDelete = Kernel_Utils::utf8_decode_s(aFile);
 #else
-    if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
-#endif      
+       std::string aFileToDelete = aFile;
+#endif
+    DeleteFile( aFileToDelete.c_str() );
+#else 
+    unlink(aFile.c_str());
+#endif
   }
-  else {
+
+  if(IsDirDeleted) {
+    if(SALOMEDS_Exists(aDirName)) {
 #ifdef WIN32
-    aTmpDir = TCollection_AsciiString("C:\\");
+#if defined(UNICODE)
+               std::wstring aDirToDelete = Kernel_Utils::utf8_decode_s(aDirName);
 #else
-    aTmpDir = TCollection_AsciiString("/tmp/");
+               std::string aDirToDelete = aDirName;
 #endif
+      RemoveDirectory(aDirToDelete.c_str());
+#else
+      rmdir(aDirName.c_str());
+#endif
+    }
   }
 
-  srand((unsigned int)time(NULL));
-  int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
-  TCollection_AsciiString aSubDir(aRND);
-  if(aSubDir.Length() <= 1) aSubDir = TCollection_AsciiString("123409876");
+}
 
-  aTmpDir += aSubDir; //Get RND sub directory
+//============================================================================
+// function : GetNameFromPath
+// purpose  : Returns the name by the path
+//============================================================================
+std::string SALOMEDSImpl_Tool::GetNameFromPath(const std::string& thePath) {
+  if (thePath.empty()) return "";
+  int pos = (int)thePath.rfind('/'); //TODO: conversion from size_t to int
+  if(pos >= 0) return thePath.substr(pos+1, thePath.size());
+  pos = thePath.rfind('\\'); 
+  if(pos >= 0) return thePath.substr(pos+1, thePath.size()); 
+  pos = thePath.rfind('|');
+  if(pos >= 0) return thePath.substr(pos+1, thePath.size()); 
+  return thePath;
+}
 
+//============================================================================
+// function : GetDirFromPath
+// purpose  : Returns the dir by the path
+//============================================================================
+std::string SALOMEDSImpl_Tool::GetDirFromPath(const std::string& thePath) {
 #ifdef WIN32
-  if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
+  std::string separator = "\\";
 #else
-  if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
+  std::string separator = "/";
 #endif
 
-  OSD_Path aPath(aTmpDir);
-  OSD_Directory aDir(aPath);
-
-  for(aRND = 0; aDir.Exists(); aRND++) {
-    aTmpDir.Insert((aTmpDir.Length() - 1), TCollection_AsciiString(aRND));  //Build a unique directory name
-    aPath = OSD_Path(aTmpDir);
-    aDir = OSD_Directory(aPath);
+  std::string path;
+  if (!thePath.empty()) {
+    int pos = (int)thePath.rfind('/'); //TODO: conversion from size_t to int
+    if (pos < 0) pos = thePath.rfind('\\');
+    if (pos < 0) pos = thePath.rfind('|');
+    
+    if (pos >= 0)
+      path = thePath.substr(0, pos+1);
+    else
+      path = std::string(".") + separator;
+
+#ifdef WIN32  //Check if the only disk letter is given as path
+    if (path.size() == 2 && path[1] == ':') path += separator;
+#endif
+    
+    while ( (pos=path.find('|')) >= 0 )
+      path.replace(pos, 1, separator);
   }
-
-  OSD_Protection aProtection(OSD_RW, OSD_RWX, OSD_RX, OSD_RX);
-  aDir.Build(aProtection);
-
-  return aTmpDir;
+  
+  return path;
 }
 
 //============================================================================
-// function : RemoveTemporaryFiles
-// purpose  : Removes files listed in theFileList
+// function : 
+// purpose  : The functions returns a list of substring of initial string 
+//            divided by given separator
 //============================================================================
-void SALOMEDSImpl_Tool::RemoveTemporaryFiles(const TCollection_AsciiString& theDirectory, 
-                                            const Handle(TColStd_HSequenceOfAsciiString)& theFiles,
-                                            const bool IsDirDeleted)
+std::vector<std::string> SALOMEDSImpl_Tool::splitString(const std::string& theValue, char separator)
 {
-  TCollection_AsciiString aDirName = theDirectory;
-
-  int i, aLength = theFiles->Length();
-  for(i=1; i<=aLength; i++) {
-    TCollection_AsciiString aFile(aDirName);
-    aFile += theFiles->Value(i);
-    OSD_Path anOSDPath(aFile);
-    OSD_File anOSDFile(anOSDPath);
-    if(!anOSDFile.Exists()) continue;
-
-    OSD_Protection aProtection = anOSDFile.Protection();
-    aProtection.SetUser(OSD_RW);
-    anOSDFile.SetProtection(aProtection);
-
-    anOSDFile.Remove();
+  std::vector<std::string> vs;
+  if(theValue[0] == separator && theValue.size() == 1) return vs;
+  int pos = (int)theValue.find(separator); //TODO: conversion from size_t to int
+  if(pos < 0) {
+    vs.push_back(theValue);
+    return vs;
   }
-
-  if(IsDirDeleted) {
-    OSD_Path aPath(aDirName);
-    OSD_Directory aDir(aPath);
-    OSD_FileIterator anIterator(aPath, '*');
-
-    if(aDir.Exists() && !anIterator.More()) aDir.Remove();
+  std::string s = theValue;
+  if(s[0] == separator) s = s.substr(1, s.size());
+  while((pos = s.find(separator)) >= 0) {
+    vs.push_back(s.substr(0, pos));
+    s = s.substr(pos+1, s.size());
   }
-
+               
+  if(!s.empty() && s[0] != separator) vs.push_back(s);
+  return vs;
 }
 
 //============================================================================
-// function : GetNameFromPath
-// purpose  : Returns the name by the path
+// function : 
+// purpose  : The functions returns a list of substring of initial string 
+//            divided by given separator include empty strings
 //============================================================================
-TCollection_AsciiString SALOMEDSImpl_Tool::GetNameFromPath(const TCollection_AsciiString& thePath) {
-  if (thePath.IsEmpty()) return "";
-  OSD_Path aPath = OSD_Path(thePath);
-  TCollection_AsciiString aNameString(aPath.Name());
-  return aNameString;
-}
 
-//============================================================================
-// function : GetDirFromPath
-// purpose  : Returns the dir by the path
-//============================================================================
-TCollection_AsciiString SALOMEDSImpl_Tool::GetDirFromPath(const TCollection_AsciiString& thePath) {
-  if (thePath.IsEmpty()) return "";
-  OSD_Path aPath = OSD_Path(thePath);
-  TCollection_AsciiString aDirString(aPath.Trek());
-  aDirString.ChangeAll('|','/');
-  return aDirString;
+std::vector<std::string> treatRepetation(const std::string& theValue);
+
+std::vector<std::string> treatRepetation(const std::string& theValue)
+{
+  std::vector<std::string> aResult;
+  int pos = (int)theValue.find(";*="); //TODO: conversion from size_t to int
+  if(pos < 0 )
+    {
+      aResult.push_back(theValue);
+      return aResult;
+    }
+  std::string val(theValue.substr(0, pos));
+  std::string suffix(theValue.substr(pos+3));
+  int nb;
+  std::istringstream tmp(suffix);
+  tmp >> nb;
+  for(int i=0; i<nb; i++)
+    aResult.push_back(val);
+  return aResult;
 }
 
+std::vector<std::string> SALOMEDSImpl_Tool::splitStringWithEmpty(const std::string& theValue, char sep)
+{
+  std::vector<std::string> aResult;
+  if(theValue[0] == sep ) aResult.push_back(std::string());
+  int pos = (int)theValue.find(sep); //TODO: conversion from size_t to int
+  if(pos < 0 ) {
+    if(sep == '|')
+      {
+        std::vector<std::string> tmp = treatRepetation(theValue);
+        std::copy(tmp.begin(), tmp.end(), std::back_insert_iterator< std::vector<std::string> >(aResult));
+      }
+    else
+      aResult.push_back(theValue);
+    return aResult;
+  }
 
+  std::string s = theValue;
+  if(s[0] == sep) s = s.substr(1, s.size());
+  while((pos = s.find(sep)) >= 0) {
+    if(sep == '|')
+      {
+        std::vector<std::string> tmp = treatRepetation(s.substr(0, pos));
+        std::copy(tmp.begin(), tmp.end(), std::back_insert_iterator< std::vector<std::string> >(aResult));
+      }
+    else
+      aResult.push_back(s.substr(0, pos));
+    s = s.substr(pos+1, s.size());
+  }
 
+  if(!s.empty() && s[0] != sep) {
+    if(sep == '|')
+      {
+        std::vector<std::string> tmp = treatRepetation(s);
+        std::copy(tmp.begin(), tmp.end(), std::back_insert_iterator< std::vector<std::string> >(aResult));
+      }
+    else
+      aResult.push_back(s);
+  }
+  if(theValue[theValue.size()-1] == sep) aResult.push_back(std::string());
 
+  return aResult;
+}
 
+//============================================================================
+// function : 
+// purpose  : The functions returns a list of lists of substrings of initial string 
+//            divided by two given separators include empty strings
+//============================================================================
+std::vector< std::vector<std::string> > SALOMEDSImpl_Tool::splitStringWithEmpty(const std::string& theValue, char sep1, char sep2)
+{
+  std::vector< std::vector<std::string> > aResult;
+  if(theValue.size() > 0) {
+    std::vector<std::string> aSections = splitStringWithEmpty( theValue, sep1 );
+    for( size_t i = 0, n = aSections.size(); i < n; i++ )
+      aResult.push_back( splitStringWithEmpty( aSections[i], sep2 ) );
+  }
+  return aResult;
+}
 
 
+void SALOMEDSImpl_Tool::GetSystemDate(int& year, int& month, int& day, int& hours, int& minutes, int& seconds)
+{
+#ifdef WIN32
+  SYSTEMTIME    st;
 
+  GetLocalTime ( &st );
 
+  month = st.wMonth;
+  day = st.wDay;
+  year = st.wYear;
+  hours = st.wHour;
+  minutes = st.wMinute;
+  seconds = st.wSecond;
+#else
+  struct tm transfert;
+  struct timeval tval;
+  struct timezone tzone;
+  //int status;
+
+  /*status = */ gettimeofday( &tval, &tzone );
+  memcpy(&transfert, localtime((time_t *)&tval.tv_sec), sizeof(tm));
+
+  month    = transfert.tm_mon + 1;
+  day      = transfert.tm_mday;
+  year     = transfert.tm_year + 1900;
+  hours    = transfert.tm_hour;
+  minutes  = transfert.tm_min ;
+  seconds  = transfert.tm_sec ;
+#endif
+}
 
+//Warning undef of Ascii Winwows define
+#ifdef WIN32
+# undef GetUserName
+#endif
+std::string SALOMEDSImpl_Tool::GetUserName()
+{
+#ifdef WIN32
+  char*  pBuff = new char[UNLEN + 1];
+  DWORD  dwSize = UNLEN + 1;
+  std::string retVal;
+  ::GetUserNameA( pBuff, &dwSize );
+  std::string theTmpUserName(pBuff,(int)dwSize -1 );
+  retVal = theTmpUserName;
+  delete [] pBuff;
+  return retVal;
+#else
+ struct passwd *infos;
+ infos = getpwuid(getuid()); 
+ return std::string(infos->pw_name);
+#endif
+}