Salome HOME
Updated copyright comment
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_Tool.cxx
index 1dca26e6ed99f218c10eb5761517f5bfe99a4ad9..b7825756c085ff915e8527e9e8b1a0eed70739f3 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
+// 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
@@ -34,6 +34,9 @@
 #include <iterator>
 #include <sstream>
 
+#include "Basics_DirUtils.hxx"
+#include "Basics_Utils.hxx"
+
 #include "SALOMEDSImpl_Tool.hxx"
 
 #ifndef WIN32
 
 bool SALOMEDS_Exists(const std::string thePath)
 {
-#ifdef WIN32 
-  if (  GetFileAttributes (  thePath.c_str()  ) == 0xFFFFFFFF  ) { 
-    if (  GetLastError () == ERROR_FILE_NOT_FOUND  ) {
-      return false;
-    }
-  }
-#else 
-  int status = access ( thePath.c_str() , F_OK ); 
-  if (status != 0) return false;
-#endif
-  return true;
+       return Kernel_Utils::IsExists( thePath );
 }
 
-
-
-
 //============================================================================
 // function : GetTempDir
 // purpose  : Return a temp directory to store created files like "/tmp/sub_dir/"
 //============================================================================
 std::string SALOMEDSImpl_Tool::GetTmpDir()
 {
-  //Find a temporary directory to store a file
-
-  std::string aTmpDir;
-
-  char *Tmp_dir = getenv("SALOME_TMP_DIR");
-  if ( Tmp_dir != NULL && SALOMEDS_Exists( Tmp_dir ))
-  {
-    aTmpDir = Tmp_dir;
-#ifdef WIN32
-    if ( aTmpDir.back() != '\\') aTmpDir += '\\';
-#else
-    if ( aTmpDir.back() != '/') aTmpDir += '/';
-#endif
-  }
-  else
-  {
-#ifdef WIN32
-    aTmpDir = "C:\\";
-#else
-    aTmpDir = "/tmp/";
-#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
-  char buffer[127];
-  sprintf( buffer, "%d", aRND );
-  std::string aSubDir( buffer );
-  if ( aSubDir.size() <= 1 ) aSubDir = "123409876";
-
-  aTmpDir += aSubDir; //Get RND sub directory
-
-  std::string aDir = aTmpDir;
-
-  for ( aRND = 0; SALOMEDS_Exists( aDir ); aRND++ )
-  {
-    sprintf(buffer, "%d", aRND);
-    aDir = aTmpDir + buffer;  //Build a unique directory name
-  }
-
-#ifdef WIN32
-  if ( aDir.back() != '\\') aDir += '\\';
-#else
-  if ( aDir.back() != '/' ) aDir += '/';
-#endif
-
-
-#ifdef WIN32
-  CreateDirectory( aDir.c_str(), NULL );
-#else
-  mkdir( aDir.c_str(), 0x1ff );
-#endif
-
-  return aDir;
+  return Kernel_Utils::GetTmpDirByEnv("SALOME_TMP_DIR");
 }
 
 //============================================================================
@@ -140,14 +77,19 @@ void SALOMEDSImpl_Tool::RemoveTemporaryFiles(const std::string& theDirectory,
 {
   std::string aDirName = theDirectory;
 
-  int i, aLength = theFiles.size();
+  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;
 
 #ifdef WIN32
-    DeleteFile(aFile.c_str());
+#if defined(UNICODE)
+       std::wstring aFileToDelete = Kernel_Utils::utf8_decode_s(aFile);
+#else
+       std::string aFileToDelete = aFile;
+#endif
+    DeleteFile( aFileToDelete.c_str() );
 #else 
     unlink(aFile.c_str());
 #endif
@@ -156,7 +98,12 @@ void SALOMEDSImpl_Tool::RemoveTemporaryFiles(const std::string& theDirectory,
   if(IsDirDeleted) {
     if(SALOMEDS_Exists(aDirName)) {
 #ifdef WIN32
-      RemoveDirectory(aDirName.c_str());
+#if defined(UNICODE)
+               std::wstring aDirToDelete = Kernel_Utils::utf8_decode_s(aDirName);
+#else
+               std::string aDirToDelete = aDirName;
+#endif
+      RemoveDirectory(aDirToDelete.c_str());
 #else
       rmdir(aDirName.c_str());
 #endif
@@ -171,7 +118,7 @@ void SALOMEDSImpl_Tool::RemoveTemporaryFiles(const std::string& theDirectory,
 //============================================================================
 std::string SALOMEDSImpl_Tool::GetNameFromPath(const std::string& thePath) {
   if (thePath.empty()) return "";
-  int pos = thePath.rfind('/');
+  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()); 
@@ -193,7 +140,7 @@ std::string SALOMEDSImpl_Tool::GetDirFromPath(const std::string& thePath) {
 
   std::string path;
   if (!thePath.empty()) {
-    int pos = thePath.rfind('/');
+    int pos = (int)thePath.rfind('/'); //TODO: conversion from size_t to int
     if (pos < 0) pos = thePath.rfind('\\');
     if (pos < 0) pos = thePath.rfind('|');
     
@@ -222,7 +169,7 @@ std::vector<std::string> SALOMEDSImpl_Tool::splitString(const std::string& theVa
 {
   std::vector<std::string> vs;
   if(theValue[0] == separator && theValue.size() == 1) return vs;
-  int pos = theValue.find(separator);
+  int pos = (int)theValue.find(separator); //TODO: conversion from size_t to int
   if(pos < 0) {
     vs.push_back(theValue);
     return vs;
@@ -250,7 +197,7 @@ 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 = theValue.find(";*=");
+  int pos = (int)theValue.find(";*="); //TODO: conversion from size_t to int
   if(pos < 0 )
     {
       aResult.push_back(theValue);
@@ -270,7 +217,7 @@ std::vector<std::string> SALOMEDSImpl_Tool::splitStringWithEmpty(const std::stri
 {
   std::vector<std::string> aResult;
   if(theValue[0] == sep ) aResult.push_back(std::string());
-  int pos = theValue.find(sep);
+  int pos = (int)theValue.find(sep); //TODO: conversion from size_t to int
   if(pos < 0 ) {
     if(sep == '|')
       {
@@ -319,7 +266,7 @@ std::vector< std::vector<std::string> > SALOMEDSImpl_Tool::splitStringWithEmpty(
   std::vector< std::vector<std::string> > aResult;
   if(theValue.size() > 0) {
     std::vector<std::string> aSections = splitStringWithEmpty( theValue, sep1 );
-    for( int i = 0, n = aSections.size(); i < n; i++ )
+    for( size_t i = 0, n = aSections.size(); i < n; i++ )
       aResult.push_back( splitStringWithEmpty( aSections[i], sep2 ) );
   }
   return aResult;