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