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