Salome HOME
Move medtool folder to MED base repository
[modules/med.git] / medtool / src / MEDLoader / MEDFileUtilities.cxx
1 // Copyright (C) 2007-2015  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 <sstream>
25
26 med_access_mode MEDFileUtilities::TraduceWriteMode(int medloaderwritemode)
27 {
28   switch(medloaderwritemode)
29   {
30     case 2:
31       return MED_ACC_CREAT;
32     case 1:
33       return MED_ACC_RDEXT;
34     case 0:
35       return MED_ACC_RDWR;
36     default:
37       throw INTERP_KERNEL::Exception("Invalid write mode specified ! must be 0(write with no question), 1(append) or 2(creation)");
38   }
39 }
40
41 const char *MEDFileUtilities::GetReadableMEDFieldType(med_field_type ft)
42 {
43   static const char medFloat64[]="MED_FLOAT64";
44   static const char medInt32[]="MED_INT32";
45   static const char medInt64[]="MED_INT64";
46   switch(ft)
47   {
48     case MED_FLOAT64:
49       return medFloat64;
50     case MED_INT32:
51       return medInt32;
52     case MED_INT64:
53       return medInt64;
54     default:
55       throw INTERP_KERNEL::Exception("Non supported field type ! Should be FLOAT64, INT32 or INT64 !");
56   }
57 }
58
59 void MEDFileUtilities::CheckMEDCode(int code, med_idt fid, const std::string& msg)
60 {
61   if(code<0)
62     {
63       std::ostringstream oss;
64       oss << "MEDFile has returned an error code (" << code <<") : " << msg;
65       throw INTERP_KERNEL::Exception(oss.str().c_str());
66     }
67 }
68
69 void MEDFileUtilities::CheckFileForRead(const std::string& fileName)
70 {
71   int status=MEDLoaderBase::getStatusOfFile(fileName);
72   std::ostringstream oss;
73   oss << " File : \"" << fileName << "\"";
74   switch(status)
75   {
76     case MEDLoaderBase::DIR_LOCKED:
77       {
78         oss << " has been detected as unreadable : impossible to read anything !";
79         throw INTERP_KERNEL::Exception(oss.str().c_str());
80       }
81     case MEDLoaderBase::NOT_EXIST:
82       {
83         oss << " has been detected as NOT EXISTING : impossible to read anything !";
84         throw INTERP_KERNEL::Exception(oss.str().c_str());
85       }
86     case MEDLoaderBase::EXIST_WRONLY:
87       {
88         oss << " has been detected as WRITE ONLY : impossible to read anything !";
89         throw INTERP_KERNEL::Exception(oss.str().c_str());
90       }
91   }
92   AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
93   if(fid<0)
94     {
95       oss << " has been detected as unreadable by MED file : impossible to read anything !";
96       throw INTERP_KERNEL::Exception(oss.str().c_str());
97     }
98   oss << " has been detected readable but ";
99   int major,minor,release;
100   MEDfileNumVersionRd(fid,&major,&minor,&release);
101   if(major<2 || (major==2 && minor<2))
102     {
103       oss << "version of MED file is < 2.2 : impossible to read anything !";
104       throw INTERP_KERNEL::Exception(oss.str().c_str());
105     }
106 }
107
108 MEDFileUtilities::AutoFid::AutoFid(med_idt fid):_fid(fid)
109 {
110 }
111
112 MEDFileUtilities::AutoFid::~AutoFid()
113 {
114   MEDfileClose(_fid);
115 }
116
117 ParaMEDMEM::MEDFileWritable::MEDFileWritable():_too_long_str(0),_zipconn_pol(2)
118 {
119 }
120
121 void ParaMEDMEM::MEDFileWritable::copyOptionsFrom(const MEDFileWritable& other) const
122 {
123   _too_long_str=other._too_long_str;
124   _zipconn_pol=other._zipconn_pol;
125 }
126
127 int ParaMEDMEM::MEDFileWritable::getTooLongStrPolicy() const
128 {
129   return _too_long_str;
130 }
131
132 void ParaMEDMEM::MEDFileWritable::setTooLongStrPolicy(int newVal)
133 {
134   if(newVal!=2 && newVal!=1 && newVal!=0)
135     throw INTERP_KERNEL::Exception("MEDFileWritable::setTooLongStrPolicy : invalid policy should be in 0,1 or 2 !");
136   _too_long_str=newVal;
137 }
138
139 int ParaMEDMEM::MEDFileWritable::getZipConnPolicy()
140 {
141   return _zipconn_pol;
142 }
143
144 void ParaMEDMEM::MEDFileWritable::setZipConnPolicy(int newVal)
145 {
146   _zipconn_pol=newVal;
147 }