Salome HOME
introduction of MED write/append with a lower major version of MED file format (hdf5...
[modules/smesh.git] / src / MEDWrapper / MED_Factory.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "MED_Factory.hxx"
24 #include "MED_Utilities.hxx"
25 #include "MED_Wrapper.hxx"
26
27 #include <stdio.h>
28 #include <errno.h>
29 #include <sstream>
30
31 #include <med.h>
32 extern "C"
33 {
34 #ifndef WIN32
35   #include <unistd.h>
36 #endif
37 }
38 #include <utilities.h>
39
40 #ifdef WIN32
41 #include <windows.h>
42 #endif
43
44 #define MED_MAJOR_EXPECTED 4
45 #define MED_MINOR_EXPECTED 0
46 #if MED_MAJOR_NUM != MED_MAJOR_EXPECTED
47   #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"
48 #endif
49 #if MED_MINOR_NUM != MED_MINOR_EXPECTED
50   #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"
51 #endif
52 #define MED_VERSIONS_APPEND_COMPATIBLE {40, 32, 33} // --- 10*major + minor (the 3rd digit, release, is not used here,
53                                                     //                       med uses always the latest available)
54                                                     // --- The first in the list should be the default: current version
55
56 namespace MED
57 {
58   bool exists(const std::string& fileName)
59   {
60 #ifdef WIN32
61 #ifdef UNICODE
62         size_t length = strlen(fileName.c_str()) + sizeof(char);
63         wchar_t* path = new wchar_t[length];
64         memset(path, '\0', length);
65         mbstowcs(path, fileName.c_str(), length);
66 #else
67         cosnt char* path = xmlPath.c_str();
68 #endif
69     return (GetFileAttributes(path) != INVALID_FILE_ATTRIBUTES);
70 #else
71     return (access(fileName.c_str(), F_OK) == 0);
72 #endif
73   }
74
75   /*!
76    *  Return the list of med versions compatibles for write/append,
77    *  encoded in 10*major+minor (for instance, code for med 3.2.1 is 32)
78    */
79   std::vector<int> GetMEDVersionsAppendCompatible()
80   {
81     int mvok[] = MED_VERSIONS_APPEND_COMPATIBLE;
82     std::vector<int> MEDVersionsOK(mvok, mvok + sizeof(mvok)/sizeof(int));
83     return MEDVersionsOK;
84   }
85
86   /*!
87    *  \brief: Check read or write(append) Compatibility of a med file
88    *  \param [in] : fileName - the file to read or to append to
89    *  \param [in] : isforAppend - when true, check if the med file version is OK to append a mesh,
90    *                              when false, check if the med file is readable.
91    */
92   bool CheckCompatibility(const std::string& fileName, bool isForAppend)
93   {
94     bool ok = false;
95     int medVersionsOK[] = MED_VERSIONS_APPEND_COMPATIBLE;
96     // check that file is accessible
97     if ( exists(fileName) ) {
98       // check HDF5 && MED compatibility
99       med_bool hdfok, medok;
100       med_err r0 = MEDfileCompatibility(fileName.c_str(), &hdfok, &medok);
101       MESSAGE(r0 << " " << hdfok << " " << medok);
102       if ( r0==0 && hdfok && medok ) {
103         med_idt aFid = MEDfileOpen(fileName.c_str(), MED_ACC_RDONLY);
104         if (aFid >= 0) {
105           med_int major, minor, release;
106           med_err ret = MEDfileNumVersionRd(aFid, &major, &minor, &release);
107           MESSAGE(ret << " " << major << "." << minor << "." << release);
108           if (ret >= 0) {
109             bool isReadOnly = !isForAppend;
110             if (isReadOnly)
111               ok = true;
112             else {
113               int medVersion = 10*major + minor;
114               for (int ii=0; ii < sizeof(medVersionsOK)/sizeof(int); ii++)
115                 if (medVersionsOK[ii] == medVersion) {
116                   ok =true;
117                   break;
118                 }
119             }
120           }
121         }
122         MEDfileClose(aFid);
123       }
124     }
125     return ok;
126   }
127
128   bool GetMEDVersion(const std::string& fileName, int& major, int& minor, int& release)
129   {
130     bool ok = false;
131     major = minor = release = 0;
132     med_idt aFid = MEDfileOpen(fileName.c_str(), MED_ACC_RDONLY);
133     if (aFid >= 0) {
134       med_int _major, _minor, _release;
135       med_err ret = MEDfileNumVersionRd(aFid, &_major, &_minor, &_release);
136       if (ret == 0) {
137         major = _major;
138         minor = _minor;
139         release = _release;
140         ok = true;
141       }
142       MEDfileClose(aFid);
143     }
144     return ok;
145   }
146
147   std::string GetMEDVersion(const std::string& fileName)
148   {
149     std::string version;
150     int major, minor, release;
151     if (GetMEDVersion(fileName, major, minor, release)) {
152       std::ostringstream os;
153       os << major << "." << minor << "." << release;
154       version = os.str();
155     }
156     return version;
157   }
158
159   PWrapper CrWrapperR(const std::string& fileName)
160   {
161     if (!CheckCompatibility(fileName)) {
162       EXCEPTION(std::runtime_error, "Cannot open file '"<<fileName<<"'.");
163     }
164     return new MED::TWrapper(fileName);
165   }
166
167   PWrapper CrWrapperW(const std::string& fileName, int theMinor)
168   {
169     if (!CheckCompatibility(fileName, true))
170       remove(fileName.c_str());
171     return new MED::TWrapper(fileName, theMinor);
172   }
173 }