Salome HOME
Update object browser
[modules/smesh.git] / src / MEDWrapper / MED_Factory.cxx
index e2f02326b59b8e01cf2be7580b09ab537781f246..6f3e9a2c2c2ee608e44a5131b75e88766059e04c 100644 (file)
@@ -24,6 +24,8 @@
 #include "MED_Utilities.hxx"
 #include "MED_Wrapper.hxx"
 
+#include <Basics_Utils.hxx>
+
 #include <stdio.h>
 #include <errno.h>
 #include <sstream>
@@ -44,14 +46,23 @@ extern "C"
 
 // -------------------------------------------------------------------------------------------------------------------
 // --- MED file compatibility: write using a lower major version implies append on an empty file of the target version
+//
+// *******************************************************************************************************************
+// ==> This file must be modified when MED version changes:
+// MED_MAJOR_NUM and MED_MINOR_NUM are defined in an external include from MED prerequisite: med.h
+// When MED_MAJOR_NUM or MED_MINOR_NUM changes, MED_MAJOR_EXPECTED and MED_MINOR_EXPECTED must be set accordingly
+// and MED_VERSIONS_APPEND_COMPATIBLE must be updated: The lower compatible versions may change with a new MED version
+// If the major version of MED change (for instance 4 --> 5) and if MED allows to append meshes in MED-4.x format,
+// Empty file content for MED-4.x should be provided (EMPTY_FILE_4x) and method CreateEmptyMEDFile should be updated.
+// *******************************************************************************************************************
 
 #define MED_MAJOR_EXPECTED 4
 #define MED_MINOR_EXPECTED 0
 #if MED_MAJOR_NUM != MED_MAJOR_EXPECTED
-  #error "MED major version does not correspond to the expected version, fix the minor and major compatibility values in CheckCompatibility method (MED_VERSIONS_APPEND_COMPATIBLE) and set the correct expected version"
+  #error "MED major version does not correspond to the expected version, fix the minor and major compatibility values in CheckCompatibility method (MED_VERSIONS_APPEND_COMPATIBLE) and set the correct expected version (MED_MAJOR_EXPECTED, MED_MINOR_EXPECTED)"
 #endif
 #if MED_MINOR_NUM != MED_MINOR_EXPECTED
-  #error "MED minor version does not correspond to the expected version, fix the minor and major compatibility values in CheckCompatibility method (MED_VERSIONS_APPEND_COMPATIBLE) and set the correct expected version"
+  #error "MED minor version does not correspond to the expected version, fix the minor and major compatibility values in CheckCompatibility method (MED_VERSIONS_APPEND_COMPATIBLE) and set the correct expected version above (MED_MAJOR_EXPECTED, MED_MINOR_EXPECTED)"
 #endif
 #define MED_VERSIONS_APPEND_COMPATIBLE {40, 32, 33} // --- 10*major + minor (the 3rd digit, release, is not used here,
                                                     //                       med uses always the latest available)
@@ -77,11 +88,22 @@ bool CreateEmptyMEDFile(const std::string& fileName, int version)
   MESSAGE("create an empty med file of the right version, for append " << version);
   static const unsigned char empty_32[] = EMPTY_FILE_32;
   static const unsigned char empty_33[] = EMPTY_FILE_33;
+#ifdef WIN32
+#ifdef UNICODE
+  std::wstring aFilename = Kernel_Utils::utf8_decode_s(fileName);
+#else
+  std::wstring aFilename = fileName;
+#endif
+  std::ofstream ofs(aFilename, std::ios::binary);
+#else
   std::ofstream ofs(fileName);
+#endif
   if (version == 32)
     ofs.write(reinterpret_cast<const char *>(empty_32),sizeof(empty_32));
   else if (version == 33)
     ofs.write(reinterpret_cast<const char *>(empty_33),sizeof(empty_33));
+  ofs.flush();
+  ofs.close();
   return true;
 }
 // -------------------------------------------------------------------------------------------------------------------
@@ -92,18 +114,18 @@ namespace MED
   {
 #ifdef WIN32
 #ifdef UNICODE
-       size_t length = strlen(fileName.c_str()) + sizeof(char);
-       wchar_t* path = new wchar_t[length];
-       memset(path, '\0', length);
-       mbstowcs(path, fileName.c_str(), length);
+    int size_needed = MultiByteToWideChar(CP_UTF8, 0, fileName.c_str(), strlen(fileName.c_str()), NULL, 0);
+    wchar_t* path = new wchar_t[size_needed + 1];
+    MultiByteToWideChar(CP_UTF8, 0, fileName.c_str(), strlen(fileName.c_str()), path, size_needed);
+    path[size_needed] = '\0';
 #else
-       cosnt char* path = xmlPath.c_str();
+    cosnt char* path = xmlPath.c_str();
 #endif
-       bool res = (GetFileAttributes(path) != INVALID_FILE_ATTRIBUTES);
+    bool res = (GetFileAttributes(path) != INVALID_FILE_ATTRIBUTES);
 #ifdef UNICODE
-       delete path;
+    delete path;
 #endif
-       return res;
+    return res;
 #else
     return (access(fileName.c_str(), F_OK) == 0);
 #endif
@@ -117,6 +139,9 @@ namespace MED
   {
     int mvok[] = MED_VERSIONS_APPEND_COMPATIBLE;
     std::vector<int> MEDVersionsOK(mvok, mvok + sizeof(mvok)/sizeof(int));
+    int curVersion = MED_MAJOR_NUM * 10 + MED_MINOR_NUM;
+    if ( MEDVersionsOK[0] != curVersion )
+      MEDVersionsOK.insert( MEDVersionsOK.begin(), curVersion );
     return MEDVersionsOK;
   }
 
@@ -129,7 +154,7 @@ namespace MED
   bool CheckCompatibility(const std::string& fileName, bool isForAppend)
   {
     bool ok = false;
-    int medVersionsOK[] = MED_VERSIONS_APPEND_COMPATIBLE;
+    std::vector<int> medVersionsOK = GetMEDVersionsAppendCompatible();
     // check that file is accessible
     if ( exists(fileName) ) {
       // check HDF5 && MED compatibility
@@ -148,7 +173,7 @@ namespace MED
               ok = true;
             else {
               int medVersion = 10*major + minor;
-              for (int ii=0; ii < sizeof(medVersionsOK)/sizeof(int); ii++)
+              for (size_t ii=0; ii < medVersionsOK.size(); ii++)
                 if (medVersionsOK[ii] == medVersion) {
                   ok =true;
                   break;
@@ -198,46 +223,46 @@ namespace MED
     if (!CheckCompatibility(fileName)) {
       EXCEPTION(std::runtime_error, "Cannot open file '"<<fileName<<"'.");
     }
-    return new MED::TWrapper(fileName);
+    return new MED::TWrapper(fileName, false);
   }
 
   PWrapper CrWrapperW(const std::string& fileName, int theVersion)
   {
     bool isCreated = false;
     if (!CheckCompatibility(fileName, true))
-      {
-        remove(fileName.c_str());
-        isCreated = true;
-      }
+    {
+      remove(fileName.c_str());
+      isCreated = true;
+    }
     int minor = -1;
     if (isCreated)
+    {
+      med_int wantedMajor = MED_MAJOR_NUM;
+      med_int wantedMinor = MED_MINOR_NUM;
+      if (theVersion > 0)
       {
-        med_int wantedMajor = MED_MAJOR_NUM;
-        med_int wantedMinor = MED_MINOR_NUM;
-        if (theVersion > 0)
-          {
-            wantedMajor = theVersion/10;
-            wantedMinor = theVersion%10;
-          }
-        if (wantedMajor == MED_MAJOR_NUM) // the med file will be actually created
-          {
-            if (wantedMinor < MED_MINOR_NUM)
-              minor = wantedMinor;
-          }
-        else                              // an empty existing med file of the right version will be used for append
+        wantedMajor = theVersion/10;
+        wantedMinor = theVersion%10;
+      }
+      if (wantedMajor == MED_MAJOR_NUM) // the med file will be actually created
+      {
+        if (wantedMinor < MED_MINOR_NUM)
+          minor = wantedMinor;
+      }
+      else                              // an empty existing med file of the right version will be used for append
+      {
+        int medVersionsOK[] = MED_VERSIONS_APPEND_COMPATIBLE;
+        bool isVersionOK = false;
+        for (size_t ii=0; ii < sizeof(medVersionsOK)/sizeof(int); ii++)
+          if (medVersionsOK[ii] == theVersion)
           {
-            int medVersionsOK[] = MED_VERSIONS_APPEND_COMPATIBLE;
-            bool isVersionOK = false;
-            for (int ii=0; ii < sizeof(medVersionsOK)/sizeof(int); ii++)
-              if (medVersionsOK[ii] == theVersion)
-                {
-                  isVersionOK =true;
-                  break;
-                }
-            if (isVersionOK)              // copy an empty existing med file of the right version, for append
-              CreateEmptyMEDFile(fileName, theVersion);
+            isVersionOK =true;
+            break;
           }
+        if (isVersionOK)              // copy an empty existing med file of the right version, for append
+          CreateEmptyMEDFile(fileName, theVersion);
       }
-    return new MED::TWrapper(fileName, minor);
+    }
+    return new MED::TWrapper(fileName, true, minor);
   }
 }