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