Back to index

salome-med  6.5.0
MED_Factory.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  File   : 
00024 //  Author : 
00025 //  Module : 
00026 //
00027 #include "MED_Factory.hxx"
00028 #include "MED_Utilities.hxx"
00029 #include "MED_V2_2_Wrapper.hxx"
00030 #include "MED_V2_1_Wrapper.hxx"
00031 
00032 #include <stdio.h>
00033 #include <sstream>
00034 
00035 extern "C"
00036 {
00037 #include <med.h>
00038 #ifndef WIN32
00039   #include <unistd.h>
00040 #endif
00041 }
00042 
00043 #ifdef _DEBUG_
00044 static int MYDEBUG = 0;
00045 #else
00046 static int MYDEBUG = 0;
00047 #endif
00048 
00049 namespace MED
00050 {
00051   
00052   EVersion GetVersionId(const std::string& theFileName,
00053                         bool theDoPreCheckInSeparateProcess)
00054   {
00055     INITMSG(MYDEBUG,"GetVersionId - theFileName = '"<<theFileName<<"'"<<std::endl);
00056     EVersion aVersion = eVUnknown;    
00057 
00058 #ifndef WIN32
00059     if (access(theFileName.c_str(),F_OK))
00060       return aVersion;
00061     if(theDoPreCheckInSeparateProcess){
00062       // First check, is it possible to deal with the file
00063       std::ostringstream aStr;
00064       // File name is in quotes for the case of space(s) inside it (PAL13009)
00065       aStr<<"bash -c \""<<getenv("MED_ROOT_DIR")<<"/bin/salome/mprint_version \'"<<theFileName<<"\'\"";
00066       if(!MYDEBUG)
00067         aStr<<" 2>&1 > /dev/null";
00068 
00069       std::string aCommand = aStr.str();
00070       int aStatus = system(aCommand.c_str());
00071 
00072       BEGMSG(MYDEBUG,"aCommand = '"<<aCommand<<"'; aStatus = "<<aStatus<<std::endl);
00073       if(aStatus != 0)
00074         return aVersion;
00075     }
00076 #endif
00077     // check compatibility of hdf and med versions
00078     med_bool hdfok, medok;
00079     MEDfileCompatibility(theFileName.c_str(), &hdfok, &medok);
00080     if ((!hdfok) /*|| (!medok)*/) // med-2.1 is KO since med-3.0.0
00081       return aVersion;
00082 
00083     // Next, try to open the file trough the MED API
00084     const char* aFileName = theFileName.c_str();
00085     med_idt aFid = MEDfileOpen(aFileName,MED_ACC_RDONLY);
00086 
00087     MSG(MYDEBUG,"GetVersionId - theFileName = '"<<theFileName<<"'; aFid = "<<aFid<<std::endl);
00088     if(aFid >= 0){
00089       med_int aMajor, aMinor, aRelease;
00090       med_err aRet = MEDfileNumVersionRd(aFid,&aMajor,&aMinor,&aRelease);
00091       INITMSG(MYDEBUG,"GetVersionId - theFileName = '"<<theFileName<<"'; aRet = "<<aRet<<std::endl);
00092       if(aRet >= 0){
00093         if(aMajor == 2 && aMinor == 1)
00094           aVersion = eV2_1;
00095         else
00096           aVersion = eV2_2;
00097       }
00098       else {
00099         // VSR: simulate med 2.3.6 behavior, med file version is assumed to 2.1
00100         aVersion = eV2_1;
00101       }
00102     }
00103     MEDfileClose(aFid);
00104 
00105     BEGMSG(MYDEBUG,"GetVersionId - theFileName = '"<<theFileName<<"'; aVersion = "<<aVersion<<std::endl);
00106     return aVersion;
00107   }
00108 
00109   bool getMEDVersion( const std::string& fname, int& major, int& minor, int& release )
00110   {
00111     med_idt f = MEDfileOpen(fname.c_str(), MED_ACC_RDONLY );
00112     if( f<0 )
00113       return false;
00114 
00115     med_int aMajor, aMinor, aRelease;
00116     med_err aRet = MEDfileNumVersionRd( f, &aMajor, &aMinor, &aRelease );
00117     major = aMajor;
00118     minor = aMinor;
00119     release = aRelease;
00120     MEDfileClose( f );
00121     if( aRet<0 ) {
00122       // VSR: simulate med 2.3.6 behavior, med file version is assumed to 2.1
00123       major = 2; minor = release = -1;
00124       //return false;
00125     }
00126     return true;
00127   }
00128 
00129   PWrapper CrWrapper(const std::string& theFileName,
00130                      bool theDoPreCheckInSeparateProcess)
00131   {
00132     PWrapper aWrapper;
00133     EVersion aVersion = GetVersionId(theFileName,theDoPreCheckInSeparateProcess);
00134     switch(aVersion){
00135     case eV2_2:
00136       aWrapper.reset(new MED::V2_2::TVWrapper(theFileName));
00137       break;
00138     case eV2_1:
00139       aWrapper.reset(new MED::V2_1::TVWrapper(theFileName));
00140       break;
00141     default:
00142       EXCEPTION(std::runtime_error,"MED::CrWrapper - theFileName = '"<<theFileName<<"'");
00143     }
00144     return aWrapper;
00145   }
00146 
00147   PWrapper CrWrapper(const std::string& theFileName, EVersion theId)
00148   {
00149     EVersion aVersion = GetVersionId(theFileName);
00150 
00151     if(aVersion != theId)
00152       remove(theFileName.c_str());
00153     
00154     PWrapper aWrapper;
00155     switch(theId){
00156     case eV2_2:
00157       aWrapper.reset(new MED::V2_2::TVWrapper(theFileName));
00158       break;
00159     case eV2_1:
00160       aWrapper.reset(new MED::V2_1::TVWrapper(theFileName));
00161       break;
00162     default:
00163       aWrapper.reset(new MED::V2_2::TVWrapper(theFileName));
00164     }
00165     return aWrapper;
00166   }
00167 
00168 }