Salome HOME
02b7e866ee737e69f6029ec33970789626113043
[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
39 namespace MED
40 {
41   bool exists(const std::string& fileName)
42   {
43 #ifdef WIN32
44     return (GetFileAttributes(xmlPath.c_str()) != INVALID_FILE_ATTRIBUTES);
45 #else
46     return (access(fileName.c_str(), F_OK) == 0);
47 #endif
48   }
49
50   bool CheckCompatibility(const std::string& fileName)
51   {
52     bool ok = false;
53     // check that file is accessible
54     if ( exists(fileName) ) {
55       // check HDF5 && MED compatibility
56       med_bool hdfok, medok;
57       MEDfileCompatibility(fileName.c_str(), &hdfok, &medok);
58       if ( hdfok && medok ) {
59         med_idt aFid = MEDfileOpen(fileName.c_str(), MED_ACC_RDONLY);
60         if (aFid >= 0) {
61           med_int major, minor, release;
62           med_err ret = MEDfileNumVersionRd(aFid, &major, &minor, &release);
63           if (ret >= 0) {
64             int version = 100*major + minor;
65             if (version >= 202)
66               ok = true;
67           }
68         }
69         MEDfileClose(aFid);
70       }
71     }
72     return ok;
73   }
74
75   bool GetMEDVersion(const std::string& fileName, int& major, int& minor, int& release)
76   {
77     bool ok = false;
78     major = minor = release = 0;
79     med_idt aFid = MEDfileOpen(fileName.c_str(), MED_ACC_RDONLY);
80     if (aFid >= 0) {
81       med_int _major, _minor, _release;
82       med_err ret = MEDfileNumVersionRd(aFid, &_major, &_minor, &_release);
83       if (ret == 0) {
84         major = _major;
85         minor = _minor;
86         release = _release;
87         ok = true;
88       }
89       MEDfileClose(aFid);
90     }
91     return ok;
92   }
93
94   std::string GetMEDVersion(const std::string& fileName)
95   {
96     std::string version;
97     int major, minor, release;
98     if (GetMEDVersion(fileName, major, minor, release)) {
99       std::ostringstream os;
100       os << major << "." << minor << "." << release;
101       version = os.str();
102     }
103     return version;
104   }
105
106   PWrapper CrWrapperR(const std::string& fileName)
107   {
108     if (!CheckCompatibility(fileName)) {
109       EXCEPTION(std::runtime_error, "Cannot open file '"<<fileName<<"'.");
110     }
111     return new MED::TWrapper(fileName);
112   }
113
114   PWrapper CrWrapperW(const std::string& fileName)
115   {
116     if (!CheckCompatibility(fileName))
117       remove(fileName.c_str());
118     return new MED::TWrapper(fileName);
119   }
120 }