Back to index

salome-gui  6.5.0
LightApp_HDFDriver.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "LightApp_HDFDriver.h"
00024 
00025 #include "HDFOI.hxx"
00026 
00027 // OCCT Includes
00028 #include <TCollection_AsciiString.hxx>
00029 
00031 LightApp_HDFDriver::LightApp_HDFDriver()
00032 {
00033 }
00034 
00036 LightApp_HDFDriver::~LightApp_HDFDriver()
00037 {
00038 }
00039 
00043 bool LightApp_HDFDriver::SaveDatasInFile( const char* theFileName, bool isMultiFile )
00044 {
00045   bool isASCII = false;
00046   bool isError = false;
00047 
00048   HDFfile *hdf_file = 0;
00049   HDFgroup *hdf_group_datacomponent = 0;
00050   HDFgroup *hdf_group_study_structure = 0;
00051   HDFgroup *hdf_sco_group  = 0;
00052   HDFgroup *hdf_sco_group2 = 0;
00053   HDFdataset *hdf_dataset = 0;
00054   hdf_size aHDFSize[1];
00055 
00056   try {
00057     hdf_file = new HDFfile ((char*)theFileName);
00058     hdf_file->CreateOnDisk();
00059 
00060     //-----------------------------------------------------------------------
00061     // 1 - Create a groupe for each SComponent and Update the PersistanceRef
00062     //-----------------------------------------------------------------------
00063     hdf_group_datacomponent = new HDFgroup ("DATACOMPONENT", hdf_file);
00064     hdf_group_datacomponent->CreateOnDisk();
00065 
00066     std::map<std::string, std::string> mapNameEntry;
00067 
00068     int tag = 1;
00069     std::map<std::string, ListOfFiles>::const_iterator it;
00070     for (it = myMap.begin(); it != myMap.end(); ++it, ++tag) {
00071       std::string aName (it->first);
00072       char* aModuleName = const_cast<char*>(aName.c_str());
00073       unsigned char* aBuffer;
00074       long           aBufferSize;
00075       PutFilesToStream(aName, aBuffer, aBufferSize, isMultiFile);
00076 
00077       //Handle(SALOMEDSImpl_SComponent) sco = itcomponent.Value();
00078       //TCollection_AsciiString scoid = sco->GetID();
00079       //hdf_sco_group = new HDFgroup(scoid.ToCString(), hdf_group_datacomponent);
00080 
00081       TCollection_AsciiString entry ("0:1:");
00082       entry += TCollection_AsciiString(tag);
00083       mapNameEntry[aModuleName] = entry.ToCString();
00084 
00085       //hdf_sco_group = new HDFgroup (aModuleName, hdf_group_datacomponent);
00086       hdf_sco_group = new HDFgroup (entry.ToCString(), hdf_group_datacomponent);
00087       hdf_sco_group->CreateOnDisk();
00088 
00089       aHDFSize[0] = aBufferSize;
00090 
00091       hdf_dataset = new HDFdataset ("FILE_STREAM", hdf_sco_group, HDF_STRING, aHDFSize, 1);
00092       hdf_dataset->CreateOnDisk();
00093       hdf_dataset->WriteOnDisk(aBuffer); //Save the stream in the HDF file
00094       hdf_dataset->CloseOnDisk();
00095       hdf_dataset = 0; //will be deleted by hdf_sco_group destructor
00096 
00097       // store multifile state
00098       aHDFSize[0] = 2;
00099       hdf_dataset = new HDFdataset("MULTIFILE_STATE", hdf_sco_group, HDF_STRING, aHDFSize, 1);
00100       hdf_dataset->CreateOnDisk();
00101       hdf_dataset->WriteOnDisk((void*)(isMultiFile ? "M" : "S")); // save: multi or single
00102       hdf_dataset->CloseOnDisk();
00103       hdf_dataset = 0; //will be deleted by hdf_sco_group destructor
00104 
00105       // store ASCII state
00106       aHDFSize[0] = 2;
00107       hdf_dataset = new HDFdataset("ASCII_STATE", hdf_sco_group, HDF_STRING, aHDFSize, 1);
00108       hdf_dataset->CreateOnDisk();
00109       hdf_dataset->WriteOnDisk((void*)(isASCII ? "A" : "B")); // save: ASCII or BINARY
00110       hdf_dataset->CloseOnDisk();
00111       hdf_dataset = 0; //will be deleted by hdf_sco_group destructor
00112 
00113       hdf_sco_group->CloseOnDisk();
00114       hdf_sco_group = 0; // will be deleted by hdf_group_datacomponent destructor
00115 
00116       delete [] aBuffer;
00117     }
00118 
00119     hdf_group_datacomponent->CloseOnDisk();
00120     hdf_group_datacomponent = 0; // will be deleted by hdf_file destructor
00121 
00122     //-----------------------------------------------------------------------
00123     // 3 - Write the Study Structure
00124     //-----------------------------------------------------------------------
00125     hdf_group_study_structure = new HDFgroup ("STUDY_STRUCTURE", hdf_file);
00126     hdf_group_study_structure->CreateOnDisk();
00127 
00128     for (it = myMap.begin(); it != myMap.end(); ++it) {
00129       std::string aName (it->first);
00130       char* aModuleName = const_cast<char*>(aName.c_str());
00131 
00132       //hdf_sco_group2 = new HDFgroup(scid.ToCString(), hdf_group_study_structure);
00133       char* entry = (char*)(mapNameEntry[aModuleName].c_str());
00134       hdf_sco_group2 = new HDFgroup (entry, hdf_group_study_structure);
00135       hdf_sco_group2->CreateOnDisk();
00136 
00137       // ComponentDataType treatment
00138       hdf_int32 name_len = (hdf_int32)strlen(aModuleName);
00139       aHDFSize[0] = name_len + 1;
00140       hdf_dataset = new HDFdataset ("COMPONENTDATATYPE", hdf_sco_group2, HDF_STRING, aHDFSize, 1);
00141       hdf_dataset->CreateOnDisk();
00142       hdf_dataset->WriteOnDisk(aModuleName);
00143       hdf_dataset->CloseOnDisk();
00144       hdf_dataset = 0; //will be deleted by hdf_sco_group2 destructor
00145 
00146       hdf_sco_group2->CloseOnDisk();
00147       hdf_sco_group2 = 0; // will be deleted by hdf_group_study_structure destructor
00148     }
00149 
00150     hdf_group_study_structure->CloseOnDisk();
00151     hdf_group_study_structure = 0; // will be deleted by hdf_file destructor
00152 
00153     hdf_file->CloseOnDisk();
00154     delete hdf_file; // recursively deletes all hdf objects...
00155 
00156   } catch (HDFexception) {
00157     isError = true;
00158   }
00159   if (isASCII && !isError) { // save file in ASCII format
00160     HDFascii::ConvertFromHDFToASCII(theFileName, true);
00161   }
00162 
00163   return !isError;
00164 }
00165 
00169 bool LightApp_HDFDriver::ReadDatasFromFile( const char* theFileName, bool isMultiFile )
00170 {
00171   bool isASCII = false;
00172   bool isError = false;
00173   TCollection_AsciiString aHDFUrl;
00174 
00175   HDFfile *hdf_file = 0;
00176   HDFgroup *hdf_group_datacomponent = 0;
00177   HDFgroup *hdf_group_study_structure = 0;
00178   HDFgroup *hdf_sco_group  = 0;
00179   HDFgroup *hdf_sco_group2 = 0;
00180 
00181   std::map<std::string, std::string> mapEntryName;
00182 
00183   if (HDFascii::isASCII(theFileName)) {
00184     isASCII = true;
00185     char* aResultPath = HDFascii::ConvertFromASCIIToHDF(theFileName);
00186     aHDFUrl = aResultPath;
00187     aHDFUrl += "hdf_from_ascii.hdf";
00188     delete(aResultPath);
00189   } else {
00190     aHDFUrl = (char*)theFileName;
00191   }
00192 
00193   hdf_file = new HDFfile((char*)aHDFUrl.ToCString());
00194 
00195   char aMultifileState[2];
00196   char ASCIIfileState[2];
00197 
00198   try {
00199     hdf_file->OpenOnDisk(HDF_RDONLY);
00200 
00201   } catch (HDFexception) {
00202     //char *eStr = new char[strlen(aUrl.ToCString()) + 17];
00203     //sprintf(eStr,"Can't open file %s", aUrl.ToCString());
00204     //_errorCode = TCollection_AsciiString(eStr);
00205     //delete [] eStr;
00206     return false;
00207   }
00208 
00209   try {
00210     if (!hdf_file->ExistInternalObject("STUDY_STRUCTURE")) {
00211       //_errorCode = "Study is empty";
00212       isError = true;
00213     } else {
00214       hdf_group_study_structure = new HDFgroup ("STUDY_STRUCTURE", hdf_file);
00215       hdf_group_study_structure->OpenOnDisk();
00216 
00217       char name[HDF_NAME_MAX_LEN + 1];
00218       Standard_Integer nbsons = hdf_group_study_structure->nInternalObjects();
00219       for (Standard_Integer i = 0; i < nbsons; i++) {
00220         hdf_group_study_structure->InternalObjectIndentify(i, name);
00221         if (strncmp(name, "INTERNAL_COMPLEX", 16) == 0) continue;
00222         hdf_object_type type = hdf_group_study_structure->InternalObjectType(name);
00223         if (type == HDF_GROUP) {
00224           hdf_sco_group2 = new HDFgroup (name, hdf_group_study_structure);
00225           hdf_sco_group2->OpenOnDisk();
00226 
00227           // Read component data
00228           char* aCompDataType = NULL;
00229           int aDataSize = 0;
00230 
00231           if (hdf_sco_group2->ExistInternalObject("COMPONENTDATATYPE")) {
00232             HDFdataset *hdf_dataset = new HDFdataset("COMPONENTDATATYPE", hdf_sco_group2);
00233             hdf_dataset->OpenOnDisk();
00234             aDataSize = hdf_dataset->GetSize();
00235             aCompDataType = new char[aDataSize];
00236             if (aCompDataType == NULL) {
00237               isError = true;
00238             } else {
00239               hdf_dataset->ReadFromDisk(aCompDataType);
00240 
00241               mapEntryName[name] = aCompDataType;
00242 
00243               delete [] aCompDataType;
00244             }
00245 
00246             hdf_dataset->CloseOnDisk();
00247             hdf_dataset = 0;
00248           }
00249 
00250           hdf_sco_group2->CloseOnDisk();
00251         }
00252       }
00253 
00254       hdf_group_study_structure->CloseOnDisk();
00255     }
00256 
00257     if (!hdf_file->ExistInternalObject("DATACOMPONENT")) {
00258       //_errorCode = "No components stored";
00259       isError = true;
00260     } else {
00261       hdf_group_datacomponent = new HDFgroup ("DATACOMPONENT", hdf_file);
00262       hdf_group_datacomponent->OpenOnDisk();
00263 
00264       char name[HDF_NAME_MAX_LEN + 1];
00265       Standard_Integer nbsons = hdf_group_datacomponent->nInternalObjects();
00266       for (Standard_Integer i = 0; i < nbsons; i++) {
00267         hdf_group_datacomponent->InternalObjectIndentify(i, name);
00268         if (strncmp(name, "INTERNAL_COMPLEX", 16) == 0) continue;
00269         hdf_object_type type = hdf_group_datacomponent->InternalObjectType(name);
00270         if (type == HDF_GROUP) {
00271           hdf_sco_group = new HDFgroup (name, hdf_group_datacomponent);
00272           hdf_sco_group->OpenOnDisk();
00273 
00274           // Read component data
00275           unsigned char* aStreamFile = NULL;
00276           int aStreamSize = 0;
00277 
00278           if (hdf_sco_group->ExistInternalObject("FILE_STREAM")) {
00279             HDFdataset *hdf_dataset = new HDFdataset("FILE_STREAM", hdf_sco_group);
00280             hdf_dataset->OpenOnDisk();
00281             aStreamSize = hdf_dataset->GetSize();
00282             aStreamFile = new unsigned char[aStreamSize];
00283             if (aStreamFile == NULL) {
00284               isError = true;
00285             } else {
00286               hdf_dataset->ReadFromDisk(aStreamFile);
00287             }
00288 
00289             hdf_dataset->CloseOnDisk();
00290             hdf_dataset = 0;
00291           }
00292 
00293           HDFdataset *multifile_hdf_dataset = new HDFdataset("MULTIFILE_STATE", hdf_sco_group);
00294           multifile_hdf_dataset->OpenOnDisk();
00295           multifile_hdf_dataset->ReadFromDisk(aMultifileState);
00296           multifile_hdf_dataset->CloseOnDisk();
00297           multifile_hdf_dataset = 0;
00298 
00299           HDFdataset *ascii_hdf_dataset = new HDFdataset("ASCII_STATE", hdf_sco_group);
00300           ascii_hdf_dataset->OpenOnDisk();
00301           ascii_hdf_dataset->ReadFromDisk(ASCIIfileState);
00302           ascii_hdf_dataset->CloseOnDisk();
00303           ascii_hdf_dataset = 0;
00304 
00305           isASCII = (ASCIIfileState[0] == 'A') ? true : false;
00306 
00307           if (aStreamFile != NULL) {
00308             // Put buffer to aListOfFiles and set to myMap
00309             ListOfFiles aListOfFiles = PutStreamToFiles(aStreamFile, aStreamSize, isMultiFile);
00310             char* aCompDataType = (char*)(mapEntryName[name].c_str());
00311             SetListOfFiles(aCompDataType, aListOfFiles);
00312 
00313             delete [] aStreamFile;
00314           }
00315 
00316           hdf_sco_group->CloseOnDisk();
00317         }
00318       }
00319 
00320       hdf_group_datacomponent->CloseOnDisk();
00321     }
00322   } catch (HDFexception) {
00323     isError = true;
00324 
00325     //Handle(TColStd_HSequenceOfAsciiString) aFilesToRemove = new TColStd_HSequenceOfAsciiString;
00326     //aFilesToRemove->Append(aHDFUrl);
00327     //RemoveFiles(aFilesToRemove, true);
00328   }
00329 
00330   hdf_file->CloseOnDisk();
00331   delete hdf_file; // all related hdf objects will be deleted
00332 
00333   if (isASCII && !isError) {
00334     //Handle(TColStd_HSequenceOfAsciiString) aFilesToRemove = new TColStd_HSequenceOfAsciiString;
00335     //aFilesToRemove->Append(aHDFUrl);
00336     //RemoveFiles(aFilesToRemove, true);
00337   }
00338 
00339   //std::map<std::string, std::string>::const_iterator it;
00340   //for (it = mapEntryName.begin(); it != mapEntryName.end(); ++it) {
00341   //  cout << "Read Component: entry = " << it->first
00342   //       << ", Component data type = " << it->second << endl;
00343   //}
00344 
00345   return !isError;
00346 }