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