Salome HOME
feat: new crackAlong method
[tools/medcoupling.git] / src / MEDLoader / MEDFileUtilities.cxx
1 // Copyright (C) 2007-2024  CEA, EDF
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (CEA/DEN)
20
21 #include "MEDFileUtilities.hxx"
22 #include "MEDFileSafeCaller.txx"
23 #include "MEDLoaderBase.hxx"
24 #include "MEDLoader.hxx"
25 #include "MEDFileBasis.hxx"
26
27 #include "InterpKernelAutoPtr.hxx"
28
29 #include <sstream>
30 #include <fstream>
31
32 const char MEDCoupling::MEDFileWritableStandAlone::DFT_FILENAME_IN_MEM[]="DftFileNameInMemory";
33
34 med_access_mode MEDFileUtilities::TraduceWriteMode(int medloaderwritemode)
35 {
36   switch(medloaderwritemode)
37   {
38     case 2:
39       return MED_ACC_CREAT;
40     case 1:
41       return MED_ACC_RDEXT;
42     case 0:
43       return MED_ACC_RDWR;
44     default:
45       throw INTERP_KERNEL::Exception("Invalid write mode specified ! must be 0(write with no question), 1(append) or 2(creation)");
46   }
47 }
48
49 const char *MEDFileUtilities::GetReadableMEDFieldType(med_field_type ft)
50 {
51   static const char medFloat64[]="MED_FLOAT64";
52   static const char medFloat32[]="MED_FLOAT32";
53   static const char medInt32[]="MED_INT32";
54   static const char medInt64[]="MED_INT64";
55   switch(ft)
56   {
57     case MED_FLOAT64:
58       return medFloat64;
59     case MED_FLOAT32:
60       return medFloat32;
61     case MED_INT32:
62       return medInt32;
63     case MED_INT64:
64       return medInt64;
65     default:
66       throw INTERP_KERNEL::Exception("Non supported field type ! Should be FLOAT64, FLOAT32, INT32 or INT64 !");
67   }
68 }
69
70 void MEDFileUtilities::CheckMEDCode(int code, med_idt fid, const std::string& msg)
71 {
72   if(code<0)
73     {
74       std::ostringstream oss;
75       oss << "MEDFile has returned an error code (" << code <<") : " << msg;
76       throw INTERP_KERNEL::Exception(oss.str().c_str());
77     }
78 }
79
80 void MEDFileUtilities::CheckFileForRead(const std::string& fileName)
81 {
82   int status=MEDLoaderBase::getStatusOfFile(fileName);
83   std::ostringstream oss;
84   oss << " File : \"" << fileName << "\"";
85   switch(status)
86   {
87     case MEDLoaderBase::DIR_LOCKED:
88       {
89         oss << " has been detected as unreadable : impossible to read anything !";
90         throw INTERP_KERNEL::Exception(oss.str().c_str());
91       }
92     case MEDLoaderBase::NOT_EXIST:
93       {
94         oss << " has been detected as NOT EXISTING : impossible to read anything !";
95         throw INTERP_KERNEL::Exception(oss.str().c_str());
96       }
97     case MEDLoaderBase::EXIST_WRONLY:
98       {
99         oss << " has been detected as WRITE ONLY : impossible to read anything !";
100         throw INTERP_KERNEL::Exception(oss.str().c_str());
101       }
102   }
103   AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
104   if(fid<0)
105     {
106       oss << " has been detected as unreadable by MED file : impossible to read anything !";
107       throw INTERP_KERNEL::Exception(oss.str().c_str());
108     }
109   oss << " has been detected readable but ";
110   med_int major,minor,release;
111   MEDfileNumVersionRd(fid,&major,&minor,&release);
112   if(major<2 || (major==2 && minor<2))
113     {
114       oss << "version of MED file is < 2.2 : impossible to read anything !";
115       throw INTERP_KERNEL::Exception(oss.str().c_str());
116     }
117 }
118
119 MEDFileUtilities::AutoFid::AutoFid(med_idt fid):_fid(fid)
120 {
121 }
122
123 MEDFileUtilities::AutoFid::~AutoFid()
124 {
125   MEDfileClose(_fid);
126 }
127
128 MEDCoupling::MEDFileWritable::MEDFileWritable():_too_long_str(0),_zipconn_pol(2)
129 {
130 }
131
132 void MEDCoupling::MEDFileWritable::copyOptionsFrom(const MEDFileWritable& other) const
133 {
134   _too_long_str=other._too_long_str;
135   _zipconn_pol=other._zipconn_pol;
136 }
137
138 int MEDCoupling::MEDFileWritable::getTooLongStrPolicy() const
139 {
140   return _too_long_str;
141 }
142
143 void MEDCoupling::MEDFileWritable::setTooLongStrPolicy(int newVal)
144 {
145   if(newVal!=2 && newVal!=1 && newVal!=0)
146     throw INTERP_KERNEL::Exception("MEDFileWritable::setTooLongStrPolicy : invalid policy should be in 0,1 or 2 !");
147   _too_long_str=newVal;
148 }
149
150 int MEDCoupling::MEDFileWritable::getZipConnPolicy()
151 {
152   return _zipconn_pol;
153 }
154
155 void MEDCoupling::MEDFileWritable::setZipConnPolicy(int newVal)
156 {
157   _zipconn_pol=newVal;
158 }
159
160 std::string MEDCoupling::MEDFileWritable::FileNameFromFID(med_idt fid)
161 {
162   med_int lgth(MEDfileName(fid,0,0));
163   if(lgth<=0)
164     return std::string();
165   INTERP_KERNEL::AutoPtr<char> tmp(new char[lgth+1]);
166   if(MEDfileName(fid,tmp,lgth)<0)
167     throw INTERP_KERNEL::Exception("MEDFileWritable::FileNameFromFID : Return code of MEDFile call \"MEDfileName\" is not >=0 as expected !");
168   return std::string(tmp);
169 }
170
171 MEDFileUtilities::AutoFid MEDCoupling::OpenMEDFileForRead(const std::string& fileName)
172 {
173   MEDFileUtilities::CheckFileForRead(fileName);
174   return MEDFileUtilities::AutoFid(MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY));
175 }
176
177 /*!
178  * Writes \a this mesh into a MED file specified by its name.
179  *  \param [in] fileName - the MED file name.
180  *  \param [in] mode - the writing mode. For more on \a mode, see \ref AdvMEDLoaderBasics.
181  * - 2 - erase; an existing file is removed.
182  * - 1 - append; same data should not be present in an existing file.
183  * - 0 - overwrite; same data present in an existing file is overwritten.
184  *  \throw If the mesh name is not set.
185  *  \throw If \a mode == 1 and the same data is present in an existing file.
186  */
187 void MEDCoupling::MEDFileWritableStandAlone::write(const std::string& fileName, int mode) const
188 {
189   med_access_mode medmod(MEDFileUtilities::TraduceWriteMode(mode));
190   MEDFileUtilities::AutoFid fid(MEDfileOpen(fileName.c_str(),medmod));
191   std::ostringstream oss; oss << "MEDFileWritableStandAlone : error on attempt to write in file : \"" << fileName << "\""; 
192   MEDFileUtilities::CheckMEDCode((int)fid,fid,oss.str());
193   writeLL(fid);
194 }
195
196 void MEDCoupling::MEDFileWritableStandAlone::write33(const std::string& fileName, int mode) const
197 {
198   this->writeXX(fileName,mode,3,3,1);
199 }
200
201 void MEDCoupling::MEDFileWritableStandAlone::write30(const std::string& fileName, int mode) const
202 {
203   this->writeXX(fileName,mode,3,0,6);
204 }
205
206 void MEDCoupling::MEDFileWritableStandAlone::write40(const std::string& fileName, int mode) const
207 {
208   this->writeXX(fileName,mode,4,0,1);
209 }
210
211 void MEDCoupling::MEDFileWritableStandAlone::writeXX(const std::string& fileName, int mode, med_int maj, med_int min, med_int rel) const
212 {
213 #if ( MED_NUM_MAJEUR>4 || ( MED_NUM_MAJEUR==4 && MED_NUM_MINEUR>=1 ) )
214   med_access_mode medmod(MEDFileUtilities::TraduceWriteMode(mode));
215   MEDFileUtilities::AutoFid fid(MEDfileVersionOpen(fileName.c_str(),medmod,maj,min,rel));
216   writeLL(fid);
217 #else
218   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.";
219   throw INTERP_KERNEL::Exception(oss.str());
220 #endif
221 }
222
223 MEDCoupling::MCAuto<MEDCoupling::DataArrayByte> MEDCoupling::MEDFileWritableStandAlone::serialize() const
224 {
225   med_memfile memfile=MED_MEMFILE_INIT;
226   memfile.app_image_ptr=0;
227   memfile.app_image_size=0;
228   //
229   std::string dftFileName(GenerateUniqueDftFileNameInMem());
230   {// 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.
231     MEDFileUtilities::AutoFid fid(MEDmemFileOpen(dftFileName.c_str(),&memfile,MED_FALSE,MED_ACC_CREAT));
232     writeLL(fid);
233   }
234   //
235   MEDCoupling::MCAuto<MEDCoupling::DataArrayByte> ret(MEDCoupling::DataArrayByte::New());
236   ret->useArray(reinterpret_cast<char *>(memfile.app_image_ptr),true,DeallocType::C_DEALLOC,memfile.app_image_size,1);
237   return ret;
238 }
239
240 std::string MEDCoupling::MEDFileWritableStandAlone::GenerateUniqueDftFileNameInMem()
241 {
242   static int ii=0;
243   std::ostringstream oss; oss << DFT_FILENAME_IN_MEM << "_" << ii++;
244   return oss.str();
245 }
246
247 MEDCoupling::MEDFileCapability::MEDFileCapability(med_idt fid)
248 {
249   MEDFILESAFECALLERRD0(MEDfileNumVersionRd,(fid,&_maj,&_min,&_rel));
250 }