X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FTOOLSDS%2FSALOMEDS_Tool.cxx;h=2b61d3fddcb726106b06b87a1c5049cdbd34e6a6;hb=d8edd4804d9e6513a17c497c1a30326d4ba0d677;hp=d0c40386c2d8fd95cfaf27116952fdbb77884066;hpb=e6bfea36374791cd31c274a2f97df90dc60ddaf3;p=modules%2Fkernel.git diff --git a/src/TOOLSDS/SALOMEDS_Tool.cxx b/src/TOOLSDS/SALOMEDS_Tool.cxx index d0c40386c..2b61d3fdd 100644 --- a/src/TOOLSDS/SALOMEDS_Tool.cxx +++ b/src/TOOLSDS/SALOMEDS_Tool.cxx @@ -1,56 +1,59 @@ -// Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// Copyright (C) 2007-2021 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 -// +// // 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 : SALOMEDS_Tool.cxx // Created : Mon Oct 21 16:24:34 2002 // Author : Sergey RUIN - // Project : SALOME // Module : SALOMEDS - +// #include "SALOMEDS_Tool.hxx" #include "utilities.h" +#include "Basics_DirUtils.hxx" +#include "Basics_Utils.hxx" -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -#ifndef WNT -#include -#include -#include +#ifndef WIN32 #include +#include +#include +#include +#include #else +#include +#include #endif -#include +#include +#include + +#include #include CORBA_SERVER_HEADER(SALOMEDS_Attributes) -using namespace std; +bool Exists(const std::string thePath) +{ + return Kernel_Utils::IsExists(thePath); +} + //============================================================================ // function : GetTempDir @@ -58,55 +61,60 @@ using namespace std; //============================================================================ std::string SALOMEDS_Tool::GetTmpDir() { + return Kernel_Utils::GetTmpDirByEnv("SALOME_TMP_DIR"); //Find a temporary directory to store a file - TCollection_AsciiString aTmpDir; + /*string aTmpDir = ""; char *Tmp_dir = getenv("SALOME_TMP_DIR"); if(Tmp_dir != NULL) { - aTmpDir = TCollection_AsciiString(Tmp_dir); + aTmpDir = string(Tmp_dir); #ifdef WIN32 - if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\'; + if(aTmpDir[aTmpDir.size()-1] != '\\') aTmpDir+='\\'; #else - if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/'; + if(aTmpDir[aTmpDir.size()-1] != '/') aTmpDir+='/'; #endif } else { #ifdef WIN32 - aTmpDir = TCollection_AsciiString("C:\\"); + aTmpDir = string("C:\\"); #else - aTmpDir = TCollection_AsciiString("/tmp/"); + aTmpDir = string("/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 - TCollection_AsciiString aSubDir(aRND); - if(aSubDir.Length() <= 1) aSubDir = TCollection_AsciiString("123409876"); + char buffer[127]; + sprintf(buffer, "%d", aRND); + string aSubDir(buffer); + if(aSubDir.size() <= 1) aSubDir = string("123409876"); aTmpDir += aSubDir; //Get RND sub directory + string aDir = aTmpDir; + + if(Exists(aDir)) { + for(aRND = 0; Exists(aDir); aRND++) { + sprintf(buffer, "%d", aRND); + aDir = aTmpDir+buffer; //Build a unique directory name + } + } + #ifdef WIN32 - if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\'; + if(aDir[aDir.size()-1] != '\\') aDir+='\\'; #else - if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/'; + if(aDir[aTmpDir.size()-1] != '/') aDir+='/'; #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); - } - - MESSAGE("#### TMP" << aTmpDir.ToCString()); - OSD_Protection aProtection(OSD_RW, OSD_RWX, OSD_RX, OSD_RX); - aDir.Build(aProtection); +#ifdef WIN32 + CreateDirectory(aDir.c_str(), NULL); +#else + mkdir(aDir.c_str(), 0x1ff); +#endif - return aTmpDir.ToCString(); + return aDir;*/ } //============================================================================ @@ -114,33 +122,42 @@ std::string SALOMEDS_Tool::GetTmpDir() // purpose : Removes files listed in theFileList //============================================================================ void SALOMEDS_Tool::RemoveTemporaryFiles(const std::string& theDirectory, - const SALOMEDS::ListOfFileNames& theFiles, - const bool IsDirDeleted) + const ListOfFiles& theFiles, + const bool IsDirDeleted) { - TCollection_AsciiString aDirName(const_cast(theDirectory.c_str())); - - int i, aLength = theFiles.length(); - for(i=0; i& theFiles, + const std::vector& theFileNames, + const int theNamesOnly) { - int i, aLength = theFiles.length(); + int i, aLength = (int)theFiles.size(); //!< TODO: conversion from size_t to int if(aLength == 0) return (new SALOMEDS::TMPFile); //Get a temporary directory for saved a file - TCollection_AsciiString aTmpDir(const_cast(theFromDirectory.c_str())); + std::string aTmpDir = theFromDirectory; long aBufferSize = 0; long aCurrentPos; @@ -178,28 +195,32 @@ namespace //Check if the file exists if (!theNamesOnly) { // mpv 15.01.2003: if only file names must be stroed, then size of files is zero - TCollection_AsciiString aFullPath = aTmpDir + const_cast(theFiles[i].in()); - OSD_Path anOSDPath(aFullPath); - OSD_File anOSDFile(anOSDPath); - if(!anOSDFile.Exists()) continue; -#ifdef WNT - ifstream aFile(aFullPath.ToCString(), ios::binary); + std::string aFullPath = aTmpDir + theFiles[i]; + if(!Exists(aFullPath)) continue; +#ifdef WIN32 +#ifdef UNICODE + std::ifstream aFile(Kernel_Utils::utf8_decode_s(aFullPath).c_str(), std::ios::binary); +#else + std::ifstream aFile(aFullPath.c_str(), std::ios::binary); +#endif #else - ifstream aFile(aFullPath.ToCString()); + std::ifstream aFile(aFullPath.c_str()); #endif - aFile.seekg(0, ios::end); - aFileSize[i] = aFile.tellg(); - aBufferSize += aFileSize[i]; //Add a space to store the file + aFile.seekg(0, std::ios::end); + aFileSize[i] = (long)aFile.tellg(); //!< TODO: conversion from std::streamoff to long + aBufferSize += aFileSize[i]; //Add a space to store the file } - aFileNameSize[i] = strlen(theFileNames[i])+1; + aFileNameSize[i] = (int)theFileNames[i].length()+1; //!< TODO: conversion from size_t to int aBufferSize += aFileNameSize[i]; //Add a space to store the file name aBufferSize += (theNamesOnly)?4:12; //Add 4 bytes: a length of the file name, // 8 bytes: length of the file itself aNbFiles++; } + if ( aNbFiles == 0 ) return (new SALOMEDS::TMPFile); aBufferSize += 4; //4 bytes for a number of the files that will be written to the stream; - unsigned char* aBuffer = new unsigned char[aBufferSize]; + unsigned char* aBuffer = new unsigned char[aBufferSize]; + if(aBuffer == NULL) return (new SALOMEDS::TMPFile); @@ -212,16 +233,18 @@ namespace aCurrentPos = 4; for(i=0; i(theFiles[i].in()); - OSD_Path anOSDPath(aFullPath); - OSD_File anOSDFile(anOSDPath); - if(!anOSDFile.Exists()) continue; -#ifdef WNT - aFile = new ifstream(aFullPath.ToCString(), ios::binary); + std::string aFullPath = aTmpDir + theFiles[i]; + if(!Exists(aFullPath)) continue; +#ifdef WIN32 +#ifdef UNICODE + aFile = new std::ifstream (Kernel_Utils::utf8_decode_s(aFullPath).c_str(), std::ios::binary); +#else + aFile = new std::ifstream(aFullPath.c_str(), std::ios::binary); +#endif #else - aFile = new ifstream(aFullPath.ToCString()); + aFile = new std::ifstream(aFullPath.c_str()); #endif } //Initialize 4 bytes of the buffer by 0 @@ -231,21 +254,21 @@ namespace aCurrentPos += 4; //Copy the file name to the buffer - memcpy((aBuffer + aCurrentPos), theFileNames[i], aFileNameSize[i]); + memcpy((aBuffer + aCurrentPos), theFileNames[i].c_str(), aFileNameSize[i]); aCurrentPos += aFileNameSize[i]; if (!theNamesOnly) { // mpv 15.01.2003: we don't copy file content to the buffer if !theNamesOnly - //Initialize 8 bytes of the buffer by 0 - memset((aBuffer + aCurrentPos), 0, 8); - //Copy the length of the file to the buffer - memcpy((aBuffer + aCurrentPos), (aFileSize + i), ((sizeof(long) > 8) ? 8 : sizeof(long))); - aCurrentPos += 8; - - aFile->seekg(0, ios::beg); - aFile->read((char *)(aBuffer + aCurrentPos), aFileSize[i]); - aFile->close(); - delete(aFile); - aCurrentPos += aFileSize[i]; + //Initialize 8 bytes of the buffer by 0 + memset((aBuffer + aCurrentPos), 0, 8); + //Copy the length of the file to the buffer + memcpy((aBuffer + aCurrentPos), (aFileSize + i), ((sizeof(long) > 8) ? 8 : sizeof(long))); + aCurrentPos += 8; + + aFile->seekg(0, std::ios::beg); + aFile->read((char *)(aBuffer + aCurrentPos), aFileSize[i]); + aFile->close(); + delete(aFile); + aCurrentPos += aFileSize[i]; } } @@ -263,17 +286,17 @@ namespace SALOMEDS::TMPFile* SALOMEDS_Tool::PutFilesToStream(const std::string& theFromDirectory, - const SALOMEDS::ListOfFileNames& theFiles, - const int theNamesOnly) + const ListOfFiles& theFiles, + const int theNamesOnly) { - SALOMEDS::ListOfFileNames aFileNames(theFiles); + ListOfFiles aFileNames(theFiles); return ::PutFilesToStream(theFromDirectory,theFiles,aFileNames,theNamesOnly); } SALOMEDS::TMPFile* -SALOMEDS_Tool::PutFilesToStream(const SALOMEDS::ListOfFileNames& theFiles, - const SALOMEDS::ListOfFileNames& theFileNames) +SALOMEDS_Tool::PutFilesToStream(const ListOfFiles& theFiles, + const ListOfFiles& theFileNames) { return ::PutFilesToStream("",theFiles,theFileNames,0); } @@ -282,19 +305,18 @@ SALOMEDS_Tool::PutFilesToStream(const SALOMEDS::ListOfFileNames& theFiles, // function : PutStreamToFile // purpose : converts the stream "theStream" to the files //============================================================================ -SALOMEDS::ListOfFileNames_var +SALOMEDS_Tool::ListOfFiles SALOMEDS_Tool::PutStreamToFiles(const SALOMEDS::TMPFile& theStream, - const std::string& theToDirectory, - const int theNamesOnly) + const std::string& theToDirectory, + const int theNamesOnly) { - SALOMEDS::ListOfFileNames_var aFiles = new SALOMEDS::ListOfFileNames; + ListOfFiles aFiles; if(theStream.length() == 0) return aFiles; //Get a temporary directory for saving a file - TCollection_AsciiString aTmpDir(const_cast(theToDirectory.c_str())); - + std::string aTmpDir = theToDirectory; unsigned char *aBuffer = (unsigned char*)theStream.NP_data(); if(aBuffer == NULL) @@ -306,7 +328,7 @@ SALOMEDS_Tool::PutStreamToFiles(const SALOMEDS::TMPFile& theStream, //Copy the number of files in the stream memcpy(&aNbFiles, aBuffer, sizeof(int)); - aFiles->length(aNbFiles); + aFiles.reserve(aNbFiles); for(i=0; i 8) ? 8 : sizeof(long))); aCurrentPos += 8; - TCollection_AsciiString aFullPath = aTmpDir + aFileName; - ofstream aFile(aFullPath.ToCString()); - aFile.write((char *)(aBuffer+aCurrentPos), aFileSize); + std::string aFullPath = aTmpDir + aFileName; +#ifdef WIN32 +#ifdef UNICODE + std::ofstream aFile(Kernel_Utils::utf8_decode_s(aFullPath).c_str(), std::ios::binary); +#else + std::ofstream aFile(aFullPath.c_str(), std::ios::binary); +#endif +#else + std::ofstream aFile(aFullPath.c_str()); +#endif + aFile.write((char *)(aBuffer + aCurrentPos), aFileSize); aFile.close(); aCurrentPos += aFileSize; } - aFiles[i] = CORBA::string_dup(aFileName); + aFiles.push_back(CORBA::string_dup(aFileName)); delete[] aFileName; } @@ -342,11 +379,30 @@ SALOMEDS_Tool::PutStreamToFiles(const SALOMEDS::TMPFile& theStream, // purpose : Returns the name by the path //============================================================================ std::string SALOMEDS_Tool::GetNameFromPath(const std::string& thePath) { - if(thePath == "") - return ""; - OSD_Path aPath = OSD_Path(TCollection_AsciiString(const_cast(thePath.c_str()))); - TCollection_AsciiString aNameString(aPath.Name()); - return aNameString.ToCString(); + if (thePath.empty()) return ""; + std::string aPath = thePath; + bool isFound = false; + size_t pos = aPath.rfind('/'); + if(pos != std::string::npos) { + aPath = aPath.substr(pos+1, aPath.size()); + isFound = true; + } + if(!isFound) { + pos = aPath.rfind('\\'); + if(pos != std::string::npos) { + aPath = aPath.substr(pos+1, aPath.size()); + isFound = true; + } + } + if(!isFound) { + pos = aPath.rfind('|'); + if(pos != std::string::npos) aPath = aPath.substr(pos+1, aPath.size()); + } + + pos = aPath.rfind('.'); + if(pos != std::string::npos) aPath = aPath.substr(0, pos); //Remove extension + + return aPath; } //============================================================================ @@ -354,12 +410,32 @@ std::string SALOMEDS_Tool::GetNameFromPath(const std::string& thePath) { // purpose : Returns the dir by the path //============================================================================ std::string SALOMEDS_Tool::GetDirFromPath(const std::string& thePath) { - if(thePath == "") - return ""; - OSD_Path aPath = OSD_Path(TCollection_AsciiString(const_cast(thePath.c_str()))); - TCollection_AsciiString aDirString(aPath.Trek()); - aDirString.ChangeAll('|','/'); - return aDirString.ToCString(); + if (thePath.empty()) return ""; + + size_t pos = thePath.rfind('/'); + std::string path; + if(pos != std::string::npos) { + path = thePath.substr(0, pos+1); + } + if(path.empty()) { + pos = thePath.rfind('\\'); + if(pos != std::string::npos) path = thePath.substr(0, pos+1); + } + if(path.empty()) { + pos = thePath.rfind('|'); + if(pos != std::string::npos) path = thePath.substr(0, pos+1); + } + if(path.empty()) { + path = thePath+"/"; + } + +#ifdef WIN32 //Check if the only disk letter is given as path + if(path.size() == 2 && path[1] == ':') path +='\\'; +#endif + + for(size_t i = 0, len = path.size(); i