X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FLightApp%2FLightApp_Driver.cxx;h=614fc3024752f4a342d86564508d6d5bb79b78b6;hb=53ecba1f9588efa0af0b9d3eee0ccec374888cf0;hp=a10d2be1e9cf34e4761278182402f6ddf7b17393;hpb=d690edb4953cbca977d9c8fd5ebdb567811d9bb4;p=modules%2Fgui.git diff --git a/src/LightApp/LightApp_Driver.cxx b/src/LightApp/LightApp_Driver.cxx index a10d2be1e..614fc3024 100644 --- a/src/LightApp/LightApp_Driver.cxx +++ b/src/LightApp/LightApp_Driver.cxx @@ -1,3 +1,25 @@ +// Copyright (C) 2007-2014 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, 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 +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + #include "LightApp_Driver.h" #include @@ -5,14 +27,11 @@ #include #include #include -#include -#include #include -#include #include -#include -#include +#include +#include #ifdef WIN32 #include @@ -20,6 +39,7 @@ /*! Constructor.*/ LightApp_Driver::LightApp_Driver() +: myIsTemp( false ) { } @@ -28,12 +48,9 @@ LightApp_Driver::~LightApp_Driver() { } -using namespace std; - -//================================================================ -// Function : SaveDatasInFile -/*! Purpose : save in file 'theFileName' datas from this driver*/ -//================================================================ +/*! + Save in file 'theFileName' datas from this driver +*/ bool LightApp_Driver::SaveDatasInFile( const char* theFileName, bool isMultiFile ) { int aNbModules = 0; @@ -66,7 +83,7 @@ bool LightApp_Driver::SaveDatasInFile( const char* theFileName, bool isMultiFile if(aFileBuffer == NULL) return false; - myTmpDir = QDir::convertSeparators( QFileInfo( theFileName ).dirPath( true ) + "/" ).latin1() ; + myTmpDir = QDir::convertSeparators( QFileInfo( theFileName ).absolutePath() + "/" ).toLatin1().constData() ; int aCurrentPos = 0; @@ -98,7 +115,7 @@ bool LightApp_Driver::SaveDatasInFile( const char* theFileName, bool isMultiFile aCurrentPos += aBufferSize[i]; } -#ifdef WNT +#ifdef WIN32 ofstream aFile(theFileName, ios::out | ios::binary); #else ofstream aFile(theFileName); @@ -114,19 +131,18 @@ bool LightApp_Driver::SaveDatasInFile( const char* theFileName, bool isMultiFile return true; } -//======================================================================= -// name : ReaDatasFromFile -/*! Purpose : filling current driver from file 'theFileName'*/ -//======================================================================= +/*! + Filling current driver from file 'theFileName' +*/ bool LightApp_Driver::ReadDatasFromFile( const char* theFileName, bool isMultiFile ) { -#ifdef WNT +#ifdef WIN32 ifstream aFile(theFileName, ios::binary); #else ifstream aFile(theFileName); #endif - myTmpDir = QDir::convertSeparators( QFileInfo( theFileName ).dirPath( true ) + "/" ).latin1() ; + myTmpDir = QDir::convertSeparators( QFileInfo( theFileName ).absolutePath() + "/" ).toLatin1().constData() ; aFile.seekg(0, ios::end); int aFileBufferSize = aFile.tellg(); @@ -174,10 +190,9 @@ bool LightApp_Driver::ReadDatasFromFile( const char* theFileName, bool isMultiFi return true; } -//================================================================ -// Function : GetTmpDir -/*! Purpose : returns temp directory for path 'theURL'*/ -//================================================================ +/*! + \return temp directory for path 'theURL' +*/ std::string LightApp_Driver::GetTmpDir (const char* theURL, const bool isMultiFile) { std::string anURLDir = GetDirFromPath(theURL); @@ -186,10 +201,9 @@ std::string LightApp_Driver::GetTmpDir (const char* theURL, const bool isMultiF return aTmpDir; } -//================================================================ -// Function : GetListOfFiles -/*! Purpose : returns list of files for module with name 'theModuleName'*/ -//================================================================ +/*! + \return list of files for module with name 'theModuleName' +*/ LightApp_Driver::ListOfFiles LightApp_Driver::GetListOfFiles( const char* theModuleName ) { ListOfFiles aListOfFiles; @@ -201,20 +215,18 @@ LightApp_Driver::ListOfFiles LightApp_Driver::GetListOfFiles( const char* theMod return aListOfFiles; } -//================================================================ -// Function : SetListOfFiles -/*! Purpose : sets list of files for module with name 'theModuleName'*/ -//================================================================ +/*! + Sets list of files for module with name 'theModuleName' +*/ void LightApp_Driver::SetListOfFiles( const char* theModuleName, const ListOfFiles theListOfFiles ) { std::string aName (theModuleName); myMap[aName] = theListOfFiles; } -//============================================================================ -// function : PutFilesToStream -/*! Purpose : converts files which was created from module into a byte sequence unsigned char*/ -//============================================================================ +/*! + Converts files which was created from module into a byte sequence unsigned char +*/ void LightApp_Driver::PutFilesToStream( const std::string& theModuleName, unsigned char*& theBuffer, long& theBufferSize, bool theNamesOnly ) { @@ -248,7 +260,7 @@ void LightApp_Driver::PutFilesToStream( const std::string& theModuleName, unsign OSD_Path anOSDPath(aFullPath); OSD_File anOSDFile(anOSDPath); if(!anOSDFile.Exists()) continue; -#ifdef WNT +#ifdef WIN32 ifstream aFile(aFullPath.ToCString(), ios::binary); #else ifstream aFile(aFullPath.ToCString()); @@ -286,7 +298,7 @@ void LightApp_Driver::PutFilesToStream( const std::string& theModuleName, unsign OSD_Path anOSDPath(aFullPath); OSD_File anOSDFile(anOSDPath); if(!anOSDFile.Exists()) continue; -#ifdef WNT +#ifdef WIN32 aFile = new ifstream(aFullPath.ToCString(), ios::binary); #else aFile = new ifstream(aFullPath.ToCString()); @@ -323,10 +335,9 @@ void LightApp_Driver::PutFilesToStream( const std::string& theModuleName, unsign theBufferSize = aBufferSize; } -//============================================================================ -// function : PutStreamToFile -/*! Purpose : converts a byte sequence to files and return list of them*/ -//============================================================================ +/*! + Converts a byte sequence to files and return list of them +*/ LightApp_Driver::ListOfFiles LightApp_Driver::PutStreamToFiles( const unsigned char* theBuffer, const long theBufferSize, bool theNamesOnly ) { @@ -336,6 +347,10 @@ LightApp_Driver::ListOfFiles LightApp_Driver::PutStreamToFiles( const unsigned c // Create a temporary directory for the component's data files std::string aDir = GetTmpDir(); + // Remember that the files are in a temporary location that should be deleted + // when a study is closed + SetIsTemporary( true ); + //Get a temporary directory for saving a file TCollection_AsciiString aTmpDir(const_cast(aDir.c_str())); @@ -366,7 +381,7 @@ LightApp_Driver::ListOfFiles LightApp_Driver::PutStreamToFiles( const unsigned c TCollection_AsciiString aFullPath = aTmpDir + aFileName; -#ifdef WNT +#ifdef WIN32 ofstream aFile(aFullPath.ToCString(), ios::out | ios::binary); #else ofstream aFile(aFullPath.ToCString()); @@ -383,13 +398,11 @@ LightApp_Driver::ListOfFiles LightApp_Driver::PutStreamToFiles( const unsigned c return aFiles; } -//============================================================================ -// function : RemoveFiles -/*! Purpose : Remove files. First item in is a directory with slash at the end. - Other items are names of files. If is true, - then the directory is also deleted. +/*! + Remove files. First item in is a directory with slash at the end. + Other items are names of files. If is true, + then the directory is also deleted. */ -//============================================================================ void LightApp_Driver::RemoveFiles( const ListOfFiles& theFiles, const bool IsDirDeleted) { int i, aLength = theFiles.size() - 1; @@ -412,17 +425,24 @@ void LightApp_Driver::RemoveFiles( const ListOfFiles& theFiles, const bool IsDir if(IsDirDeleted) { OSD_Path aPath(aDirName); OSD_Directory aDir(aPath); - OSD_FileIterator anIterator(aPath, '*'); - - if(aDir.Exists() && !anIterator.More()) aDir.Remove(); + // san -- Using a special code block below is essential - it ensures that + // OSD_FileIterator instance is destroyed by the moment when + // OSD_Directory::Remove() is called. + // Otherwise, the directory remains locked (at least on Windows) + // by the iterator and cannot be removed. + { + OSD_FileIterator anIterator(aPath, '*'); + if(!aDir.Exists() || anIterator.More()) + return; + } + aDir.Remove(); } } -//============================================================================ -// function : RemoveTemporaryFiles -/*! Purpose : removes files which was created from module theModuleName if - is true tmp directory is also deleted if it is empty*/ -//============================================================================ +/*! + Removes files which was created from module theModuleName if + is true tmp directory is also deleted if it is empty +*/ void LightApp_Driver::RemoveTemporaryFiles( const char* theModuleName, const bool IsDirDeleted ) { std::string aModuleName(theModuleName); @@ -433,25 +453,27 @@ void LightApp_Driver::RemoveTemporaryFiles( const char* theModuleName, const boo } -//============================================================================ -// function : ClearDriverContents -/*! Purpose : clear map of list files*/ -//============================================================================ +/*! + Clears map of list files +*/ void LightApp_Driver::ClearDriverContents() { std::map::iterator it; for ( it = myMap.begin(); it != myMap.end(); ++it ) { const char* aModuleName = const_cast(it->first.c_str()); - RemoveTemporaryFiles( aModuleName, false ); + // If the driver contains temporary files - + // remove them along with the temporary directory + RemoveTemporaryFiles( aModuleName, IsTemporary() ); } - myMap.clear(); + myMap.clear(); + // Reset the "temporary" flag + SetIsTemporary( false ); } -//============================================================================ -// function : GetTempDir -/*! Purpose : return a temp directory to store created files like "/tmp/sub_dir/" */ -//============================================================================ +/*! + \return a temp directory to store created files like "/tmp/sub_dir/" +*/ std::string LightApp_Driver::GetTmpDir() { if ( myTmpDir.length() != 0 ) @@ -505,15 +527,22 @@ std::string LightApp_Driver::GetTmpDir() aDir = OSD_Directory(aPath); } +#ifdef WIN32 + // Workaround for OSD_Protection bug on Windows + OSD_Protection aProtection(OSD_RWXD, OSD_RWXD, OSD_RWXD, OSD_RWXD); +#else + OSD_Protection aProtection(OSD_RX, OSD_RWXD, OSD_RX, OSD_RX); +#endif + aDir.Build(aProtection); + myTmpDir = aTmpDir.ToCString(); return aTmpDir.ToCString(); } -//============================================================================ -// function : GetDirFromPath -/*! Purpose : returns the dir by the path*/ -//============================================================================ +/*! + \return the dir by the path +*/ std::string LightApp_Driver::GetDirFromPath( const std::string& thePath ) { if(thePath == "") return "";