Salome HOME
ADD a end user module (services.py) to help the manipulation of SALOME KERNEL service...
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_Tool.cxx
index 898d8d67e9cc35266e93123b7348dcb3db482f3b..89e9559377992955df9f4d85186a419f86ae8a0c 100644 (file)
@@ -1,24 +1,25 @@
-//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
 //
-//  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+// 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 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
-//  Lesser General Public License for more details.
+// 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
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+// 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
+// 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
@@ -30,6 +31,8 @@
 #include <fstream>
 #include <stdlib.h>
 #include <string.h>
+#include <iterator>
+#include <sstream>
 
 #include "SALOMEDSImpl_Tool.hxx"
 
 #include <windows.h>
 #endif
 
-using namespace std;
-
 
-
-bool Exists(const string thePath) 
+bool Exists(const std::string thePath) 
 {
 #ifdef WIN32 
   if (  GetFileAttributes (  thePath.c_str()  ) == 0xFFFFFFFF  ) { 
-    if (  GetLastError () != ERROR_FILE_NOT_FOUND  ) {
+    if (  GetLastError () == ERROR_FILE_NOT_FOUND  ) {
       return false;
     }
   }
@@ -72,15 +72,15 @@ bool Exists(const string thePath)
 // function : GetTempDir
 // purpose  : Return a temp directory to store created files like "/tmp/sub_dir/" 
 //============================================================================ 
-string SALOMEDSImpl_Tool::GetTmpDir()
+std::string SALOMEDSImpl_Tool::GetTmpDir()
 {
   //Find a temporary directory to store a file
 
-  string aTmpDir;
+  std::string aTmpDir;
 
   char *Tmp_dir = getenv("SALOME_TMP_DIR");
   if(Tmp_dir != NULL) {
-    aTmpDir = string(Tmp_dir);
+    aTmpDir = std::string(Tmp_dir);
 #ifdef WIN32
     if(aTmpDir[aTmpDir.size()-1] != '\\') aTmpDir+='\\';
 #else
@@ -89,9 +89,9 @@ string SALOMEDSImpl_Tool::GetTmpDir()
   }
   else {
 #ifdef WIN32
-    aTmpDir = string("C:\\");
+    aTmpDir = std::string("C:\\");
 #else
-    aTmpDir = string("/tmp/");
+    aTmpDir = std::string("/tmp/");
 #endif
   }
 
@@ -99,12 +99,12 @@ string SALOMEDSImpl_Tool::GetTmpDir()
   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);
-  string aSubDir(buffer);
-  if(aSubDir.size() <= 1) aSubDir = string("123409876");
+  std::string aSubDir(buffer);
+  if(aSubDir.size() <= 1) aSubDir = std::string("123409876");
 
   aTmpDir += aSubDir; //Get RND sub directory
 
-  string aDir = aTmpDir;
+  std::string aDir = aTmpDir;
   
   if(Exists(aDir)) {
     for(aRND = 0; Exists(aDir); aRND++) {
@@ -133,15 +133,15 @@ string SALOMEDSImpl_Tool::GetTmpDir()
 // function : RemoveTemporaryFiles
 // purpose  : Removes files listed in theFileList
 //============================================================================
-void SALOMEDSImpl_Tool::RemoveTemporaryFiles(const string& theDirectory, 
-                                            const vector<string>& theFiles,
-                                            const bool IsDirDeleted)
+void SALOMEDSImpl_Tool::RemoveTemporaryFiles(const std::string& theDirectory, 
+                                             const std::vector<std::string>& theFiles,
+                                             const bool IsDirDeleted)
 {
-  string aDirName = theDirectory;
+  std::string aDirName = theDirectory;
 
   int i, aLength = theFiles.size();
   for(i=1; i<=aLength; i++) {
-    string aFile(aDirName);
+    std::string aFile(aDirName);
     aFile += theFiles[i-1];
     if(!Exists(aFile)) continue;
 
@@ -168,7 +168,7 @@ void SALOMEDSImpl_Tool::RemoveTemporaryFiles(const string& theDirectory,
 // function : GetNameFromPath
 // purpose  : Returns the name by the path
 //============================================================================
-string SALOMEDSImpl_Tool::GetNameFromPath(const string& thePath) {
+std::string SALOMEDSImpl_Tool::GetNameFromPath(const std::string& thePath) {
   if (thePath.empty()) return "";
   int pos = thePath.rfind('/');
   if(pos > 0) return thePath.substr(pos+1, thePath.size());
@@ -183,14 +183,14 @@ string SALOMEDSImpl_Tool::GetNameFromPath(const string& thePath) {
 // function : GetDirFromPath
 // purpose  : Returns the dir by the path
 //============================================================================
-string SALOMEDSImpl_Tool::GetDirFromPath(const string& thePath) {
+std::string SALOMEDSImpl_Tool::GetDirFromPath(const std::string& thePath) {
 #ifdef WIN32
-  string separator = "\\";
+  std::string separator = "\\";
 #else
-  string separator = "/";
+  std::string separator = "/";
 #endif
 
-  string path;
+  std::string path;
   if (!thePath.empty()) {
     int pos = thePath.rfind('/');
     if (pos < 0) pos = thePath.rfind('\\');
@@ -199,7 +199,7 @@ string SALOMEDSImpl_Tool::GetDirFromPath(const string& thePath) {
     if (pos > 0)
       path = thePath.substr(0, pos+1);
     else
-      path = string(".") + separator;
+      path = std::string(".") + separator;
 
 #ifdef WIN32  //Check if the only disk letter is given as path
     if (path.size() == 2 && path[1] == ':') path += separator;
@@ -217,9 +217,9 @@ string SALOMEDSImpl_Tool::GetDirFromPath(const string& thePath) {
 // purpose  : The functions returns a list of substring of initial string 
 //            divided by given separator
 //============================================================================
-vector<string> SALOMEDSImpl_Tool::splitString(const string& theValue, char separator)
+std::vector<std::string> SALOMEDSImpl_Tool::splitString(const std::string& theValue, char separator)
 {
-  vector<string> vs;
+  std::vector<std::string> vs;
   if(theValue[0] == separator && theValue.size() == 1) return vs;
   int pos = theValue.find(separator);
   if(pos < 0) {
@@ -227,13 +227,13 @@ vector<string> SALOMEDSImpl_Tool::splitString(const string& theValue, char separ
     return vs;
   }
  
-  string s = theValue;
+  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;
 }
@@ -243,25 +243,67 @@ vector<string> SALOMEDSImpl_Tool::splitString(const string& theValue, char separ
 // purpose  : The functions returns a list of substring of initial string 
 //            divided by given separator include empty strings
 //============================================================================
-vector<string> SALOMEDSImpl_Tool::splitStringWithEmpty(const string& theValue, char sep)
+
+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(";*=");
+  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)
 {
-  vector<string> aResult;
-  if(theValue[0] == sep ) aResult.push_back(string());
+  std::vector<std::string> aResult;
+  if(theValue[0] == sep ) aResult.push_back(std::string());
   int pos = theValue.find(sep);
   if(pos < 0 ) {
-    aResult.push_back(theValue);
+    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;
   }
 
-  string s = theValue;
+  std::string s = theValue;
   if(s[0] == sep) s = s.substr(1, s.size());
   while((pos = s.find(sep)) >= 0) {
-    aResult.push_back(s.substr(0, pos));
+    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) aResult.push_back(s);
-  if(theValue[theValue.size()-1] == sep) aResult.push_back(string());
+  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;
 }
@@ -271,11 +313,11 @@ vector<string> SALOMEDSImpl_Tool::splitStringWithEmpty(const string& theValue, c
 // purpose  : The functions returns a list of lists of substrings of initial string 
 //            divided by two given separators include empty strings
 //============================================================================
-vector< vector<string> > SALOMEDSImpl_Tool::splitStringWithEmpty(const string& theValue, char sep1, char sep2)
+std::vector< std::vector<std::string> > SALOMEDSImpl_Tool::splitStringWithEmpty(const std::string& theValue, char sep1, char sep2)
 {
-  vector< vector<string> > aResult;
+  std::vector< std::vector<std::string> > aResult;
   if(theValue.size() > 0) {
-    vector<string> aSections = splitStringWithEmpty( theValue, sep1 );
+    std::vector<std::string> aSections = splitStringWithEmpty( theValue, sep1 );
     for( int i = 0, n = aSections.size(); i < n; i++ )
       aResult.push_back( splitStringWithEmpty( aSections[i], sep2 ) );
   }
@@ -318,21 +360,21 @@ void SALOMEDSImpl_Tool::GetSystemDate(int& year, int& month, int& day, int& hour
 #ifdef WIN32
 # undef GetUserName
 #endif
-string SALOMEDSImpl_Tool::GetUserName()
+std::string SALOMEDSImpl_Tool::GetUserName()
 {
 #ifdef WIN32
   char*  pBuff = new char[UNLEN + 1];
   DWORD  dwSize = UNLEN + 1;
-  string retVal;
+  std::string retVal;
   ::GetUserNameA( pBuff, &dwSize );
-  string theTmpUserName(pBuff,(int)dwSize -1 );
+  std::string theTmpUserName(pBuff,(int)dwSize -1 );
   retVal = theTmpUserName;
   delete [] pBuff;
   return retVal;
 #else
  struct passwd *infos;
  infos = getpwuid(getuid()); 
- return string(infos->pw_name);
+ return std::string(infos->pw_name);
 #endif
 }