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