]> SALOME platform Git repositories - modules/med.git/blob - src/MEDMEM/MEDMEM_MedVersion.cxx
Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/med.git] / src / MEDMEM / MEDMEM_MedVersion.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #include "MEDMEM_MedVersion.hxx"
23 #include "MEDMEM_Utilities.hxx"
24 #include "MEDMEM_Compatibility21_22.hxx"
25
26 using namespace MEDMEM;
27 using namespace MED_EN;
28
29 medFileVersion MEDMEM::getMedFileVersion(const string & fileName)
30   throw (MEDEXCEPTION)
31 {
32   medFileVersion version;
33
34   med_2_1::med_idt fid21;
35   med_2_3::med_idt fid22;
36
37   med_2_1::med_err ret21;
38   med_2_3::med_err ret22;
39
40   med_2_1::med_int major21;
41   med_2_1::med_int minor21;
42   med_2_1::med_int release21;
43
44   med_2_3::med_int major22;
45   med_2_3::med_int minor22;
46   med_2_3::med_int release22;
47
48   med_2_3::med_mode_acces access22 = med_2_3::MED_LECTURE;
49   med_2_1::med_mode_acces access21 = med_2_1::MED_LECT;
50
51   /*
52     Med Version 2.1 access to the file
53   */
54
55   fid21 = med_2_1::MEDouvrir((const_cast <char *> (fileName.c_str())),
56                              access21);
57
58   if (fid21 < 0)
59     throw MEDEXCEPTION("Problem in getMedFileVersion(const string &) Med file V2.1 access");
60
61   ret21 = med_2_1::MEDversionLire(fid21,&major21,&minor21,&release21);
62
63   if (ret21 < 0)
64     throw MEDEXCEPTION("Problem in getMedFileVersion(const string &) Med file V2.1 version numbers reading");
65
66   if ((minor21 == -1) || (release21 == -1))
67     {
68       MESSAGE_MED("getMedFileVersion the file may have been produced by a version 2.1.x x<5");
69       minor21 = 1;
70       release21 = 5;
71     }
72
73   ret21 = med_2_1::MEDfermer(fid21);
74
75   if (ret21 < 0)
76     throw MEDEXCEPTION("Problem in getMedFileVersion(const string &) Med file V2.1 file closing");
77
78   /*
79     Med Version 2.3 access to the file
80   */
81
82   fid22 = med_2_3::MEDouvrir((const_cast <char *> (fileName.c_str())),
83                              access22);
84
85   if (fid22 < 0)
86     throw MEDEXCEPTION("Problem in getMedFileVersion(const string &) Med file V2.2 access");
87
88   ret22 = med_2_3::MEDversionLire(fid22,&major22,&minor22,&release22);
89
90   if (ret22 < 0)
91     throw MEDEXCEPTION("Problem in getMedFileVersion(const string &) Med file V2.2 version numbers reading");
92
93   if ((minor22 == -1) || (release22 == -1))
94     {
95       MESSAGE_MED("getMedFileVersion the file may have been produced by a version 2.1.x x<5");
96       minor22 = 1;
97       release22 = 5;
98     }
99
100   ret22 = med_2_3::MEDfermer(fid22);
101
102   if (ret22 < 0)
103     throw MEDEXCEPTION("Problem in getMedFileVersion(const string &) Med file V2.2 file closing");
104
105   if ((major21 != major22) || (minor21 != minor22) || (release21 != release22))
106     throw MEDEXCEPTION("Problem in getMedFileVersion(const string &) Med file V21 and V22 version numbers are different");
107
108   MESSAGE_MED("getMedFileVersion: status version 21 of the file major " << major21 << " minor " << minor21 << " release " << release21);
109
110   MESSAGE_MED("getMedFileVersion: status version 22 of the file major " << major22 << " minor " << minor22 << " release " << release22);
111
112   if (major21 == 2)
113     {
114       if (minor21 == 1)
115         version = V21;
116       else if (minor21 > 1)
117         version = V22;
118     }
119   else
120     version = V22;
121
122   MESSAGE_MED("getMedFileVersion the version of the file is " << version);
123
124   return version;
125 }
126
127 //================================================================================
128 /*!
129  * \brief return file mode access corresponding to MED_EN::med_mode_acces in given med version
130   * \param mode - either MED_LECT, MED_ECRI or MED_REMP
131   * \param medVersion - V21 or V22 or ??
132   * \retval int - file mode access
133   *
134   * To be used in MEDouvrir() call
135  */
136 //================================================================================
137
138 //int MEDMEM::getMedAccessMode(MED_EN::med_mode_acces mode,
139 //                             MED_EN::medFileVersion medVersion)
140 //  throw (MEDEXCEPTION)
141 //{
142 //  switch ( medVersion ) {
143 //  case V21:
144 /*
145   from MEDouvrir.c:
146      switch(mode_acces)
147     {
148     case MED_LECT :
149       if (access(nom,F_OK))
150         return -1;
151       else 
152         if ((fid = _MEDfichierOuvrir(nom,mode_acces)) < 0)
153           return -1;
154       break;
155
156     case MED_ECRI :
157       if (access(nom,F_OK))
158         {
159           if ((fid = _MEDfichierCreer(nom)) < 0)
160             return -1;
161         }
162       else
163         if ((fid = _MEDfichierOuvrir(nom,mode_acces)) < 0)
164           return -1;
165       break;
166
167     case MED_REMP :
168       if ((fid = _MEDfichierCreer(nom)) < 0)
169         return -1;
170       break;
171 */
172 //    switch ( mode ) {
173 //    case MED_EN::MED_LECT: return med_2_1::MED_LECT;
174 //    case MED_EN::MED_ECRI: return med_2_1::MED_ECRI;
175 //    case MED_EN::MED_REMP: return med_2_1::MED_ECRI;
176 //    default:
177 //      throw MEDEXCEPTION("getMedAccessMode(): Wrong access mode");
178 //    }
179 //  case V22:
180 /*
181   from med.h:
182    MED_LECTURE          : Ouverture en lecture seule
183    MED_LECTURE_ECRITURE : Ouverture en lecture/ecriture, si un élément existe il est écrasé
184    MED_LECTURE_AJOUT    : Ouverture en lecture/ecriture, si un élément existe une erreur est générée
185    MED_CREATION         : Créer le fichier s'il n'existe pas, l'écrase sinon
186 */
187 //    switch ( mode ) {
188 //    case MED_EN::MED_LECT: return med_2_3::MED_LECTURE;
189 //    case MED_EN::MED_ECRI: return med_2_3::MED_LECTURE_ECRITURE;
190 //    case MED_EN::MED_REMP: return med_2_3::MED_LECTURE_ECRITURE;
191 //              case MED_EN::MED_CREA: return med_2_3::MED_CREATION;
192 //    default:
193 //      throw MEDEXCEPTION("getMedAccessMode(): Wrong access mode");
194 //    }
195 //  default:;
196 //  }
197 //  throw MEDEXCEPTION("getMedAccessMode(): Unknown med version");
198 //}