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