-#include "LightApp_Driver.h"\r
-\r
-#include <TCollection_AsciiString.hxx> \r
-\r
-#include <OSD_Path.hxx>\r
-#include <OSD_File.hxx>\r
-#include <OSD_Directory.hxx>\r
-#include <OSD_Process.hxx>\r
-#include <OSD_Directory.hxx>\r
-#include <OSD_Protection.hxx>\r
-#include <OSD_SingleProtection.hxx>\r
-#include <OSD_FileIterator.hxx>\r
-\r
-#ifdef WIN32\r
-#include <time.h>\r
-#endif\r
-\r
-/*! Constructor.*/\r
-LightApp_Driver::LightApp_Driver()\r
-{\r
-}\r
- \r
-/*! Destructor.*/\r
-LightApp_Driver::~LightApp_Driver()\r
-{\r
-}\r
-\r
-using namespace std;\r
-\r
-//================================================================\r
-// Function : SaveDatasInFile\r
-/*! Purpose : save in file 'theFileName' datas from this driver*/\r
-//================================================================\r
-bool LightApp_Driver::SaveDatasInFile( const char* theFileName, bool isMultiFile )\r
-{\r
- int aNbModules = 0;\r
- std::map<std::string, ListOfFiles>::const_iterator it;\r
- for (it = myMap.begin(); it != myMap.end(); ++it)\r
- aNbModules++;\r
-\r
- unsigned char** aBuffer = new unsigned char*[aNbModules]; \r
- long* aBufferSize = new long[aNbModules];\r
- char** aModuleName = new char*[aNbModules];\r
-\r
- if(aBuffer == NULL || aBufferSize == NULL || aModuleName == NULL)\r
- return false;\r
-\r
- int aFileBufferSize = 4; //4 bytes for a number of the modules that will be written to the stream;\r
- int i = 0;\r
- for (it = myMap.begin(); it != myMap.end(); ++it) {\r
- aModuleName[i] = const_cast<char*>(it->first.c_str());//(it->first);\r
- aFileBufferSize += 4; //Add 4 bytes: a length of the module name\r
- aFileBufferSize += strlen(aModuleName[i])+1;\r
- std::string aName(aModuleName[i]);\r
- PutFilesToStream(aName, aBuffer[i], aBufferSize[i], isMultiFile);\r
- aFileBufferSize += 8; //Add 8 bytes: a length of the buffer\r
- aFileBufferSize += aBufferSize[i];\r
- // Remove the files and tmp directory, created by the component storage procedure\r
- if (!isMultiFile)\r
- RemoveTemporaryFiles(aModuleName[i], true);\r
- i++;\r
- }\r
- int n = i;\r
-\r
- unsigned char* aFileBuffer = new unsigned char[aFileBufferSize];\r
- if(aFileBuffer == NULL)\r
- return false;\r
-\r
- int aCurrentPos = 0;\r
-\r
- //Initialize 4 bytes of the buffer by 0\r
- memset(aFileBuffer, 0, 4); \r
- //Copy the number of modules that will be written to the stream\r
- memcpy(aFileBuffer, &aNbModules, ((sizeof(int) > 4) ? 4 : sizeof(int)));\r
- aCurrentPos += 4;\r
-\r
- int aBufferNameSize = 0;\r
- for (i = 0; i < n; i++) {\r
- aBufferNameSize = strlen(aModuleName[i])+1;\r
- //Initialize 4 bytes of the buffer by 0\r
- memset((aFileBuffer + aCurrentPos), 0, 4); \r
- //Copy the length of the module name to the buffer\r
- memcpy((aFileBuffer + aCurrentPos), &aBufferNameSize, ((sizeof(int) > 4) ? 4 : sizeof(int))); \r
- aCurrentPos += 4;\r
- //Copy the module name to the buffer\r
- memcpy((aFileBuffer + aCurrentPos), aModuleName[i], aBufferNameSize);\r
- aCurrentPos += aBufferNameSize;\r
-\r
- //Initialize 8 bytes of the buffer by 0\r
- memset((aFileBuffer + aCurrentPos), 0, 8);\r
- //Copy the length of the module buffer to the buffer\r
- memcpy((aFileBuffer + aCurrentPos), (aBufferSize + i), ((sizeof(long) > 8) ? 8 : sizeof(long)));\r
- aCurrentPos += 8;\r
- //Copy the module buffer to the buffer\r
- memcpy((aFileBuffer + aCurrentPos), aBuffer[i], aBufferSize[i]);\r
- aCurrentPos += aBufferSize[i];\r
- }\r
-\r
- ofstream aFile(theFileName);\r
- aFile.write((char*)aFileBuffer, aFileBufferSize); \r
- aFile.close(); \r
-\r
- delete[] aBuffer;\r
- delete[] aBufferSize;\r
- delete[] aModuleName;\r
- delete[] aFileBuffer;\r
- return true;\r
-}\r
-\r
-//=======================================================================\r
-// name : ReaDatasFromFile\r
-/*! Purpose : filling current driver from file 'theFileName'*/\r
-//=======================================================================\r
-bool LightApp_Driver::ReadDatasFromFile( const char* theFileName, bool isMultiFile )\r
-{\r
-#ifdef WNT\r
- ifstream aFile(theFileName, ios::binary);\r
-#else\r
- ifstream aFile(theFileName);\r
-#endif \r
-\r
- aFile.seekg(0, ios::end);\r
- int aFileBufferSize = aFile.tellg();\r
- unsigned char* aFileBuffer = new unsigned char[aFileBufferSize];\r
- aFile.seekg(0, ios::beg);\r
- aFile.read((char*)aFileBuffer, aFileBufferSize);\r
- aFile.close();\r
-\r
- int aNbModules = 0;\r
- //Copy the number of files in the stream\r
- memcpy(&aNbModules, aFileBuffer, sizeof(int));\r
- long aCurrentPos = 4;\r
- int aModuleNameSize;\r
-\r
- for (int i = 0; i < aNbModules; i++) {\r
- //Put a length of the module name to aModuleNameSize\r
- memcpy(&aModuleNameSize, (aFileBuffer + aCurrentPos), ((sizeof(int) > 4) ? 4 : sizeof(int))); \r
- aCurrentPos += 4;\r
-\r
- char *aModuleName = new char[aModuleNameSize];\r
- //Put a module name to aModuleName\r
- memcpy(aModuleName, (aFileBuffer + aCurrentPos), aModuleNameSize); \r
- aCurrentPos += aModuleNameSize;\r
-\r
- //Put a length of the file buffer to aBufferSize\r
- long aBufferSize;\r
- memcpy(&aBufferSize, (aFileBuffer + aCurrentPos), ((sizeof(long) > 8) ? 8 : sizeof(long))); \r
- aCurrentPos += 8;\r
- unsigned char *aBuffer = new unsigned char[aBufferSize];\r
- \r
- //Put a buffer for current module to aBuffer\r
- memcpy(aBuffer, (aFileBuffer + aCurrentPos), aBufferSize); \r
- aCurrentPos += aBufferSize;\r
-\r
- // Put buffer to aListOfFiles and set to myMap\r
- ListOfFiles aListOfFiles = PutStreamToFiles(aBuffer, aBufferSize, isMultiFile);\r
- SetListOfFiles(aModuleName, aListOfFiles);\r
-\r
- delete[] aModuleName;\r
- delete[] aBuffer;\r
- }\r
- delete[] aFileBuffer;\r
- return true;\r
-}\r
-\r
-//================================================================\r
-// Function : GetTmpDir\r
-/*! Purpose : returns temp directory for path 'theURL'*/\r
-//================================================================\r
-std::string LightApp_Driver::GetTmpDir (const char* theURL, const bool isMultiFile)\r
-{\r
- std::string anURLDir = GetDirFromPath(theURL);\r
- std::string aTmpDir = isMultiFile ? anURLDir : GetTmpDir();\r
-\r
- return aTmpDir;\r
-}\r
-\r
-//================================================================\r
-// Function : GetListOfFiles\r
-/*! Purpose : returns list of files for module with name 'theModuleName'*/\r
-//================================================================\r
-LightApp_Driver::ListOfFiles LightApp_Driver::GetListOfFiles( const char* theModuleName )\r
-{\r
- ListOfFiles aListOfFiles;\r
-\r
- std::string aName(theModuleName);\r
- if (myMap.count(aName))\r
- aListOfFiles = myMap[aName];\r
-\r
- return aListOfFiles;\r
-}\r
-\r
-//================================================================\r
-// Function : SetListOfFiles\r
-/*! Purpose : sets list of files for module with name 'theModuleName'*/\r
-//================================================================\r
-void LightApp_Driver::SetListOfFiles( const char* theModuleName, const ListOfFiles theListOfFiles )\r
-{\r
- std::string aName (theModuleName);\r
- myMap[aName] = theListOfFiles;\r
-}\r
-\r
-//============================================================================\r
-// function : PutFilesToStream\r
-/*! Purpose : converts files which was created from module <theModuleName> into a byte sequence unsigned char*/\r
-//============================================================================\r
-void LightApp_Driver::PutFilesToStream( const std::string& theModuleName, unsigned char*& theBuffer,\r
- long& theBufferSize, bool theNamesOnly )\r
-{\r
- ListOfFiles aFiles = myMap[theModuleName];\r
- // aFiles must contain temporary directory name in its first item\r
- // and names of files (relatively the temporary directory) in the others\r
-\r
- int i, aLength = aFiles.size() - 1;\r
- if(aLength <= 0) {\r
- theBufferSize = 0;\r
- theBuffer = new unsigned char[theBufferSize];\r
- return;\r
- }\r
- //Get a temporary directory for saved a file\r
- TCollection_AsciiString aTmpDir(const_cast<char*>(aFiles[0].c_str()));\r
-\r
- long aBufferSize = 0;\r
- long aCurrentPos;\r
- int aNbFiles = 0;\r
- int* aFileNameSize= new int[aLength];\r
- long* aFileSize= new long[aLength];\r
-\r
- //Determine the required size of the buffer\r
- TCollection_AsciiString aFileName;\r
- for (i = 0; i < aLength; i++) {\r
- char* aFName = const_cast<char*>(aFiles[i+1].c_str());\r
- aFileName = aFName;\r
- //Check if the file exists\r
- if (!theNamesOnly) { // mpv 15.01.2003: if only file names must be stroed, then size of files is zero\r
- TCollection_AsciiString aFullPath = aTmpDir + aFileName; \r
- OSD_Path anOSDPath(aFullPath);\r
- OSD_File anOSDFile(anOSDPath);\r
- if(!anOSDFile.Exists()) continue;\r
-#ifdef WNT\r
- ifstream aFile(aFullPath.ToCString(), ios::binary);\r
-#else\r
- ifstream aFile(aFullPath.ToCString());\r
-#endif\r
- aFile.seekg(0, ios::end);\r
- aFileSize[i] = aFile.tellg();\r
- aBufferSize += aFileSize[i]; //Add a space to store the file\r
- }\r
- aFileNameSize[i] = strlen(aFName) + 1;\r
- aBufferSize += aFileNameSize[i]; //Add a space to store the file name\r
- aBufferSize += (theNamesOnly)?4:12; //Add 4 bytes: a length of the file name,\r
- // 8 bytes: length of the file itself\r
- aNbFiles++;\r
- delete[] aFName;\r
- }\r
-\r
- aBufferSize += 4; //4 bytes for a number of the files that will be written to the stream;\r
- theBuffer = new unsigned char[aBufferSize]; \r
- if(theBuffer == NULL) {\r
- theBufferSize = 0;\r
- theBuffer = 0;\r
- return;\r
- }\r
- //Initialize 4 bytes of the buffer by 0\r
- memset(theBuffer, 0, 4); \r
- //Copy the number of files that will be written to the stream\r
- memcpy(theBuffer, &aNbFiles, ((sizeof(int) > 4) ? 4 : sizeof(int))); \r
-\r
- aCurrentPos = 4;\r
-\r
- for(i = 0; i < aLength; i++) {\r
- ifstream *aFile;\r
- if (!theNamesOnly) { // mpv 15.01.2003: we don't open any file if theNamesOnly = true\r
- TCollection_AsciiString aName(const_cast<char*>(aFiles[i+1].c_str()));\r
- TCollection_AsciiString aFullPath = aTmpDir + aName;\r
- OSD_Path anOSDPath(aFullPath);\r
- OSD_File anOSDFile(anOSDPath);\r
- if(!anOSDFile.Exists()) continue;\r
-#ifdef WNT\r
- aFile = new ifstream(aFullPath.ToCString(), ios::binary);\r
-#else\r
- aFile = new ifstream(aFullPath.ToCString());\r
-#endif\r
- }\r
- //Initialize 4 bytes of the buffer by 0\r
- memset((theBuffer + aCurrentPos), 0, 4); \r
- //Copy the length of the file name to the buffer\r
- memcpy((theBuffer + aCurrentPos), (aFileNameSize + i), ((sizeof(int) > 4) ? 4 : sizeof(int))); \r
- aCurrentPos += 4;\r
-\r
- //Copy the file name to the buffer\r
- char* aFName = const_cast<char*>(aFiles[i+1].c_str());\r
- memcpy((theBuffer + aCurrentPos), aFName, aFileNameSize[i]);\r
- aCurrentPos += aFileNameSize[i];\r
- \r
- if (!theNamesOnly) { // mpv 15.01.2003: we don't copy file content to the buffer if !theNamesOnly\r
- //Initialize 8 bytes of the buffer by 0\r
- memset((theBuffer + aCurrentPos), 0, 8); \r
- //Copy the length of the file to the buffer\r
- memcpy((theBuffer + aCurrentPos), (aFileSize + i), ((sizeof(long) > 8) ? 8 : sizeof(long)));\r
- aCurrentPos += 8;\r
- \r
- aFile->seekg(0, ios::beg);\r
- aFile->read((char *)(theBuffer + aCurrentPos), aFileSize[i]);\r
- aFile->close();\r
- delete(aFile);\r
- aCurrentPos += aFileSize[i];\r
- }\r
- }\r
- delete[] aFileNameSize;\r
- delete[] aFileSize;\r
-\r
- theBufferSize = aBufferSize;\r
-}\r
-\r
-//============================================================================\r
-// function : PutStreamToFile\r
-/*! Purpose : converts a byte sequence <theBuffer> to files and return list of them*/\r
-//============================================================================\r
-LightApp_Driver::ListOfFiles LightApp_Driver::PutStreamToFiles( const unsigned char* theBuffer,\r
- const long theBufferSize, bool theNamesOnly )\r
-{\r
- if(theBufferSize == 0 || theBuffer == 0)\r
- return ListOfFiles();\r
-\r
- // Create a temporary directory for the component's data files\r
- std::string aDir = GetTmpDir();\r
-\r
- //Get a temporary directory for saving a file\r
- TCollection_AsciiString aTmpDir(const_cast<char*>(aDir.c_str()));\r
-\r
- long aFileSize, aCurrentPos = 4;\r
- int i, aFileNameSize, aNbFiles = 0;\r
-\r
- //Copy the number of files in the stream\r
- memcpy(&aNbFiles, theBuffer, sizeof(int)); \r
-\r
- const int n = aNbFiles + 1;\r
- ListOfFiles aFiles(n);\r
- aFiles[0] = aDir;\r
-\r
- for(i = 0; i < aNbFiles; i++) {\r
- //Put a length of the file name to aFileNameSize\r
- memcpy(&aFileNameSize, (theBuffer + aCurrentPos), ((sizeof(int) > 4) ? 4 : sizeof(int))); \r
- aCurrentPos += 4;\r
-\r
- char *aFileName = new char[aFileNameSize];\r
- //Put a file name to aFileName\r
- memcpy(aFileName, (theBuffer + aCurrentPos), aFileNameSize); \r
- aCurrentPos += aFileNameSize;\r
- \r
- //Put a length of the file to aFileSize\r
- if (!theNamesOnly) {\r
- memcpy(&aFileSize, (theBuffer + aCurrentPos), ((sizeof(long) > 8) ? 8 : sizeof(long)));\r
- aCurrentPos += 8; \r
- \r
- TCollection_AsciiString aFullPath = aTmpDir + aFileName;\r
- ofstream aFile(aFullPath.ToCString());\r
- aFile.write((char *)(theBuffer+aCurrentPos), aFileSize); \r
- aFile.close(); \r
- aCurrentPos += aFileSize;\r
- }\r
- std::string aStrFileName(aFileName);\r
- aFiles[i+1] = aStrFileName;\r
- delete[] aFileName;\r
- }\r
- return aFiles;\r
-}\r
-\r
-//============================================================================\r
-// function : RemoveTemporaryFiles\r
-/*! Purpose : removes files which was created from module theModuleName if <IsDirDeleted> is true tmp directory is also deleted if it is empty*/\r
-//============================================================================\r
-void LightApp_Driver::RemoveTemporaryFiles( const char* theModuleName, const bool IsDirDeleted )\r
-{\r
- std::string aModuleName(theModuleName);\r
- ListOfFiles aFiles = myMap[aModuleName];\r
- // aFiles must contain temporary directory name in its first item\r
- // and names of files (relatively the temporary directory) in the others\r
-\r
- int i, aLength = aFiles.size() - 1;\r
- if(aLength <= 0) {\r
- return;\r
- }\r
- //Get a temporary directory for saved a file\r
- TCollection_AsciiString aDirName(const_cast<char*>(aFiles[0].c_str()));\r
-\r
- for(i = 0; i < aLength; i++) {\r
- TCollection_AsciiString aFile(aDirName);\r
- aFile += const_cast<char*>(aFiles[i+1].c_str());\r
- OSD_Path anOSDPath(aFile);\r
- OSD_File anOSDFile(anOSDPath);\r
- if(!anOSDFile.Exists()) continue;\r
-\r
- OSD_Protection aProtection = anOSDFile.Protection();\r
- aProtection.SetUser(OSD_RW);\r
- anOSDFile.SetProtection(aProtection);\r
-\r
- anOSDFile.Remove();\r
- }\r
-\r
- if(IsDirDeleted) {\r
- OSD_Path aPath(aDirName);\r
- OSD_Directory aDir(aPath);\r
- OSD_FileIterator anIterator(aPath, '*');\r
-\r
- if(aDir.Exists() && !anIterator.More()) aDir.Remove();\r
- }\r
-}\r
-\r
-//============================================================================\r
-// function : ClearDriverContents\r
-/*! Purpose : clear map of list files*/ \r
-//============================================================================ \r
-void LightApp_Driver::ClearDriverContents()\r
-{\r
- myMap.clear();\r
-}\r
-\r
-//============================================================================\r
-// function : GetTempDir\r
-/*! Purpose : return a temp directory to store created files like "/tmp/sub_dir/" */\r
-//============================================================================ \r
-std::string LightApp_Driver::GetTmpDir()\r
-{\r
- //Find a temporary directory to store a file\r
- TCollection_AsciiString aTmpDir;\r
-\r
- char *Tmp_dir = getenv("SALOME_TMP_DIR");\r
- if(Tmp_dir != NULL) {\r
- aTmpDir = TCollection_AsciiString(Tmp_dir);\r
-#ifdef WIN32\r
- if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';\r
-#else\r
- if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';\r
-#endif \r
- }\r
- else {\r
-#ifdef WIN32\r
- aTmpDir = TCollection_AsciiString("C:\\");\r
-#else\r
- aTmpDir = TCollection_AsciiString("/tmp/");\r
-#endif\r
- }\r
-\r
- srand((unsigned int)time(NULL));\r
- int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory\r
- TCollection_AsciiString aSubDir(aRND);\r
- if(aSubDir.Length() <= 1) aSubDir = TCollection_AsciiString("123409876");\r
-\r
- aTmpDir += aSubDir; //Get RND sub directory\r
-\r
-#ifdef WIN32\r
- if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';\r
-#else\r
- if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';\r
-#endif\r
-\r
- OSD_Path aPath(aTmpDir);\r
- OSD_Directory aDir(aPath);\r
-\r
- for(aRND = 0; aDir.Exists(); aRND++) {\r
- aTmpDir.Insert((aTmpDir.Length() - 1), TCollection_AsciiString(aRND)); //Build a unique directory name\r
- aPath = OSD_Path(aTmpDir);\r
- aDir = OSD_Directory(aPath);\r
- }\r
-\r
- OSD_Protection aProtection(OSD_RW, OSD_RWX, OSD_RX, OSD_RX);\r
- aDir.Build(aProtection);\r
-\r
- return aTmpDir.ToCString();\r
-}\r
-\r
-//============================================================================\r
-// function : GetDirFromPath\r
-/*! Purpose : returns the dir by the path*/\r
-//============================================================================\r
-std::string LightApp_Driver::GetDirFromPath( const std::string& thePath ) {\r
- if(thePath == "")\r
- return "";\r
- OSD_Path aPath = OSD_Path(TCollection_AsciiString(const_cast<char*>(thePath.c_str())));\r
- TCollection_AsciiString aDirString(aPath.Trek());\r
- aDirString.ChangeAll('|','/');\r
- return aDirString.ToCString();\r
-}\r
-\r
+#include "LightApp_Driver.h"
+
+#include <TCollection_AsciiString.hxx>
+
+#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>
+
+#include <qfileinfo.h>
+#include <qdir.h>
+
+#ifdef WIN32
+#include <time.h>
+#endif
+
+/*! Constructor.*/
+LightApp_Driver::LightApp_Driver()
+{
+}
+
+/*! Destructor.*/
+LightApp_Driver::~LightApp_Driver()
+{
+}
+
+using namespace std;
+
+//================================================================
+// Function : SaveDatasInFile
+/*! Purpose : save in file 'theFileName' datas from this driver*/
+//================================================================
+bool LightApp_Driver::SaveDatasInFile( const char* theFileName, bool isMultiFile )
+{
+ int aNbModules = 0;
+ std::map<std::string, ListOfFiles>::const_iterator it;
+ for (it = myMap.begin(); it != myMap.end(); ++it)
+ aNbModules++;
+
+ unsigned char** aBuffer = new unsigned char*[aNbModules];
+ long* aBufferSize = new long[aNbModules];
+ char** aModuleName = new char*[aNbModules];
+
+ if(aBuffer == NULL || aBufferSize == NULL || aModuleName == NULL)
+ return false;
+
+ int aFileBufferSize = 4; //4 bytes for a number of the modules that will be written to the stream;
+ int i = 0;
+ for (it = myMap.begin(); it != myMap.end(); ++it) {
+ aModuleName[i] = const_cast<char*>(it->first.c_str());//(it->first);
+ aFileBufferSize += 4; //Add 4 bytes: a length of the module name
+ aFileBufferSize += strlen(aModuleName[i])+1;
+ std::string aName(aModuleName[i]);
+ PutFilesToStream(aName, aBuffer[i], aBufferSize[i], isMultiFile);
+ aFileBufferSize += 8; //Add 8 bytes: a length of the buffer
+ aFileBufferSize += aBufferSize[i];
+ i++;
+ }
+ int n = i;
+
+ unsigned char* aFileBuffer = new unsigned char[aFileBufferSize];
+ if(aFileBuffer == NULL)
+ return false;
+
+ myTmpDir = QDir::convertSeparators( QFileInfo( theFileName ).dirPath( true ) + "/" ).latin1() ;
+
+ int aCurrentPos = 0;
+
+ //Initialize 4 bytes of the buffer by 0
+ memset(aFileBuffer, 0, 4);
+ //Copy the number of modules that will be written to the stream
+ memcpy(aFileBuffer, &aNbModules, ((sizeof(int) > 4) ? 4 : sizeof(int)));
+ aCurrentPos += 4;
+
+ int aBufferNameSize = 0;
+ for (i = 0; i < n; i++) {
+ aBufferNameSize = strlen(aModuleName[i])+1;
+ //Initialize 4 bytes of the buffer by 0
+ memset((aFileBuffer + aCurrentPos), 0, 4);
+ //Copy the length of the module name to the buffer
+ memcpy((aFileBuffer + aCurrentPos), &aBufferNameSize, ((sizeof(int) > 4) ? 4 : sizeof(int)));
+ aCurrentPos += 4;
+ //Copy the module name to the buffer
+ memcpy((aFileBuffer + aCurrentPos), aModuleName[i], aBufferNameSize);
+ aCurrentPos += aBufferNameSize;
+
+ //Initialize 8 bytes of the buffer by 0
+ memset((aFileBuffer + aCurrentPos), 0, 8);
+ //Copy the length of the module buffer to the buffer
+ memcpy((aFileBuffer + aCurrentPos), (aBufferSize + i), ((sizeof(long) > 8) ? 8 : sizeof(long)));
+ aCurrentPos += 8;
+ //Copy the module buffer to the buffer
+ memcpy((aFileBuffer + aCurrentPos), aBuffer[i], aBufferSize[i]);
+ aCurrentPos += aBufferSize[i];
+ }
+
+#ifdef WNT
+ ofstream aFile(theFileName, ios::out | ios::binary);
+#else
+ ofstream aFile(theFileName);
+#endif
+ aFile.write((char*)aFileBuffer, aFileBufferSize);
+ aFile.close();
+
+ delete[] aBuffer;
+ delete[] aBufferSize;
+ delete[] aModuleName;
+ delete[] aFileBuffer;
+
+ return true;
+}
+
+//=======================================================================
+// name : ReaDatasFromFile
+/*! Purpose : filling current driver from file 'theFileName'*/
+//=======================================================================
+bool LightApp_Driver::ReadDatasFromFile( const char* theFileName, bool isMultiFile )
+{
+#ifdef WNT
+ ifstream aFile(theFileName, ios::binary);
+#else
+ ifstream aFile(theFileName);
+#endif
+
+ myTmpDir = QDir::convertSeparators( QFileInfo( theFileName ).dirPath( true ) + "/" ).latin1() ;
+
+ aFile.seekg(0, ios::end);
+ int aFileBufferSize = aFile.tellg();
+ unsigned char* aFileBuffer = new unsigned char[aFileBufferSize];
+ aFile.seekg(0, ios::beg);
+ aFile.read((char*)aFileBuffer, aFileBufferSize);
+ aFile.close();
+
+ int aNbModules = 0;
+ //Copy the number of files in the stream
+ memcpy(&aNbModules, aFileBuffer, sizeof(int));
+ long aCurrentPos = 4;
+ int aModuleNameSize;
+
+ for (int i = 0; i < aNbModules; i++) {
+ //Put a length of the module name to aModuleNameSize
+ memcpy(&aModuleNameSize, (aFileBuffer + aCurrentPos), ((sizeof(int) > 4) ? 4 : sizeof(int)));
+ aCurrentPos += 4;
+
+ char *aModuleName = new char[aModuleNameSize];
+ //Put a module name to aModuleName
+ memcpy(aModuleName, (aFileBuffer + aCurrentPos), aModuleNameSize);
+ aCurrentPos += aModuleNameSize;
+
+ //Put a length of the file buffer to aBufferSize
+ long aBufferSize;
+ memcpy(&aBufferSize, (aFileBuffer + aCurrentPos), ((sizeof(long) > 8) ? 8 : sizeof(long)));
+ aCurrentPos += 8;
+ unsigned char *aBuffer = new unsigned char[aBufferSize];
+
+ //Put a buffer for current module to aBuffer
+ memcpy(aBuffer, (aFileBuffer + aCurrentPos), aBufferSize);
+ aCurrentPos += aBufferSize;
+
+ // Put buffer to aListOfFiles and set to myMap
+ ListOfFiles aListOfFiles = PutStreamToFiles(aBuffer, aBufferSize, isMultiFile);
+ SetListOfFiles(aModuleName, aListOfFiles);
+
+ delete[] aModuleName;
+ delete[] aBuffer;
+ }
+
+ delete[] aFileBuffer;
+
+ return true;
+}
+
+//================================================================
+// Function : GetTmpDir
+/*! Purpose : returns temp directory for path 'theURL'*/
+//================================================================
+std::string LightApp_Driver::GetTmpDir (const char* theURL, const bool isMultiFile)
+{
+ std::string anURLDir = GetDirFromPath(theURL);
+ std::string aTmpDir = isMultiFile ? anURLDir : GetTmpDir();
+
+ return aTmpDir;
+}
+
+//================================================================
+// Function : GetListOfFiles
+/*! Purpose : returns list of files for module with name 'theModuleName'*/
+//================================================================
+LightApp_Driver::ListOfFiles LightApp_Driver::GetListOfFiles( const char* theModuleName )
+{
+ ListOfFiles aListOfFiles;
+
+ std::string aName(theModuleName);
+ if (myMap.count(aName))
+ aListOfFiles = myMap[aName];
+
+ return aListOfFiles;
+}
+
+//================================================================
+// Function : SetListOfFiles
+/*! Purpose : 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 <theModuleName> into a byte sequence unsigned char*/
+//============================================================================
+void LightApp_Driver::PutFilesToStream( const std::string& theModuleName, unsigned char*& theBuffer,
+ long& theBufferSize, bool theNamesOnly )
+{
+ ListOfFiles aFiles = myMap[theModuleName];
+ // aFiles must contain temporary directory name in its first item
+ // and names of files (relatively the temporary directory) in the others
+
+ int i, aLength = aFiles.size() - 1;
+ if(aLength <= 0) {
+ theBufferSize = 0;
+ theBuffer = new unsigned char[theBufferSize];
+ return;
+ }
+ //Get a temporary directory for saved a file
+ TCollection_AsciiString aTmpDir(const_cast<char*>(aFiles[0].c_str()));
+
+ long aBufferSize = 0;
+ long aCurrentPos;
+ int aNbFiles = 0;
+ int* aFileNameSize= new int[aLength];
+ long* aFileSize= new long[aLength];
+
+ //Determine the required size of the buffer
+ TCollection_AsciiString aFileName;
+ for (i = 0; i < aLength; i++) {
+ char* aFName = const_cast<char*>(aFiles[i+1].c_str());
+ aFileName = aFName;
+ //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 + aFileName;
+ OSD_Path anOSDPath(aFullPath);
+ OSD_File anOSDFile(anOSDPath);
+ if(!anOSDFile.Exists()) continue;
+#ifdef WNT
+ ifstream aFile(aFullPath.ToCString(), ios::binary);
+#else
+ ifstream aFile(aFullPath.ToCString());
+#endif
+ aFile.seekg(0, ios::end);
+ aFileSize[i] = aFile.tellg();
+ aBufferSize += aFileSize[i]; //Add a space to store the file
+ }
+ aFileNameSize[i] = strlen(aFName) + 1;
+ 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++;
+ }
+
+ aBufferSize += 4; //4 bytes for a number of the files that will be written to the stream;
+ theBuffer = new unsigned char[aBufferSize];
+ if(theBuffer == NULL) {
+ theBufferSize = 0;
+ theBuffer = 0;
+ return;
+ }
+ //Initialize 4 bytes of the buffer by 0
+ memset(theBuffer, 0, 4);
+ //Copy the number of files that will be written to the stream
+ memcpy(theBuffer, &aNbFiles, ((sizeof(int) > 4) ? 4 : sizeof(int)));
+
+ aCurrentPos = 4;
+
+ for(i = 0; i < aLength; i++) {
+ ifstream *aFile;
+ if (!theNamesOnly) { // mpv 15.01.2003: we don't open any file if theNamesOnly = true
+ TCollection_AsciiString aName(const_cast<char*>(aFiles[i+1].c_str()));
+ TCollection_AsciiString aFullPath = aTmpDir + aName;
+ OSD_Path anOSDPath(aFullPath);
+ OSD_File anOSDFile(anOSDPath);
+ if(!anOSDFile.Exists()) continue;
+#ifdef WNT
+ aFile = new ifstream(aFullPath.ToCString(), ios::binary);
+#else
+ aFile = new ifstream(aFullPath.ToCString());
+#endif
+ }
+ //Initialize 4 bytes of the buffer by 0
+ memset((theBuffer + aCurrentPos), 0, 4);
+ //Copy the length of the file name to the buffer
+ memcpy((theBuffer + aCurrentPos), (aFileNameSize + i), ((sizeof(int) > 4) ? 4 : sizeof(int)));
+ aCurrentPos += 4;
+
+ //Copy the file name to the buffer
+ char* aFName = const_cast<char*>(aFiles[i+1].c_str());
+ memcpy((theBuffer + aCurrentPos), aFName, 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((theBuffer + aCurrentPos), 0, 8);
+ //Copy the length of the file to the buffer
+ memcpy((theBuffer + aCurrentPos), (aFileSize + i), ((sizeof(long) > 8) ? 8 : sizeof(long)));
+ aCurrentPos += 8;
+
+ aFile->seekg(0, ios::beg);
+ aFile->read((char *)(theBuffer + aCurrentPos), aFileSize[i]);
+ aFile->close();
+ delete(aFile);
+ aCurrentPos += aFileSize[i];
+ }
+ }
+ delete[] aFileNameSize;
+ delete[] aFileSize;
+
+ theBufferSize = aBufferSize;
+}
+
+//============================================================================
+// function : PutStreamToFile
+/*! Purpose : converts a byte sequence <theBuffer> to files and return list of them*/
+//============================================================================
+LightApp_Driver::ListOfFiles LightApp_Driver::PutStreamToFiles( const unsigned char* theBuffer,
+ const long theBufferSize, bool theNamesOnly )
+{
+ if(theBufferSize == 0 || theBuffer == 0)
+ return ListOfFiles();
+
+ // Create a temporary directory for the component's data files
+ std::string aDir = GetTmpDir();
+
+ //Get a temporary directory for saving a file
+ TCollection_AsciiString aTmpDir(const_cast<char*>(aDir.c_str()));
+
+ long aFileSize, aCurrentPos = 4;
+ int i, aFileNameSize, aNbFiles = 0;
+
+ //Copy the number of files in the stream
+ memcpy(&aNbFiles, theBuffer, sizeof(int));
+
+ const int n = aNbFiles + 1;
+ ListOfFiles aFiles(n);
+ aFiles[0] = aDir;
+
+ for(i = 0; i < aNbFiles; i++) {
+ //Put a length of the file name to aFileNameSize
+ memcpy(&aFileNameSize, (theBuffer + aCurrentPos), ((sizeof(int) > 4) ? 4 : sizeof(int)));
+ aCurrentPos += 4;
+
+ char *aFileName = new char[aFileNameSize];
+ //Put a file name to aFileName
+ memcpy(aFileName, (theBuffer + aCurrentPos), aFileNameSize);
+ aCurrentPos += aFileNameSize;
+
+ //Put a length of the file to aFileSize
+ if (!theNamesOnly) {
+ memcpy(&aFileSize, (theBuffer + aCurrentPos), ((sizeof(long) > 8) ? 8 : sizeof(long)));
+ aCurrentPos += 8;
+
+ TCollection_AsciiString aFullPath = aTmpDir + aFileName;
+
+#ifdef WNT
+ ofstream aFile(aFullPath.ToCString(), ios::out | ios::binary);
+#else
+ ofstream aFile(aFullPath.ToCString());
+#endif
+
+ aFile.write((char *)(theBuffer+aCurrentPos), aFileSize);
+ aFile.close();
+ aCurrentPos += aFileSize;
+ }
+ std::string aStrFileName(aFileName);
+ aFiles[i+1] = aStrFileName;
+ delete[] aFileName;
+ }
+ return aFiles;
+}
+
+//============================================================================
+// function : RemoveFiles
+/*! Purpose : Remove files. First item in <theFiles> is a directory with slash at the end.
+ Other items are names of files. If <IsDirDeleted> is true,
+ then the directory is also deleted.
+*/
+//============================================================================
+void LightApp_Driver::RemoveFiles( const ListOfFiles& theFiles, const bool IsDirDeleted)
+{
+ int i, aLength = theFiles.size() - 1;
+ if(aLength <= 0) {
+ return;
+ }
+ //Get a temporary directory for saved a file
+ TCollection_AsciiString aDirName(const_cast<char*>(theFiles[0].c_str()));
+
+ for(i = 0; i < aLength; i++) {
+ TCollection_AsciiString aFile(aDirName);
+ aFile += const_cast<char*>(theFiles[i+1].c_str());
+ OSD_Path anOSDPath(aFile);
+ OSD_File anOSDFile(anOSDPath);
+ if(!anOSDFile.Exists()) continue;
+
+ anOSDFile.Remove();
+ }
+
+ if(IsDirDeleted) {
+ OSD_Path aPath(aDirName);
+ OSD_Directory aDir(aPath);
+ OSD_FileIterator anIterator(aPath, '*');
+
+ if(aDir.Exists() && !anIterator.More()) aDir.Remove();
+ }
+}
+
+//============================================================================
+// function : RemoveTemporaryFiles
+/*! Purpose : removes files which was created from module theModuleName if
+ <IsDirDeleted> 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);
+ ListOfFiles aFiles = myMap[aModuleName];
+ // aFiles must contain temporary directory name in its first item
+ // and names of files (relatively the temporary directory) in the others
+ RemoveFiles( aFiles, IsDirDeleted );
+
+}
+
+//============================================================================
+// function : ClearDriverContents
+/*! Purpose : clear map of list files*/
+//============================================================================
+void LightApp_Driver::ClearDriverContents()
+{
+ std::map<std::string, ListOfFiles>::iterator it;
+ for ( it = myMap.begin(); it != myMap.end(); ++it )
+ {
+ const char* aModuleName = const_cast<char*>(it->first.c_str());
+ RemoveTemporaryFiles( aModuleName, false );
+ }
+ myMap.clear();
+}
+
+//============================================================================
+// function : GetTempDir
+/*! Purpose : return a temp directory to store created files like "/tmp/sub_dir/" */
+//============================================================================
+std::string LightApp_Driver::GetTmpDir()
+{
+ if ( myTmpDir.length() != 0 )
+ return myTmpDir;
+
+ //Find a temporary directory to store a file
+ TCollection_AsciiString aTmpDir;
+
+ char *Tmp_dir = getenv("SALOME_TMP_DIR");
+ if ( !Tmp_dir )
+ Tmp_dir = getenv ( "TEMP" );
+ if ( !Tmp_dir )
+ Tmp_dir = getenv ( "TMP" );
+ if ( Tmp_dir )
+ {
+ aTmpDir = TCollection_AsciiString(Tmp_dir);
+#ifdef WIN32
+ if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
+#else
+ if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
+#endif
+ }
+ else
+ {
+#ifdef WIN32
+ aTmpDir = TCollection_AsciiString("C:\\");
+#else
+ aTmpDir = TCollection_AsciiString("/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");
+
+ aTmpDir += aSubDir; //Get RND sub directory
+
+#ifdef WIN32
+ if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
+#else
+ if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
+#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);
+ }
+
+ myTmpDir = aTmpDir.ToCString();
+
+ return aTmpDir.ToCString();
+}
+
+//============================================================================
+// function : GetDirFromPath
+/*! Purpose : returns the dir by the path*/
+//============================================================================
+std::string LightApp_Driver::GetDirFromPath( const std::string& thePath ) {
+ if(thePath == "")
+ return "";
+ OSD_Path aPath = OSD_Path(TCollection_AsciiString(const_cast<char*>(thePath.c_str())));
+ TCollection_AsciiString aDirString(aPath.Trek());
+ aDirString.ChangeAll('|','/');
+ return aDirString.ToCString();
+}
+