Salome HOME
CoTech decision: move MEDWrapper from MED to SMESH
[modules/smesh.git] / src / MEDWrapper / Factory / MED_Factory.cxx
1 // Copyright (C) 2007-2013  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.
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 #include "MED_Factory.hxx"
23 #include "MED_Utilities.hxx"
24 #include "MED_V2_2_Wrapper.hxx"
25
26 #include <stdio.h>
27 #include <sstream>
28
29 extern "C"
30 {
31 #include <med.h>
32 #ifndef WIN32
33   #include <unistd.h>
34 #endif
35 }
36
37 #ifdef _DEBUG_
38 static int MYDEBUG = 0;
39 #else
40 static int MYDEBUG = 0;
41 #endif
42
43 namespace MED
44 {
45   
46   EVersion GetVersionId(const std::string& theFileName,
47                         bool theDoPreCheckInSeparateProcess)
48   {
49     INITMSG(MYDEBUG,"GetVersionId - theFileName = '"<<theFileName<<"'"<<std::endl);
50     EVersion aVersion = eVUnknown;    
51
52 #ifndef WIN32
53     if (access(theFileName.c_str(),F_OK))
54       return aVersion;
55     if(theDoPreCheckInSeparateProcess){
56       // First check, is it possible to deal with the file
57       std::ostringstream aStr;
58       // File name is in quotes for the case of space(s) inside it (PAL13009)
59       aStr<<"bash -c \""<<getenv("MED_ROOT_DIR")<<"/bin/salome/mprint_version \'"<<theFileName<<"\'\"";
60       if(!MYDEBUG)
61         aStr<<" 2>&1 > /dev/null";
62
63       std::string aCommand = aStr.str();
64       int aStatus = system(aCommand.c_str());
65
66       BEGMSG(MYDEBUG,"aCommand = '"<<aCommand<<"'; aStatus = "<<aStatus<<std::endl);
67       if(aStatus != 0)
68         return aVersion;
69     }
70 #endif
71     // check compatibility of hdf and med versions
72     med_bool hdfok, medok;
73     MEDfileCompatibility(theFileName.c_str(), &hdfok, &medok);
74     if ((!hdfok) /*|| (!medok)*/) // med-2.1 is KO since med-3.0.0
75       return aVersion;
76
77     // Next, try to open the file trough the MED API
78     const char* aFileName = theFileName.c_str();
79     med_idt aFid = MEDfileOpen(aFileName,MED_ACC_RDONLY);
80
81     MSG(MYDEBUG,"GetVersionId - theFileName = '"<<theFileName<<"'; aFid = "<<aFid<<std::endl);
82     if(aFid >= 0){
83       med_int aMajor, aMinor, aRelease;
84       med_err aRet = MEDfileNumVersionRd(aFid,&aMajor,&aMinor,&aRelease);
85       INITMSG(MYDEBUG,"GetVersionId - theFileName = '"<<theFileName<<"'; aRet = "<<aRet<<std::endl);
86       if(aRet >= 0){
87         if(aMajor == 2 && aMinor == 1)
88           aVersion = eV2_1;
89         else
90           aVersion = eV2_2;
91       }
92       else {
93         // VSR: simulate med 2.3.6 behavior, med file version is assumed to 2.1
94         aVersion = eV2_1;
95       }
96     }
97     MEDfileClose(aFid);
98
99     BEGMSG(MYDEBUG,"GetVersionId - theFileName = '"<<theFileName<<"'; aVersion = "<<aVersion<<std::endl);
100     return aVersion;
101   }
102
103   bool getMEDVersion( const std::string& fname, int& major, int& minor, int& release )
104   {
105     med_idt f = MEDfileOpen(fname.c_str(), MED_ACC_RDONLY );
106     if( f<0 )
107       return false;
108
109     med_int aMajor, aMinor, aRelease;
110     med_err aRet = MEDfileNumVersionRd( f, &aMajor, &aMinor, &aRelease );
111     major = aMajor;
112     minor = aMinor;
113     release = aRelease;
114     MEDfileClose( f );
115     if( aRet<0 ) {
116       // VSR: simulate med 2.3.6 behavior, med file version is assumed to 2.1
117       major = 2; minor = release = -1;
118       //return false;
119     }
120     return true;
121   }
122
123   PWrapper CrWrapper(const std::string& theFileName,
124                      bool theDoPreCheckInSeparateProcess)
125   {
126     PWrapper aWrapper;
127     EVersion aVersion = GetVersionId(theFileName,theDoPreCheckInSeparateProcess);
128     switch(aVersion){
129     case eV2_2:
130       aWrapper.reset(new MED::V2_2::TVWrapper(theFileName));
131       break;
132     case eV2_1:
133       EXCEPTION(std::runtime_error,"Cannot open file '"<<theFileName<<"'. Med version 2.1 is not supported any more.");
134       //aWrapper.reset(new MED::V2_1::TVWrapper(theFileName));
135       break;
136     default:
137       EXCEPTION(std::runtime_error,"MED::CrWrapper - theFileName = '"<<theFileName<<"'");
138     }
139     return aWrapper;
140   }
141
142   PWrapper CrWrapper(const std::string& theFileName, EVersion theId)
143   {
144     EVersion aVersion = GetVersionId(theFileName);
145
146     if(aVersion != theId)
147       remove(theFileName.c_str());
148     
149     PWrapper aWrapper;
150     switch(theId){
151     case eV2_2:
152       aWrapper.reset(new MED::V2_2::TVWrapper(theFileName));
153       break;
154     case eV2_1:
155       EXCEPTION(std::runtime_error,"Cannot open file '"<<theFileName<<"'. Med version 2.1 is not supported any more.");
156       //aWrapper.reset(new MED::V2_1::TVWrapper(theFileName));
157       break;
158     default:
159       aWrapper.reset(new MED::V2_2::TVWrapper(theFileName));
160     }
161     return aWrapper;
162   }
163
164 }