Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/med.git] / src / MEDWrapper / Factory / MED_Factory.cxx
1 //  
2 //
3 //  Copyright (C) 2003  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 //
23 //
24 //  File   : 
25 //  Author : 
26 //  Module : 
27 //  $Header$
28
29 #include "MED_Factory.hxx"
30 #include "MED_Utilities.hxx"
31 #include "MED_V2_2_Wrapper.hxx"
32 #include "MED_V2_1_Wrapper.hxx"
33
34 #include <stdio.h>
35 #include <strstream>
36
37 extern "C"
38 {
39 #include <med.h>
40 }
41
42 #ifdef _DEBUG_
43 static int MYDEBUG = 0;
44 #else
45 static int MYDEBUG = 0;
46 #endif
47
48 namespace MED
49 {
50   
51   EVersion GetVersionId(const std::string& theFileName,
52                         bool theDoPreCheckInSeparateProcess)
53   {
54     INITMSG(MYDEBUG,"GetVersionId - theFileName = '"<<theFileName<<"'"<<std::endl);
55     EVersion aVersion = eVUnknown;    
56
57 #ifndef WIN32
58     if(theDoPreCheckInSeparateProcess){
59       // First check, is it possible to deal with the file
60       std::ostringstream aStr;
61       // File name is in quotes for the case of space(s) inside it (PAL13009)
62       aStr<<"bash -c \""<<getenv("MED_ROOT_DIR")<<"/bin/salome/mprint_version \'"<<theFileName<<"\'\"";
63       if(!MYDEBUG)
64         aStr<<" 2>&1 > /dev/null";
65       
66       std::string aCommand = aStr.str();
67       int aStatus = system(aCommand.c_str());
68       
69       BEGMSG(MYDEBUG,"aCommand = '"<<aCommand<<"'; aStatus = "<<aStatus<<std::endl);
70       if(aStatus != 0)
71         return aVersion;
72     }
73 #endif
74
75     // Next, try to open the file trough the MED API
76     char* aFileName = const_cast<char*>(theFileName.c_str());
77     med_idt aFid = MEDouvrir(aFileName,MED_LECTURE);
78
79     MSG(MYDEBUG,"GetVersionId - theFileName = '"<<theFileName<<"'; aFid = "<<aFid<<std::endl);
80     if(aFid >= 0){
81       med_int aMajor, aMinor, aRelease;
82       med_err aRet = MEDversionLire(aFid,&aMajor,&aMinor,&aRelease);
83       INITMSG(MYDEBUG,"GetVersionId - theFileName = '"<<theFileName<<"'; aRet = "<<aRet<<std::endl);
84       if(aRet >= 0){
85         if(aMajor >= 2 && aMinor >= 2)
86           aVersion = eV2_2;
87         else
88           aVersion = eV2_1;
89       }
90     }
91     MEDfermer(aFid);
92
93     BEGMSG(MYDEBUG,"GetVersionId - theFileName = '"<<theFileName<<"'; aVersion = "<<aVersion<<std::endl);
94     return aVersion;
95   }
96
97   PWrapper CrWrapper(const std::string& theFileName,
98                      bool theDoPreCheckInSeparateProcess)
99   {
100     PWrapper aWrapper;
101     EVersion aVersion = GetVersionId(theFileName,theDoPreCheckInSeparateProcess);
102     switch(aVersion){
103     case eV2_2:
104       aWrapper.reset(new MED::V2_2::TVWrapper(theFileName));
105       break;
106     case eV2_1:
107       aWrapper.reset(new MED::V2_1::TVWrapper(theFileName));
108       break;
109     default:
110       EXCEPTION(std::runtime_error,"MED::CrWrapper - theFileName = '"<<theFileName<<"'");
111     }
112     return aWrapper;
113   }
114
115   PWrapper CrWrapper(const std::string& theFileName, EVersion theId)
116   {
117     EVersion aVersion = GetVersionId(theFileName);
118
119     if(aVersion != theId)
120       remove(theFileName.c_str());
121     
122     PWrapper aWrapper;
123     switch(theId){
124     case eV2_2:
125       aWrapper.reset(new MED::V2_2::TVWrapper(theFileName));
126       break;
127     case eV2_1:
128       aWrapper.reset(new MED::V2_1::TVWrapper(theFileName));
129       break;
130     }
131     return aWrapper;
132   }
133
134 }