Salome HOME
3e354c131c4e8cdea033cc7b44c1b608773ccab5
[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
45 namespace MED
46 {
47   bool exists(const std::string& fileName)
48   {
49 #ifdef WIN32
50 #ifdef UNICODE
51         size_t length = strlen(fileName.c_str()) + sizeof(char);
52         wchar_t* path = new wchar_t[length];
53         memset(path, '\0', length);
54         mbstowcs(path, fileName.c_str(), length);
55 #else
56         cosnt char* path = xmlPath.c_str();
57 #endif
58         bool res = (GetFileAttributes(path) != INVALID_FILE_ATTRIBUTES);
59 #ifdef UNICODE
60         delete path;
61 #endif
62         return res;
63 #else
64     return (access(fileName.c_str(), F_OK) == 0);
65 #endif
66   }
67
68   bool CheckCompatibility(const std::string& fileName, bool isForAppend)
69   {
70     bool ok = false;
71     // check that file is accessible
72     if ( exists(fileName) ) {
73       // check HDF5 && MED compatibility
74       med_bool hdfok, medok;
75       med_err r0 = MEDfileCompatibility(fileName.c_str(), &hdfok, &medok);
76       //MESSAGE(r0 << " " << hdfok << " " << medok);
77       if ( r0==0 && hdfok && medok ) {
78         med_idt aFid = MEDfileOpen(fileName.c_str(), MED_ACC_RDONLY);
79         if (aFid >= 0) {
80           med_int major, minor, release;
81           med_err ret = MEDfileNumVersionRd(aFid, &major, &minor, &release);
82           //MESSAGE(ret << " " << major << "." << minor << "." << release);
83           if (ret >= 0) {
84             bool isReadOnly = !isForAppend;
85             if ( isReadOnly  || ((major == MED_MAJOR_NUM) && (minor == MED_MINOR_NUM)))
86               ok = true;
87           }
88         }
89         MEDfileClose(aFid);
90       }
91     }
92     return ok;
93   }
94
95   bool GetMEDVersion(const std::string& fileName, int& major, int& minor, int& release)
96   {
97     bool ok = false;
98     major = minor = release = 0;
99     med_idt aFid = MEDfileOpen(fileName.c_str(), MED_ACC_RDONLY);
100     if (aFid >= 0) {
101       med_int _major, _minor, _release;
102       med_err ret = MEDfileNumVersionRd(aFid, &_major, &_minor, &_release);
103       if (ret == 0) {
104         major = _major;
105         minor = _minor;
106         release = _release;
107         ok = true;
108       }
109       MEDfileClose(aFid);
110     }
111     return ok;
112   }
113
114   std::string GetMEDVersion(const std::string& fileName)
115   {
116     std::string version;
117     int major, minor, release;
118     if (GetMEDVersion(fileName, major, minor, release)) {
119       std::ostringstream os;
120       os << major << "." << minor << "." << release;
121       version = os.str();
122     }
123     return version;
124   }
125
126   PWrapper CrWrapperR(const std::string& fileName)
127   {
128     if (!CheckCompatibility(fileName)) {
129       EXCEPTION(std::runtime_error, "Cannot open file '"<<fileName<<"'.");
130     }
131     return new MED::TWrapper(fileName);
132   }
133
134   PWrapper CrWrapperW(const std::string& fileName, int theMinor)
135   {
136     if (!CheckCompatibility(fileName, true))
137       remove(fileName.c_str());
138     return new MED::TWrapper(fileName, theMinor);
139   }
140 }