-// Copyright (C) 2007-2013 CEA/DEN, EDF R&D
+// Copyright (C) 2007-2024 CEA, EDF
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// Author : Anthony Geay (CEA/DEN)
#include "MEDFileUtilities.hxx"
+#include "MEDFileSafeCaller.txx"
#include "MEDLoaderBase.hxx"
+#include "MEDLoader.hxx"
+#include "MEDFileBasis.hxx"
+
+#include "InterpKernelAutoPtr.hxx"
#include <sstream>
+#include <fstream>
+
+const char MEDCoupling::MEDFileWritableStandAlone::DFT_FILENAME_IN_MEM[]="DftFileNameInMemory";
med_access_mode MEDFileUtilities::TraduceWriteMode(int medloaderwritemode)
{
switch(medloaderwritemode)
- {
+ {
case 2:
return MED_ACC_CREAT;
case 1:
return MED_ACC_RDWR;
default:
throw INTERP_KERNEL::Exception("Invalid write mode specified ! must be 0(write with no question), 1(append) or 2(creation)");
- }
+ }
}
const char *MEDFileUtilities::GetReadableMEDFieldType(med_field_type ft)
{
static const char medFloat64[]="MED_FLOAT64";
+ static const char medFloat32[]="MED_FLOAT32";
static const char medInt32[]="MED_INT32";
static const char medInt64[]="MED_INT64";
switch(ft)
- {
+ {
case MED_FLOAT64:
return medFloat64;
+ case MED_FLOAT32:
+ return medFloat32;
case MED_INT32:
return medInt32;
case MED_INT64:
return medInt64;
default:
- throw INTERP_KERNEL::Exception("Non supported field type ! Should be FLOAT64, INT32 or INT64 !");
- }
+ throw INTERP_KERNEL::Exception("Non supported field type ! Should be FLOAT64, FLOAT32, INT32 or INT64 !");
+ }
}
void MEDFileUtilities::CheckMEDCode(int code, med_idt fid, const std::string& msg)
std::ostringstream oss;
oss << " File : \"" << fileName << "\"";
switch(status)
- {
+ {
case MEDLoaderBase::DIR_LOCKED:
{
oss << " has been detected as unreadable : impossible to read anything !";
oss << " has been detected as WRITE ONLY : impossible to read anything !";
throw INTERP_KERNEL::Exception(oss.str().c_str());
}
- }
+ }
AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
if(fid<0)
{
throw INTERP_KERNEL::Exception(oss.str().c_str());
}
oss << " has been detected readable but ";
- int major,minor,release;
+ med_int major,minor,release;
MEDfileNumVersionRd(fid,&major,&minor,&release);
if(major<2 || (major==2 && minor<2))
{
{
}
-MEDFileUtilities::AutoFid::operator med_idt() const
-{
- return _fid;
-}
-
MEDFileUtilities::AutoFid::~AutoFid()
{
MEDfileClose(_fid);
}
-ParaMEDMEM::MEDFileWritable::MEDFileWritable():_too_long_str(0),_zipconn_pol(2)
+MEDCoupling::MEDFileWritable::MEDFileWritable():_too_long_str(0),_zipconn_pol(2)
{
}
-void ParaMEDMEM::MEDFileWritable::copyOptionsFrom(const MEDFileWritable& other) const
+void MEDCoupling::MEDFileWritable::copyOptionsFrom(const MEDFileWritable& other) const
{
_too_long_str=other._too_long_str;
_zipconn_pol=other._zipconn_pol;
}
-int ParaMEDMEM::MEDFileWritable::getTooLongStrPolicy() const
+int MEDCoupling::MEDFileWritable::getTooLongStrPolicy() const
{
return _too_long_str;
}
-void ParaMEDMEM::MEDFileWritable::setTooLongStrPolicy(int newVal)
+void MEDCoupling::MEDFileWritable::setTooLongStrPolicy(int newVal)
{
if(newVal!=2 && newVal!=1 && newVal!=0)
throw INTERP_KERNEL::Exception("MEDFileWritable::setTooLongStrPolicy : invalid policy should be in 0,1 or 2 !");
_too_long_str=newVal;
}
-int ParaMEDMEM::MEDFileWritable::getZipConnPolicy()
+int MEDCoupling::MEDFileWritable::getZipConnPolicy()
{
return _zipconn_pol;
}
-void ParaMEDMEM::MEDFileWritable::setZipConnPolicy(int newVal)
+void MEDCoupling::MEDFileWritable::setZipConnPolicy(int newVal)
{
_zipconn_pol=newVal;
}
+
+std::string MEDCoupling::MEDFileWritable::FileNameFromFID(med_idt fid)
+{
+ med_int lgth(MEDfileName(fid,0,0));
+ if(lgth<=0)
+ return std::string();
+ INTERP_KERNEL::AutoPtr<char> tmp(new char[lgth+1]);
+ if(MEDfileName(fid,tmp,lgth)<0)
+ throw INTERP_KERNEL::Exception("MEDFileWritable::FileNameFromFID : Return code of MEDFile call \"MEDfileName\" is not >=0 as expected !");
+ return std::string(tmp);
+}
+
+MEDFileUtilities::AutoFid MEDCoupling::OpenMEDFileForRead(const std::string& fileName)
+{
+ MEDFileUtilities::CheckFileForRead(fileName);
+ return MEDFileUtilities::AutoFid(MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY));
+}
+
+/*!
+ * Writes \a this mesh into a MED file specified by its name.
+ * \param [in] fileName - the MED file name.
+ * \param [in] mode - the writing mode. For more on \a mode, see \ref AdvMEDLoaderBasics.
+ * - 2 - erase; an existing file is removed.
+ * - 1 - append; same data should not be present in an existing file.
+ * - 0 - overwrite; same data present in an existing file is overwritten.
+ * \throw If the mesh name is not set.
+ * \throw If \a mode == 1 and the same data is present in an existing file.
+ */
+void MEDCoupling::MEDFileWritableStandAlone::write(const std::string& fileName, int mode) const
+{
+ med_access_mode medmod(MEDFileUtilities::TraduceWriteMode(mode));
+ MEDFileUtilities::AutoFid fid(MEDfileOpen(fileName.c_str(),medmod));
+ std::ostringstream oss; oss << "MEDFileWritableStandAlone : error on attempt to write in file : \"" << fileName << "\"";
+ MEDFileUtilities::CheckMEDCode((int)fid,fid,oss.str());
+ writeLL(fid);
+}
+
+void MEDCoupling::MEDFileWritableStandAlone::write33(const std::string& fileName, int mode) const
+{
+ this->writeXX(fileName,mode,3,3,1);
+}
+
+void MEDCoupling::MEDFileWritableStandAlone::write30(const std::string& fileName, int mode) const
+{
+ this->writeXX(fileName,mode,3,0,6);
+}
+
+void MEDCoupling::MEDFileWritableStandAlone::write40(const std::string& fileName, int mode) const
+{
+ this->writeXX(fileName,mode,4,0,1);
+}
+
+void MEDCoupling::MEDFileWritableStandAlone::writeXX(const std::string& fileName, int mode, med_int maj, med_int min, med_int rel) const
+{
+#if ( MED_NUM_MAJEUR>4 || ( MED_NUM_MAJEUR==4 && MED_NUM_MINEUR>=1 ) )
+ med_access_mode medmod(MEDFileUtilities::TraduceWriteMode(mode));
+ MEDFileUtilities::AutoFid fid(MEDfileVersionOpen(fileName.c_str(),medmod,maj,min,rel));
+ writeLL(fid);
+#else
+ 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.";
+ throw INTERP_KERNEL::Exception(oss.str());
+#endif
+}
+
+MEDCoupling::MCAuto<MEDCoupling::DataArrayByte> MEDCoupling::MEDFileWritableStandAlone::serialize() const
+{
+ med_memfile memfile=MED_MEMFILE_INIT;
+ memfile.app_image_ptr=0;
+ memfile.app_image_size=0;
+ //
+ std::string dftFileName(GenerateUniqueDftFileNameInMem());
+ {// 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.
+ MEDFileUtilities::AutoFid fid(MEDmemFileOpen(dftFileName.c_str(),&memfile,MED_FALSE,MED_ACC_CREAT));
+ writeLL(fid);
+ }
+ //
+ MEDCoupling::MCAuto<MEDCoupling::DataArrayByte> ret(MEDCoupling::DataArrayByte::New());
+ ret->useArray(reinterpret_cast<char *>(memfile.app_image_ptr),true,DeallocType::C_DEALLOC,memfile.app_image_size,1);
+ return ret;
+}
+
+std::string MEDCoupling::MEDFileWritableStandAlone::GenerateUniqueDftFileNameInMem()
+{
+ static int ii=0;
+ std::ostringstream oss; oss << DFT_FILENAME_IN_MEM << "_" << ii++;
+ return oss.str();
+}
+
+MEDCoupling::MEDFileCapability::MEDFileCapability(med_idt fid)
+{
+ MEDFILESAFECALLERRD0(MEDfileNumVersionRd,(fid,&_maj,&_min,&_rel));
+}