1 // Copyright (C) 2007-2020 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (CEA/DEN)
21 #include "MEDFileUtilities.hxx"
22 #include "MEDFileSafeCaller.txx"
23 #include "MEDLoaderBase.hxx"
24 #include "MEDLoader.hxx"
25 #include "MEDFileBasis.hxx"
27 #include "InterpKernelAutoPtr.hxx"
32 const char MEDCoupling::MEDFileWritableStandAlone::DFT_FILENAME_IN_MEM[]="DftFileNameInMemory";
34 med_access_mode MEDFileUtilities::TraduceWriteMode(int medloaderwritemode)
36 switch(medloaderwritemode)
45 throw INTERP_KERNEL::Exception("Invalid write mode specified ! must be 0(write with no question), 1(append) or 2(creation)");
49 const char *MEDFileUtilities::GetReadableMEDFieldType(med_field_type ft)
51 static const char medFloat64[]="MED_FLOAT64";
52 static const char medInt32[]="MED_INT32";
53 static const char medInt64[]="MED_INT64";
63 throw INTERP_KERNEL::Exception("Non supported field type ! Should be FLOAT64, INT32 or INT64 !");
67 void MEDFileUtilities::CheckMEDCode(int code, med_idt fid, const std::string& msg)
71 std::ostringstream oss;
72 oss << "MEDFile has returned an error code (" << code <<") : " << msg;
73 throw INTERP_KERNEL::Exception(oss.str().c_str());
77 void MEDFileUtilities::CheckFileForRead(const std::string& fileName)
79 int status=MEDLoaderBase::getStatusOfFile(fileName);
80 std::ostringstream oss;
81 oss << " File : \"" << fileName << "\"";
84 case MEDLoaderBase::DIR_LOCKED:
86 oss << " has been detected as unreadable : impossible to read anything !";
87 throw INTERP_KERNEL::Exception(oss.str().c_str());
89 case MEDLoaderBase::NOT_EXIST:
91 oss << " has been detected as NOT EXISTING : impossible to read anything !";
92 throw INTERP_KERNEL::Exception(oss.str().c_str());
94 case MEDLoaderBase::EXIST_WRONLY:
96 oss << " has been detected as WRITE ONLY : impossible to read anything !";
97 throw INTERP_KERNEL::Exception(oss.str().c_str());
100 AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
103 oss << " has been detected as unreadable by MED file : impossible to read anything !";
104 throw INTERP_KERNEL::Exception(oss.str().c_str());
106 oss << " has been detected readable but ";
107 med_int major,minor,release;
108 MEDfileNumVersionRd(fid,&major,&minor,&release);
109 if(major<2 || (major==2 && minor<2))
111 oss << "version of MED file is < 2.2 : impossible to read anything !";
112 throw INTERP_KERNEL::Exception(oss.str().c_str());
116 MEDFileUtilities::AutoFid::AutoFid(med_idt fid):_fid(fid)
120 MEDFileUtilities::AutoFid::~AutoFid()
125 MEDCoupling::MEDFileWritable::MEDFileWritable():_too_long_str(0),_zipconn_pol(2)
129 void MEDCoupling::MEDFileWritable::copyOptionsFrom(const MEDFileWritable& other) const
131 _too_long_str=other._too_long_str;
132 _zipconn_pol=other._zipconn_pol;
135 int MEDCoupling::MEDFileWritable::getTooLongStrPolicy() const
137 return _too_long_str;
140 void MEDCoupling::MEDFileWritable::setTooLongStrPolicy(int newVal)
142 if(newVal!=2 && newVal!=1 && newVal!=0)
143 throw INTERP_KERNEL::Exception("MEDFileWritable::setTooLongStrPolicy : invalid policy should be in 0,1 or 2 !");
144 _too_long_str=newVal;
147 int MEDCoupling::MEDFileWritable::getZipConnPolicy()
152 void MEDCoupling::MEDFileWritable::setZipConnPolicy(int newVal)
157 std::string MEDCoupling::MEDFileWritable::FileNameFromFID(med_idt fid)
159 med_int lgth(MEDfileName(fid,0,0));
161 return std::string();
162 INTERP_KERNEL::AutoPtr<char> tmp(new char[lgth+1]);
163 if(MEDfileName(fid,tmp,lgth)<0)
164 throw INTERP_KERNEL::Exception("MEDFileWritable::FileNameFromFID : Return code of MEDFile call \"MEDfileName\" is not >=0 as expected !");
165 return std::string(tmp);
168 MEDFileUtilities::AutoFid MEDCoupling::OpenMEDFileForRead(const std::string& fileName)
170 MEDFileUtilities::CheckFileForRead(fileName);
171 return MEDFileUtilities::AutoFid(MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY));
175 * Writes \a this mesh into a MED file specified by its name.
176 * \param [in] fileName - the MED file name.
177 * \param [in] mode - the writing mode. For more on \a mode, see \ref AdvMEDLoaderBasics.
178 * - 2 - erase; an existing file is removed.
179 * - 1 - append; same data should not be present in an existing file.
180 * - 0 - overwrite; same data present in an existing file is overwritten.
181 * \throw If the mesh name is not set.
182 * \throw If \a mode == 1 and the same data is present in an existing file.
184 void MEDCoupling::MEDFileWritableStandAlone::write(const std::string& fileName, int mode) const
186 med_access_mode medmod(MEDFileUtilities::TraduceWriteMode(mode));
187 MEDFileUtilities::AutoFid fid(MEDfileOpen(fileName.c_str(),medmod));
188 std::ostringstream oss; oss << "MEDFileWritableStandAlone : error on attempt to write in file : \"" << fileName << "\"";
189 MEDFileUtilities::CheckMEDCode((int)fid,fid,oss.str());
193 void MEDCoupling::MEDFileWritableStandAlone::write33(const std::string& fileName, int mode) const
195 this->writeXX(fileName,mode,3,3,1);
198 void MEDCoupling::MEDFileWritableStandAlone::write30(const std::string& fileName, int mode) const
200 this->writeXX(fileName,mode,3,0,6);
203 void MEDCoupling::MEDFileWritableStandAlone::writeXX(const std::string& fileName, int mode, med_int maj, med_int min, med_int rel) const
205 #if ( MED_NUM_MAJEUR>4 || ( MED_NUM_MAJEUR==4 && MED_NUM_MINEUR>=1 ) )
206 med_access_mode medmod(MEDFileUtilities::TraduceWriteMode(mode));
207 MEDFileUtilities::AutoFid fid(MEDfileVersionOpen(fileName.c_str(),medmod,maj,min,rel));
210 std::ostringstream oss; oss << "MEDFileWritableStandAlone::write" << maj << min << " : the MED version used to compile medcoupling is " << MEDFileVersionStr() << " ! If you need this feature please use version >= 4.1.";
211 throw INTERP_KERNEL::Exception(oss.str());
215 MEDCoupling::MCAuto<MEDCoupling::DataArrayByte> MEDCoupling::MEDFileWritableStandAlone::serialize() const
217 med_memfile memfile=MED_MEMFILE_INIT;
218 memfile.app_image_ptr=0;
219 memfile.app_image_size=0;
221 std::string dftFileName(GenerateUniqueDftFileNameInMem());
222 {// very important to let this braces ! The AutoFid destructor must be called, to have a "clean" memfile.app_image_ptr pointer embedded in the returned object.
223 MEDFileUtilities::AutoFid fid(MEDmemFileOpen(dftFileName.c_str(),&memfile,MED_FALSE,MED_ACC_CREAT));
227 MEDCoupling::MCAuto<MEDCoupling::DataArrayByte> ret(MEDCoupling::DataArrayByte::New());
228 ret->useArray(reinterpret_cast<char *>(memfile.app_image_ptr),true,DeallocType::C_DEALLOC,memfile.app_image_size,1);
232 std::string MEDCoupling::MEDFileWritableStandAlone::GenerateUniqueDftFileNameInMem()
235 std::ostringstream oss; oss << DFT_FILENAME_IN_MEM << "_" << ii++;
239 MEDCoupling::MEDFileCapability::MEDFileCapability(med_idt fid)
241 MEDFILESAFECALLERRD0(MEDfileNumVersionRd,(fid,&_maj,&_min,&_rel));