X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FMEDLoader%2FMEDFileUtilities.cxx;h=d309257f5c9b47bc96286e77bf1c67d924bbbc78;hb=ac1df6b0ba8b337555fb39610c89f678d889580d;hp=4aaf594477c7db0a43230999a8bf5c3601af1c4b;hpb=1e36a6710aab710674e20fbd89f6a9a8f238c023;p=tools%2Fmedcoupling.git diff --git a/src/MEDLoader/MEDFileUtilities.cxx b/src/MEDLoader/MEDFileUtilities.cxx index 4aaf59447..d309257f5 100644 --- a/src/MEDLoader/MEDFileUtilities.cxx +++ b/src/MEDLoader/MEDFileUtilities.cxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2015 CEA/DEN, EDF R&D +// Copyright (C) 2007-2016 CEA/DEN, EDF R&D // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -20,9 +20,14 @@ #include "MEDFileUtilities.hxx" #include "MEDLoaderBase.hxx" +#include "MEDLoader.hxx" + +#include "InterpKernelAutoPtr.hxx" #include +const char MEDCoupling::MEDFileWritableStandAlone::DFT_FILENAME_IN_MEM[]="DftFileNameInMemory"; + med_access_mode MEDFileUtilities::TraduceWriteMode(int medloaderwritemode) { switch(medloaderwritemode) @@ -145,3 +150,75 @@ void MEDCoupling::MEDFileWritable::setZipConnPolicy(int newVal) { _zipconn_pol=newVal; } + +std::string MEDCoupling::MEDFileWritable::FileNameFromFID(med_idt fid) +{ + int lgth(MEDfileName(fid,0,0)); + if(lgth<=0) + return std::string(); + INTERP_KERNEL::AutoPtr 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(fid,fid,oss.str()); + writeLL(fid); +} + +void MEDCoupling::MEDFileWritableStandAlone::write30(const std::string& fileName, int mode) const +{ + med_access_mode medmod(MEDFileUtilities::TraduceWriteMode(mode)); +#if MED_NUM_MAJEUR>3 || ( MED_NUM_MAJEUR==3 && ( (MED_NUM_MINEUR==2 && MED_NUM_RELEASE>=1) || MED_NUM_MINEUR>=3) ) + MEDFileUtilities::AutoFid fid(MEDfileVersionOpen(fileName.c_str(),medmod,3,0,0)); + writeLL(fid); +#else + std::ostringstream oss; oss << "MEDFileWritableStandAlone::write30 : is implemented with MEDFile " << MEDFileVersionStr() << " ! If you need this feature please use version >= 3.2.1."; + throw INTERP_KERNEL::Exception(oss.str()); +#endif +} + +MEDCoupling::MCAuto 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 ret(MEDCoupling::DataArrayByte::New()); + ret->useArray(reinterpret_cast(memfile.app_image_ptr),true,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(); +}