Salome HOME
Merge branch 'master' of https://codev-tuleap.cea.fr/plugins/git/salome/smesh
[modules/smesh.git] / src / MEDWrapper / MED_Wrapper.cxx
index f4a7fc45831ef77928260a5112154d39d0a0a658..4d0f992714aa6140a2198f0178c0de2d824a0363 100644 (file)
@@ -23,7 +23,9 @@
 #include "MED_Wrapper.hxx"
 #include "MED_TStructures.hxx"
 #include "MED_Utilities.hxx"
+#include "MED_TFile.hxx"
 
+#include "MEDFileUtilities.hxx"
 #include <med.h>
 #include <med_err.h>
 #include <med_proto.h>
@@ -76,90 +78,75 @@ namespace MED
   }
 
   //---------------------------------------------------------------
-  class TFile
+  const TIdt& MEDIDTHoder::Id() const
   {
-    TFile();
-    TFile(const TFile&);
+    if (myFid < 0)
+      EXCEPTION(std::runtime_error, "TFile - GetFid() < 0");
+    return myFid;
+  }
 
-  public:
-    TFile(const std::string& theFileName, TInt theMajor=-1, TInt theMinor=-1):
-      myCount(0),
-      myFid(0),
-      myFileName(theFileName),
-      myMajor(theMajor),
-      myMinor(theMinor)
+  void TMemFile::Open(EModeAcces theMode, TErr* theErr)
+  {
+    if (this->myCount++ == 0)
     {
-      if ((myMajor < 0) || (myMajor > MED_MAJOR_NUM)) myMajor = MED_MAJOR_NUM;
-      if ((myMinor < 0) || (myMajor == MED_MAJOR_NUM && myMinor > MED_MINOR_NUM)) myMinor = MED_MINOR_NUM;
+      std::string dftFileName = MEDCoupling::MEDFileWritableStandAlone::GenerateUniqueDftFileNameInMem();
+      memfile = MED_MEMFILE_INIT;
+      memfile.app_image_ptr=0;
+      memfile.app_image_size=0;
+      myFid = MEDmemFileOpen(dftFileName.c_str(),&memfile,MED_FALSE,MED_ACC_CREAT);
     }
+    if (theErr)
+      *theErr = TErr(myFid);
+    else if (myFid < 0)
+      EXCEPTION(std::runtime_error,"TMemFile - MEDmemFileOpen");
+  }
 
-    ~TFile()
-    {
-      Close();
-    }
+  TFile::TFile(const std::string& theFileName, TInt theMajor, TInt theMinor):
+    myFileName(theFileName),
+    myMajor(theMajor),
+    myMinor(theMinor)
+  {
+    if ((myMajor < 0) || (myMajor > MED_MAJOR_NUM)) myMajor = MED_MAJOR_NUM;
+    if ((myMinor < 0) || (myMajor == MED_MAJOR_NUM && myMinor > MED_MINOR_NUM)) myMinor = MED_MINOR_NUM;
+  }
 
-    void
-    Open(EModeAcces theMode,
-         TErr* theErr = NULL)
-    {
-      if (myCount++ == 0) {
-        const char* aFileName = myFileName.c_str();
+  void TFile::Open(EModeAcces theMode, TErr* theErr)
+  {
+    if (myCount++ == 0) {
+      const char* aFileName = myFileName.c_str();
 #ifdef WIN32
-        if (med_access_mode(theMode) == MED_ACC_RDWR) {
-          // Force removing readonly attribute from a file under Windows, because of a bug in the HDF5
-          std::string aReadOlnyRmCmd = "attrib -r \"" + myFileName + "\"> nul 2>&1";
+      if (med_access_mode(theMode) == MED_ACC_RDWR) {
+        // Force removing readonly attribute from a file under Windows, because of a bug in the HDF5
+        std::string aReadOlnyRmCmd = "attrib -r \"" + myFileName + "\"> nul 2>&1";
 #ifdef UNICODE
-          const char* to_decode = aReadOlnyRmCmd.c_str();
-          int size_needed = MultiByteToWideChar(CP_UTF8, 0, to_decode, strlen(to_decode), NULL, 0);
-          wchar_t* awReadOlnyRmCmd = new wchar_t[size_needed + 1];
-          MultiByteToWideChar(CP_UTF8, 0, to_decode, strlen(to_decode), awReadOlnyRmCmd, size_needed);
-          awReadOlnyRmCmd[size_needed] = '\0';
-          _wsystem(awReadOlnyRmCmd);
-          delete[] awReadOlnyRmCmd;
+        const char* to_decode = aReadOlnyRmCmd.c_str();
+        int size_needed = MultiByteToWideChar(CP_UTF8, 0, to_decode, strlen(to_decode), NULL, 0);
+        wchar_t* awReadOlnyRmCmd = new wchar_t[size_needed + 1];
+        MultiByteToWideChar(CP_UTF8, 0, to_decode, strlen(to_decode), awReadOlnyRmCmd, size_needed);
+        awReadOlnyRmCmd[size_needed] = '\0';
+        _wsystem(awReadOlnyRmCmd);
+        delete[] awReadOlnyRmCmd;
 #else
-          system(aReadOlnyRmCmd.c_str());
-#endif
-        }
+        system(aReadOlnyRmCmd.c_str());
 #endif
-        myFid = MEDfileVersionOpen(aFileName,med_access_mode(theMode), myMajor, myMinor, MED_RELEASE_NUM);
       }
-      if (theErr)
-        *theErr = TErr(myFid);
-      else if (myFid < 0)
-        EXCEPTION(std::runtime_error,"TFile - MEDfileVersionOpen('"<<myFileName<<"',"<<theMode<<"',"<< myMajor <<"',"<< myMinor<<"',"<< MED_RELEASE_NUM<<")");
-    }
-
-    const TIdt&
-    Id() const
-    {
-      if (myFid < 0)
-        EXCEPTION(std::runtime_error, "TFile - GetFid() < 0");
-      return myFid;
-    }
-
-    void
-    Close()
-    {
-      if (--myCount == 0)
-        MEDfileClose(myFid);
+#endif
+      myFid = MEDfileVersionOpen(aFileName,med_access_mode(theMode), myMajor, myMinor, MED_RELEASE_NUM);
     }
-
-  protected:
-    TInt myCount;
-    TIdt myFid;
-    std::string myFileName;
-    TInt myMajor;
-    TInt myMinor;
-  };
+    if (theErr)
+      *theErr = TErr(myFid);
+    else if (myFid < 0)
+      EXCEPTION(std::runtime_error,"TFile - MEDfileVersionOpen('"<<myFileName<<"',"<<theMode<<"',"<< myMajor <<"',"<< myMinor<<"',"<< MED_RELEASE_NUM<<")");
+  }
 
   //---------------------------------------------------------------
   class TFileWrapper
   {
-    PFile myFile;
+    PFileInternal myFile;
     TInt myMinor;
 
   public:
-    TFileWrapper(const PFile& theFile,
+    TFileWrapper(const PFileInternal& theFile,
                  EModeAcces theMode,
                  TErr* theErr = NULL,
                  TInt theMinor=-1):
@@ -211,11 +198,15 @@ namespace MED
 
   //---------------------------------------------------------------
   TWrapper
-  ::TWrapper(const std::string& theFileName, bool write, TInt theMajor, TInt theMinor):
-    myFile(new TFile(theFileName, theMajor, theMinor)),
+  ::TWrapper(const std::string& theFileName, bool write, TFileInternal *tfileInst, TInt theMajor, TInt theMinor):
     myMajor(theMajor),
     myMinor(theMinor)
   {
+    if(!tfileInst)
+      myFile.reset(new TFile(theFileName, theMajor, theMinor) );
+    else
+      myFile.reset(new TFileDecorator(tfileInst) );
+    
     TErr aRet;
     if ( write ) {
       myFile->Open(eLECTURE_ECRITURE, &aRet);