]> SALOME platform Git repositories - modules/smesh.git/blob - src/MEDWrapper/MED_Factory.cxx
Salome HOME
34f85e894a46baed11c84c971fa9c016a4693220
[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_V2_2_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 #ifdef _DEBUG_
40 static int MYDEBUG = 0;
41 #else
42 static int MYDEBUG = 0;
43 #endif
44
45 namespace MED
46 {
47   enum EStatus { eBad, eDeprecated, eGood };
48
49   EStatus GetVersionId(const std::string& theFileName)
50   {
51     INITMSG(MYDEBUG,"GetVersionId - theFileName = '"<<theFileName<<"'"<<std::endl);
52
53     EStatus status = eBad;    
54
55     bool ok = true;
56     // 1. Check that file is accessible
57 #ifdef WIN32
58     ok = (GetFileAttributes(xmlPath.c_str()) != INVALID_FILE_ATTRIBUTES);
59 #else
60     ok = (access(theFileName.c_str(), F_OK) == 0);
61 #endif
62     // 2. Check compatibility of hdf and med versions
63     if ( ok ) {
64       med_bool hdfok, medok;
65       MEDfileCompatibility(theFileName.c_str(), &hdfok, &medok);
66       ok = hdfok /*&& medok*/; // med-2.1 is KO since med-3.0.0
67     }
68     // 3. Try to open the file with MED API
69     if ( ok ) {
70       med_idt aFid = MEDfileOpen(theFileName.c_str(), MED_ACC_RDONLY);
71
72       MSG(MYDEBUG,"GetVersionId - theFileName = '"<<theFileName<<"'; aFid = "<<aFid<<std::endl);
73       if (aFid >= 0) {
74         med_int aMajor, aMinor, aRelease;
75         med_err aRet = MEDfileNumVersionRd(aFid, &aMajor, &aMinor, &aRelease);
76         INITMSG(MYDEBUG,"GetVersionId - theFileName = '"<<theFileName<<"'; aRet = "<<aRet<<std::endl);
77         if (aRet >= 0) {
78           if (aMajor == 2 && aMinor == 1)
79             status = eDeprecated;
80           else
81             status = eGood;
82         }
83         else {
84           // VSR: simulate med 2.3.6 behavior, med file version is assumed to 2.1
85           status = eDeprecated;
86         }
87       }
88       MEDfileClose(aFid);
89       ok = true;
90       BEGMSG(MYDEBUG,"GetVersionId - theFileName = '"<<theFileName<<"'; status = "<<status<<std::endl);
91     }
92     return status;
93   }
94
95   bool GetMEDVersion(const std::string& fname, int& major, int& minor, int& release)
96   {
97     med_idt f = MEDfileOpen(fname.c_str(), MED_ACC_RDONLY);
98     if ( f<0 )
99       return false;
100
101     med_int aMajor, aMinor, aRelease;
102     med_err aRet = MEDfileNumVersionRd(f, &aMajor, &aMinor, &aRelease);
103     major = aMajor;
104     minor = aMinor;
105     release = aRelease;
106     MEDfileClose( f );
107     if ( aRet<0 ) {
108       // VSR: simulate med 2.3.6 behavior, med file version is assumed to 2.1
109       major = 2; minor = release = -1;
110       //return false;
111     }
112     return true;
113   }
114
115   PWrapper CrWrapperR(const std::string& theFileName)
116   {
117     PWrapper aWrapper;
118     EStatus status = GetVersionId(theFileName);
119     switch (status) {
120     case eGood:
121       aWrapper.reset(new MED::V2_2::TVWrapper(theFileName));
122       break;
123     case eDeprecated:
124       EXCEPTION(std::runtime_error,"Cannot open file '"<<theFileName<<"'. Med version 2.1 is not supported any more.");
125       break;
126     default:
127       EXCEPTION(std::runtime_error,"MED::CrWrapper - theFileName = '"<<theFileName<<"'");
128       break;
129     }
130     return aWrapper;
131   }
132
133   PWrapper CrWrapperW(const std::string& theFileName)
134   {
135     EStatus status = GetVersionId(theFileName);
136
137     if (status != eGood)
138       remove(theFileName.c_str());
139     
140     PWrapper aWrapper;
141     aWrapper.reset(new MED::V2_2::TVWrapper(theFileName));
142
143     return aWrapper;
144   }
145 }