Salome HOME
Some corrections related to using UNICODE.
[modules/smesh.git] / src / MEDWrapper / MED_Factory.cxx
index 52558d0f43c3e0210eda60ea00ec8a06ebfc11bc..3e354c131c4e8cdea033cc7b44c1b608773ccab5 100644 (file)
@@ -35,34 +35,54 @@ extern "C"
   #include <unistd.h>
 #endif
 }
+#include <utilities.h>
+
+#ifdef WIN32
+#include <windows.h>
+#endif
+
 
 namespace MED
 {
   bool exists(const std::string& fileName)
   {
 #ifdef WIN32
-    return (GetFileAttributes(xmlPath.c_str()) != INVALID_FILE_ATTRIBUTES);
+#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);
+#else
+       cosnt char* path = xmlPath.c_str();
+#endif
+       bool res = (GetFileAttributes(path) != INVALID_FILE_ATTRIBUTES);
+#ifdef UNICODE
+       delete path;
+#endif
+       return res;
 #else
     return (access(fileName.c_str(), F_OK) == 0);
 #endif
   }
 
-  bool CheckCompatibility(const std::string& fileName)
+  bool CheckCompatibility(const std::string& fileName, bool isForAppend)
   {
     bool ok = false;
     // check that file is accessible
     if ( exists(fileName) ) {
       // check HDF5 && MED compatibility
       med_bool hdfok, medok;
-      MEDfileCompatibility(fileName.c_str(), &hdfok, &medok);
-      if ( hdfok && medok ) {
+      med_err r0 = MEDfileCompatibility(fileName.c_str(), &hdfok, &medok);
+      //MESSAGE(r0 << " " << hdfok << " " << medok);
+      if ( r0==0 && hdfok && medok ) {
         med_idt aFid = MEDfileOpen(fileName.c_str(), MED_ACC_RDONLY);
         if (aFid >= 0) {
           med_int major, minor, release;
           med_err ret = MEDfileNumVersionRd(aFid, &major, &minor, &release);
+          //MESSAGE(ret << " " << major << "." << minor << "." << release);
           if (ret >= 0) {
-            int version = 100*major + minor;
-            if (version >= 202)
+            bool isReadOnly = !isForAppend;
+            if ( isReadOnly  || ((major == MED_MAJOR_NUM) && (minor == MED_MINOR_NUM)))
               ok = true;
           }
         }
@@ -113,7 +133,7 @@ namespace MED
 
   PWrapper CrWrapperW(const std::string& fileName, int theMinor)
   {
-    if (!CheckCompatibility(fileName))
+    if (!CheckCompatibility(fileName, true))
       remove(fileName.c_str());
     return new MED::TWrapper(fileName, theMinor);
   }