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