Salome HOME
Test EXPORTS definition with target name as suggested by cmake
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_StudyManager.cxx
index 428c5b636666cdc5c2d9d2d7b34f52702a3db3f5..ee5457b34709f05b3e18ba06ddb4a39647bf493b 100644 (file)
@@ -1,26 +1,28 @@
-// Copyright (C) 2005  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 
-// Lesser General Public License for more details.
+//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
 //
-// 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
+//  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 //
-// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//  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.
+//
+//  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   : SALOMEDSImpl_StudyManager.cxx
 //  Author : Sergey RUIN
 //  Module : SALOME
-
+//
 #include "SALOMEDSImpl_StudyManager.hxx"
 
 #include "DF_ChildIterator.hxx"
@@ -30,6 +32,7 @@
 #include "SALOMEDSImpl_Tool.hxx"
 #include "SALOMEDSImpl_SComponent.hxx"
 #include "SALOMEDSImpl_GenericAttribute.hxx"
+#include "SALOMEDSImpl_ScalarVariable.hxx"
 #include <map>
 
 #include "HDFOI.hxx"
@@ -45,6 +48,7 @@ static void ReadAttributes(SALOMEDSImpl_Study*, const SALOMEDSImpl_SObject&, HDF
 static void BuildTree (SALOMEDSImpl_Study*, HDFgroup*);
 static void Translate_IOR_to_persistentID (const SALOMEDSImpl_SObject&,
                                           SALOMEDSImpl_Driver*, bool isMultiFile, bool isASCII);
+static void ReadNoteBookVariables(SALOMEDSImpl_Study* theStudy, HDFgroup* theGroup);
 
 //============================================================================
 /*! Function : SALOMEDSImpl_StudyManager
@@ -111,6 +115,7 @@ SALOMEDSImpl_Study* SALOMEDSImpl_StudyManager::Open(const string& aUrl)
   // open the HDFFile
   HDFfile *hdf_file =0;
   HDFgroup *hdf_group_study_structure =0;
+  HDFgroup *hdf_notebook_vars = 0; 
 
   char* aC_HDFUrl;
   string aHDFUrl;
@@ -120,9 +125,9 @@ SALOMEDSImpl_Study* SALOMEDSImpl_StudyManager::Open(const string& aUrl)
     char* aResultPath = HDFascii::ConvertFromASCIIToHDF(aUrl.c_str());
     aC_HDFUrl = new char[strlen(aResultPath) + 19];
     sprintf(aC_HDFUrl, "%shdf_from_ascii.hdf", aResultPath);
-    delete(aResultPath);
+    delete [] (aResultPath);
     aHDFUrl = aC_HDFUrl;
-    delete aC_HDFUrl;
+    delete [] aC_HDFUrl;
   } else {
     aHDFUrl = aUrl;
   }
@@ -134,18 +139,12 @@ SALOMEDSImpl_Study* SALOMEDSImpl_StudyManager::Open(const string& aUrl)
   }
   catch (HDFexception)
     {
-//#ifndef WNT
-//      char eStr[strlen(aUrl.ToCString())+17];
-//#else
-         char *eStr;
-         eStr = new char[strlen(aUrl.c_str())+17];
-//#endif
-      sprintf(eStr,"Can't open file %s",aUrl.c_str());
-//#ifdef WNT
-         delete [] eStr;
-//#endif
-      _errorCode = string(eStr);
-      return NULL;
+       char *eStr;
+       eStr = new char[strlen(aUrl.c_str())+17];
+        sprintf(eStr,"Can't open file %s",aUrl.c_str());
+        delete [] eStr;
+        _errorCode = string(eStr);
+        return NULL;
     }
 
   // Temporary aStudyUrl in place of study name
@@ -174,18 +173,22 @@ SALOMEDSImpl_Study* SALOMEDSImpl_StudyManager::Open(const string& aUrl)
   }
   catch (HDFexception)
     {
-//#ifndef WNT
-//      char eStr[strlen(aUrl.ToCString())+17];
-//#else
-         char *eStr = new char [strlen(aUrl.c_str())+17];
-//#endif
+      char *eStr = new char [strlen(aUrl.c_str())+17];
       sprintf(eStr,"Can't open file %s", aUrl.c_str());
       _errorCode = string(eStr);
       return NULL;
     }
 
-  hdf_file->CloseOnDisk();
+  //Read and create notebook variables 
+  if(hdf_file->ExistInternalObject("NOTEBOOK_VARIABLES")) {
+    hdf_notebook_vars  = new HDFgroup("NOTEBOOK_VARIABLES",hdf_file);
+    ReadNoteBookVariables(Study,hdf_notebook_vars);
+    hdf_notebook_vars =0; //will be deleted by hdf_sco_group destructor
+  }
 
+  hdf_file->CloseOnDisk();
+  hdf_group_study_structure = new HDFgroup("STUDY_STRUCTURE",hdf_file);
+  
   if (isASCII) {
     vector<string> aFilesToRemove;
     aFilesToRemove.push_back("hdf_from_ascii.hdf");
@@ -457,12 +460,14 @@ bool SALOMEDSImpl_StudyManager::Impl_SaveAs(const string& aUrl,
   HDFgroup *hdf_group_study_structure =0;
   HDFgroup *hdf_sco_group =0;
   HDFgroup *hdf_sco_group2 =0;
+  HDFgroup *hdf_notebook_vars =0; 
+  HDFgroup *hdf_notebook_var  = 0;
 
   HDFgroup *hdf_group_datacomponent =0;
   HDFdataset *hdf_dataset =0;
   hdf_size size[1];
   hdf_int32 name_len = 0;
-  char *component_name = 0;
+  string component_name;
 
   if(!aStudy) {
     _errorCode = "Study is null";
@@ -609,12 +614,12 @@ bool SALOMEDSImpl_StudyManager::Impl_SaveAs(const string& aUrl,
          hdf_sco_group2->CreateOnDisk();
           SaveAttributes(SC, hdf_sco_group2);
          // ComponentDataType treatment
-         component_name = (char*)SC.ComponentDataType().c_str();
-         name_len = (hdf_int32)strlen(component_name);
+         component_name = SC.ComponentDataType();
+         name_len = (hdf_int32)component_name.length();
          size[0] = name_len +1 ;
          hdf_dataset = new HDFdataset("COMPONENTDATATYPE",hdf_sco_group2,HDF_STRING,size,1);
          hdf_dataset->CreateOnDisk();
-         hdf_dataset->WriteOnDisk(component_name);
+         hdf_dataset->WriteOnDisk((char*)component_name.c_str());
          hdf_dataset->CloseOnDisk();
          hdf_dataset=0; //will be deleted by hdf_sco_group destructor
          Impl_SaveObject(SC, hdf_sco_group2);
@@ -633,17 +638,70 @@ bool SALOMEDSImpl_StudyManager::Impl_SaveAs(const string& aUrl,
        hdf_soo_group->CloseOnDisk();
        hdf_soo_group=0; // will be deleted by hdf_group_study_structure destructor
       }
+      //-----------------------------------------------------------------------
+      //5 - Write the NoteBook Variables
+      //-----------------------------------------------------------------------
 
+      //5.1 Create group to store all note book variables
+      hdf_notebook_vars = new HDFgroup("NOTEBOOK_VARIABLES",hdf_file);
+      hdf_notebook_vars->CreateOnDisk();
+      
+      string varValue;
+      string varType;
+      string varIndex;
+
+      for(int i=0 ;i < aStudy->myNoteBookVars.size(); i++ ){
+        // For each variable create HDF group
+        hdf_notebook_var = new HDFgroup((char*)aStudy->myNoteBookVars[i]->Name().c_str(),hdf_notebook_vars);
+        hdf_notebook_var->CreateOnDisk();
+
+        // Save Variable type
+        varType = aStudy->myNoteBookVars[i]->SaveType();
+        name_len = (hdf_int32) varType.length();
+        size[0] = name_len +1 ;
+        hdf_dataset = new HDFdataset("VARIABLE_TYPE",hdf_notebook_var,HDF_STRING,size,1);
+        hdf_dataset->CreateOnDisk();
+        hdf_dataset->WriteOnDisk((char*)varType.c_str());
+        hdf_dataset->CloseOnDisk();
+        hdf_dataset=0; //will be deleted by hdf_sco_group destructor
+        
+        char buffer[256];
+        sprintf(buffer,"%d",i);
+        varIndex= string(buffer);
+        name_len = (hdf_int32) varIndex.length();
+        size[0] = name_len +1 ;
+        hdf_dataset = new HDFdataset("VARIABLE_INDEX",hdf_notebook_var,HDF_STRING,size,1);
+        hdf_dataset->CreateOnDisk();
+        hdf_dataset->WriteOnDisk((char*)varIndex.c_str());
+        hdf_dataset->CloseOnDisk();
+        hdf_dataset=0; //will be deleted by hdf_sco_group destructor
+        
+        
+        // Save Variable value
+        varValue = aStudy->myNoteBookVars[i]->Save();
+        name_len = (hdf_int32) varValue.length();
+        size[0] = name_len +1 ;
+        hdf_dataset = new HDFdataset("VARIABLE_VALUE",hdf_notebook_var,HDF_STRING,size,1);
+        hdf_dataset->CreateOnDisk();
+        hdf_dataset->WriteOnDisk((char*)varValue.c_str());
+        hdf_dataset->CloseOnDisk();
+        hdf_dataset=0; //will be deleted by hdf_sco_group destructor
+        hdf_notebook_var->CloseOnDisk();
+        hdf_notebook_var = 0; //will be deleted by hdf_sco_group destructor
+      }
+      hdf_notebook_vars->CloseOnDisk();
+      hdf_notebook_vars = 0; //will be deleted by hdf_sco_group destructor
+        
       if (aLocked) aStudy->GetProperties()->SetLocked(true);
       //-----------------------------------------------------------------------
-      //5 - Write the Study Properties
+      //6 - Write the Study Properties
       //-----------------------------------------------------------------------
-      name_len = (hdf_int32) aStudy->Name().size();
+      string study_name = aStudy->Name();
+      name_len = (hdf_int32) study_name.size();
       size[0] = name_len +1 ;
       hdf_dataset = new HDFdataset("STUDY_NAME",hdf_group_study_structure,HDF_STRING,size,1);
       hdf_dataset->CreateOnDisk();
-      char* studid = (char*)aStudy->Name().c_str();
-      hdf_dataset->WriteOnDisk(studid);
+      hdf_dataset->WriteOnDisk((char*)study_name.c_str());
       hdf_dataset->CloseOnDisk();
       hdf_dataset=0; // will be deleted by hdf_group_study_structure destructor
 
@@ -717,8 +775,8 @@ bool SALOMEDSImpl_StudyManager::Impl_SaveObject(const SALOMEDSImpl_SObject& SC,
 
       SALOMEDSImpl_SObject SO = SALOMEDSImpl_Study::SObject(itchild.Value());
 
-      char* scoid = (char*) SO.GetID().c_str();
-      hdf_group_sobject = new HDFgroup(scoid, hdf_group_datatype);
+      string scoid = SO.GetID();
+      hdf_group_sobject = new HDFgroup(scoid.c_str(), hdf_group_datatype);
       hdf_group_sobject->CreateOnDisk();
       SaveAttributes(SO, hdf_group_sobject);
       Impl_SaveObject(SO, hdf_group_sobject);
@@ -1137,7 +1195,7 @@ static void ReadAttributes(SALOMEDSImpl_Study* theStudy,
   } else if (!strcmp(hdf_dataset->GetName(),"AttributeReference") ||
              !strcmp(hdf_dataset->GetName(),"Reference")) { // Old format maintainance
     theStudy->NewBuilder()->Addreference(aSO, theStudy->CreateObjectID(current_string));
-    delete(current_string);
+    delete [] (current_string);
     hdf_dataset->CloseOnDisk();
     return;
   } else {
@@ -1148,7 +1206,7 @@ static void ReadAttributes(SALOMEDSImpl_Study* theStudy,
     anAttr->Load(current_string);
   }
   
-  delete(current_string);
+  delete [] (current_string);
   hdf_dataset->CloseOnDisk();
 }
 
@@ -1214,3 +1272,85 @@ static void Translate_IOR_to_persistentID (const SALOMEDSImpl_SObject& so,
   }
 }
 
+void ReadNoteBookVariables(SALOMEDSImpl_Study* theStudy, HDFgroup* theGroup)
+{
+  if(!theGroup)
+    return;
+
+  HDFgroup* new_group =0;
+  HDFdataset* new_dataset =0;
+  
+  char aVarName[HDF_NAME_MAX_LEN+1];
+  char *currentVarType = 0;
+  char *currentVarValue = 0;
+  char *currentVarIndex = 0;
+  int order = 0;
+  //Open HDF group with notebook variables
+  theGroup->OpenOnDisk();
+
+  //Get Nb of variables
+  int aNbVars = theGroup->nInternalObjects();
+
+  map<int,SALOMEDSImpl_GenericVariable*> aVarsMap;
+
+  for( int iVar=0;iVar < aNbVars;iVar++ ) {
+    theGroup->InternalObjectIndentify(iVar,aVarName);
+    hdf_object_type type = theGroup->InternalObjectType(aVarName);
+    if(type == HDF_GROUP) {
+
+      //Read Variable
+      new_group = new HDFgroup(aVarName,theGroup);
+      new_group->OpenOnDisk();
+
+      //Read Type
+      new_dataset = new HDFdataset("VARIABLE_TYPE",new_group);
+      new_dataset->OpenOnDisk();
+      currentVarType = new char[new_dataset->GetSize()+1];
+      new_dataset->ReadFromDisk(currentVarType);
+      new_dataset->CloseOnDisk();
+      new_dataset = 0; //will be deleted by hdf_sco_group destructor
+
+      //Read Order
+      if(new_group->ExistInternalObject("VARIABLE_INDEX")) {
+        new_dataset = new HDFdataset("VARIABLE_INDEX",new_group);
+        new_dataset->OpenOnDisk();
+        currentVarIndex = new char[new_dataset->GetSize()+1];
+        new_dataset->ReadFromDisk(currentVarIndex);
+        new_dataset->CloseOnDisk();
+        new_dataset = 0; //will be deleted by hdf_sco_group destructor
+        order = atoi(currentVarIndex);
+        delete [] currentVarIndex;
+      }
+      else
+        order = iVar;
+      
+      //Read Value
+      new_dataset = new HDFdataset("VARIABLE_VALUE",new_group);
+      new_dataset->OpenOnDisk();
+      currentVarValue = new char[new_dataset->GetSize()+1];
+      new_dataset->ReadFromDisk(currentVarValue);
+      new_dataset->CloseOnDisk();
+      new_dataset = 0; //will be deleted by hdf_sco_group destructor
+
+      new_group->CloseOnDisk();
+      new_group = 0;  //will be deleted by hdf_sco_group destructor
+      
+      SALOMEDSImpl_GenericVariable::VariableTypes aVarType =
+        SALOMEDSImpl_GenericVariable::String2VariableType(string(currentVarType));
+      delete [] currentVarType;
+
+      //Create variable and add it in the study
+      SALOMEDSImpl_GenericVariable* aVariable = 
+        new SALOMEDSImpl_ScalarVariable(aVarType,string(aVarName));
+      aVariable->Load(string(currentVarValue));
+      aVarsMap.insert(make_pair<int,SALOMEDSImpl_GenericVariable*>(order,aVariable));
+      delete [] currentVarValue;
+    }
+  }
+  
+  map<int,SALOMEDSImpl_GenericVariable*>::const_iterator it= aVarsMap.begin();
+  for(;it!=aVarsMap.end();it++)
+    theStudy->AddVariable((*it).second);
+  
+  theGroup->CloseOnDisk();
+}