1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "MED_Factory.hxx"
24 #include "MED_Utilities.hxx"
25 #include "MED_Wrapper.hxx"
38 #include <utilities.h>
47 bool exists(const std::string& fileName)
51 size_t length = strlen(fileName.c_str()) + sizeof(char);
52 wchar_t* path = new wchar_t[length];
53 memset(path, '\0', length);
54 mbstowcs(path, fileName.c_str(), length);
56 cosnt char* path = xmlPath.c_str();
58 return (GetFileAttributes(path) != INVALID_FILE_ATTRIBUTES);
60 return (access(fileName.c_str(), F_OK) == 0);
64 bool CheckCompatibility(const std::string& fileName, bool isForAppend)
67 // check that file is accessible
68 if ( exists(fileName) ) {
69 // check HDF5 && MED compatibility
70 med_bool hdfok, medok;
71 med_err r0 = MEDfileCompatibility(fileName.c_str(), &hdfok, &medok);
72 //MESSAGE(r0 << " " << hdfok << " " << medok);
73 if ( r0==0 && hdfok && medok ) {
74 med_idt aFid = MEDfileOpen(fileName.c_str(), MED_ACC_RDONLY);
76 med_int major, minor, release;
77 med_err ret = MEDfileNumVersionRd(aFid, &major, &minor, &release);
78 //MESSAGE(ret << " " << major << "." << minor << "." << release);
80 bool isReadOnly = !isForAppend;
81 if ( isReadOnly || ((major == MED_MAJOR_NUM) && (minor == MED_MINOR_NUM)))
91 bool GetMEDVersion(const std::string& fileName, int& major, int& minor, int& release)
94 major = minor = release = 0;
95 med_idt aFid = MEDfileOpen(fileName.c_str(), MED_ACC_RDONLY);
97 med_int _major, _minor, _release;
98 med_err ret = MEDfileNumVersionRd(aFid, &_major, &_minor, &_release);
110 std::string GetMEDVersion(const std::string& fileName)
113 int major, minor, release;
114 if (GetMEDVersion(fileName, major, minor, release)) {
115 std::ostringstream os;
116 os << major << "." << minor << "." << release;
122 PWrapper CrWrapperR(const std::string& fileName)
124 if (!CheckCompatibility(fileName)) {
125 EXCEPTION(std::runtime_error, "Cannot open file '"<<fileName<<"'.");
127 return new MED::TWrapper(fileName);
130 PWrapper CrWrapperW(const std::string& fileName, int theMinor)
132 if (!CheckCompatibility(fileName, true))
133 remove(fileName.c_str());
134 return new MED::TWrapper(fileName, theMinor);