Salome HOME
merging the main trunk with the BrForComp branch to build a pre V3_0_1
[modules/med.git] / src / MEDMEM / MEDMEM_DriverFactory.cxx
1 #include "MEDMEM_DriverFactory.hxx"
2 #include "MEDMEM_MedMedDriver.hxx"
3 #include "MEDMEM_MedMeshDriver.hxx"
4 #include "MEDMEM_Mesh.hxx"
5 #include "MEDMEM_GibiMeshDriver.hxx"
6 #include "MEDMEM_PorflowMeshDriver.hxx"
7 #include "MEDMEM_VtkMeshDriver.hxx"
8 #include "MEDMEM_VtkMedDriver.hxx"
9
10 #include "MEDMEM_Exception.hxx"
11
12 #include "MEDMEM_MedVersion.hxx"
13 #include "MEDMEM_Compatibility21_22.hxx"
14 #include "MEDMEM_MedMedDriver21.hxx"
15 #include "MEDMEM_MedMedDriver22.hxx"
16 #include "MEDMEM_MedMeshDriver21.hxx"
17 #include "MEDMEM_MedMeshDriver22.hxx"
18
19 using namespace MEDMEM;
20 using namespace MED_EN;
21
22 template<>
23 void fill<-1,0x3>(double *a, const double *b)
24 {
25 }
26
27 template<>
28 bool compare<-1>(const double *a, const double *b)
29 {
30   return false;
31 }
32
33 MED_EN::medFileVersion DRIVERFACTORY::globalMedFileVersionForWriting = MED_EN::V22;
34
35 medFileVersion DRIVERFACTORY::getMedFileVersionForWriting()
36 {
37   return DRIVERFACTORY::globalMedFileVersionForWriting;
38 }
39
40 void DRIVERFACTORY::setMedFileVersionForWriting(medFileVersion version)
41 {
42   DRIVERFACTORY::globalMedFileVersionForWriting = version;
43 }
44
45 GENDRIVER *DRIVERFACTORY::buildDriverForMesh(driverTypes driverType,
46                                              const std::string & fileName,
47                                              MESH *mesh,
48                                              const string & driverName,
49                                              med_mode_acces access)
50 {
51   GENDRIVER *ret;
52   switch(driverType)
53     {
54     case MED_DRIVER : {
55       switch(access)
56         {
57         case MED_LECT : {
58           ret = new MED_MESH_RDONLY_DRIVER(fileName, mesh);
59           ret->setMeshName(driverName);
60           return ret;
61         }
62         case MED_ECRI : {
63           ret = new MED_MESH_WRONLY_DRIVER(fileName, mesh);
64           ret->setMeshName(driverName);
65           return ret;
66         }
67         case MED_REMP : {
68           ret = new MED_MESH_RDWR_DRIVER(fileName, mesh);
69           ret->setMeshName(driverName);
70           return ret;
71         }
72         default:
73           throw MED_EXCEPTION ("access type has not been properly specified to the method");
74         }
75       break;
76     }
77
78     case GIBI_DRIVER : {
79       switch(access)
80         {
81         case MED_LECT : {
82           ret=new GIBI_MESH_RDONLY_DRIVER(fileName,mesh);
83           return ret;
84         }
85         case MED_ECRI : {
86           throw MED_EXCEPTION ("access mode other than MED_LECT has been specified with the GIBI_DRIVER type which is not allowed because GIBI_DRIVER is only a read access driver");
87         }
88         case MED_REMP : {
89           throw MED_EXCEPTION ("access mode other than MED_LECT has been specified with the GIBI_DRIVER type which is not allowed because GIBI_DRIVER is only a read access driver");
90         }
91         default:
92           throw MED_EXCEPTION ("access type has not been properly specified to the method");
93         }
94       break;
95     }
96
97     case PORFLOW_DRIVER : {
98       switch(access)
99         {
100         case MED_LECT : {
101           ret=new PORFLOW_MESH_RDONLY_DRIVER(fileName,mesh);
102           return ret;
103         }
104         case MED_ECRI : {
105           throw MED_EXCEPTION ("access mode other than MED_LECT has been specified with the PORFLOW_DRIVER type which is not allowed because PORFLOW_DRIVER is only a read access driver");
106         }
107         case MED_REMP : {
108           throw MED_EXCEPTION ("access mode other than MED_LECT has been specified with the PORFLOW_DRIVER type which is not allowed because PORFLOW_DRIVER is only a read access driver");
109         }
110         default:
111           throw MED_EXCEPTION ("access type has not been properly specified to the method");
112         }
113       break;
114     }
115
116     case VTK_DRIVER : {
117       switch(access)
118         {
119         case MED_LECT : {
120           throw MED_EXCEPTION ("access mode other than MED_ECRI or MED_REMPT has been specified with the VTK_DRIVER type which is not allowed because VTK_DRIVER is only a write access driver");
121         }
122         case MED_ECRI : {
123           ret=new VTK_MESH_DRIVER(fileName,mesh);
124           return ret;
125         }
126         case MED_REMP : {
127           ret=new VTK_MESH_DRIVER(fileName,mesh);
128           return ret;
129         }
130         default:
131           throw MED_EXCEPTION ("access type has not been properly specified to the method");
132         }
133       break;
134     }
135
136     case NO_DRIVER : {
137       throw MED_EXCEPTION ("NO_DRIVER has been specified to the method which is not allowed");
138     }
139     default:
140       throw MED_EXCEPTION ("other driver than MED_DRIVER GIBI_DRIVER PORFLOW_DRIVER and VT_DRIVER has been specified to the method which is not allowed");
141     }
142 }
143
144 GENDRIVER *DRIVERFACTORY::buildDriverForMed(driverTypes driverType,
145                                             const std::string & fileName,
146                                             MED *med, med_mode_acces access)
147 {
148   GENDRIVER *ret;
149
150   switch(driverType)
151     {
152     case MED_DRIVER : {
153       switch(access)
154         {
155         case MED_LECT : {
156           ret=new MED_MED_RDONLY_DRIVER(fileName,med);
157           break ;
158         }
159         case MED_ECRI : {
160           ret=new MED_MED_WRONLY_DRIVER(fileName,med);
161           break ;
162         }
163         case MED_REMP : {
164           ret=new MED_MED_RDONLY_DRIVER(fileName,med);
165           break ;
166         }
167         default:
168           throw MED_EXCEPTION ("access type has not been properly specified to the method");
169         }
170       break;
171     }
172
173     case VTK_DRIVER : {
174       switch(access)
175         {
176         case MED_LECT : {
177           throw MED_EXCEPTION ("access mode other than MED_ECRI or MED_REMPT has been specified with the VTK_DRIVER type which is not allowed because VTK_DRIVER is only a write access driver");
178         }
179         case MED_ECRI : {
180           ret=new VTK_MED_DRIVER(fileName,med);
181           break ;
182         }
183         case MED_REMP : {
184           ret=new VTK_MED_DRIVER(fileName,med);
185           break ;
186         }
187         default:
188           throw MED_EXCEPTION ("access type has not been properly specified to the method");
189         }
190       break;
191     }
192
193     case GIBI_DRIVER : {
194       throw MED_EXCEPTION ("GIBI_DRIVER has been specified to the method which is not allowed because there is no GIBI driver for the MED object");
195       break;
196     }
197
198     case PORFLOW_DRIVER : {
199       throw MED_EXCEPTION ("PORFLOW_DRIVER has been specified to the method which is not allowed because there is no PORFLOW driver for the MED object");
200       break;
201     }
202
203     case NO_DRIVER : {
204       throw MED_EXCEPTION ("NO_DRIVER has been specified to the method which is not allowed");
205       break;
206     }
207     default:
208       throw MED_EXCEPTION ("NO_DRIVER has been specified to the method which is not allowed");
209     }
210   return ret;
211 }
212
213 GENDRIVER * DRIVERFACTORY::buildMedDriverFromFile(const string & fileName,
214                                                   MED * const ptrMed,
215                                                   MED_EN::med_mode_acces access)
216 {
217   medFileVersion version;
218
219   try
220     {
221       version = getMedFileVersion(fileName);
222     }
223   catch (MEDEXCEPTION & ex)
224     {
225       version = DRIVERFACTORY::globalMedFileVersionForWriting;
226     }
227
228   MESSAGE("buildMedDriverFromFile version of the file " << version);
229
230   GENDRIVER * driver;
231
232   switch(access)
233     {
234     case MED_LECT : {
235       if (version == V21)
236         driver = new MED_MED_RDONLY_DRIVER21(fileName,ptrMed);
237       else if (version == V22)
238         driver = new MED_MED_RDONLY_DRIVER22(fileName,ptrMed);
239       return driver;
240     }
241     case MED_ECRI : {
242       if (version == V21)
243         driver = new MED_MED_WRONLY_DRIVER21(fileName,ptrMed);
244       else if (version == V22)
245         driver = new MED_MED_WRONLY_DRIVER22(fileName,ptrMed);
246       return driver;
247     }
248     case MED_REMP : {
249       if (version == V21)
250         driver = new MED_MED_RDWR_DRIVER21(fileName,ptrMed);
251       else if (version == V22)
252         driver = new MED_MED_RDWR_DRIVER22(fileName,ptrMed);
253       return driver;
254     }
255     default:
256       throw MED_EXCEPTION ("access type has not been properly specified to the method");
257     }
258 }
259
260 GENDRIVER * DRIVERFACTORY::buildMeshDriverFromFile(const string & fileName,
261                                                    MESH * ptrMesh,
262                                                    MED_EN::med_mode_acces access)
263 {
264   medFileVersion version;
265
266   try
267     {
268       version = getMedFileVersion(fileName);
269     }
270   catch (MEDEXCEPTION & ex)
271     {
272       version = DRIVERFACTORY::globalMedFileVersionForWriting;
273     }
274
275   MESSAGE("buildMeshDriverFromFile version of the file " << version);
276
277   GENDRIVER * driver;
278
279   switch(access)
280     {
281     case MED_LECT : {
282       if (version == V21)
283         driver = new MED_MESH_RDONLY_DRIVER21(fileName,ptrMesh);
284       else if (version == V22)
285         driver = new MED_MESH_RDONLY_DRIVER22(fileName,ptrMesh);
286       return driver;
287     }
288     case MED_ECRI : {
289       if (version == V21)
290         driver = new MED_MESH_WRONLY_DRIVER21(fileName,ptrMesh);
291       else if (version == V22)
292         driver = new MED_MESH_WRONLY_DRIVER22(fileName,ptrMesh);
293       return driver;
294     }
295     case MED_REMP : {
296       if (version == V21)
297         driver = new MED_MESH_RDWR_DRIVER21(fileName,ptrMesh);
298       else if (version == V22)
299         driver = new MED_MESH_RDWR_DRIVER22(fileName,ptrMesh);
300       return driver;
301     }
302     default:
303       throw MED_EXCEPTION ("access type has not been properly specified to the method");
304     }
305 }
306
307 GENDRIVER * DRIVERFACTORY::buildConcreteMedDriverForMesh(const std::string & fileName,
308                                                          MESH *ptrMesh,const string &  driverName,
309                                                          MED_EN::med_mode_acces access,
310                                                          MED_EN::medFileVersion version)
311 {
312   GENDRIVER * driver;
313
314   MESSAGE("buildConcreteMedDriverForMesh version of the file " << version);
315
316   switch(access)
317     {
318     case MED_LECT : {
319       if (version == V21)
320         driver = new MED_MESH_RDONLY_DRIVER21(fileName,ptrMesh);
321       else if (version == V22)
322         driver = new MED_MESH_RDONLY_DRIVER22(fileName,ptrMesh);
323       driver->setMeshName(driverName);
324       return driver;
325     }
326     case MED_ECRI : {
327       if (version == V21)
328         driver = new MED_MESH_WRONLY_DRIVER21(fileName,ptrMesh);
329       else if (version == V22)
330         driver = new MED_MESH_WRONLY_DRIVER22(fileName,ptrMesh);
331       driver->setMeshName(driverName);
332       return driver;
333     }
334     case MED_REMP : {
335       if (version == V21)
336         driver = new MED_MESH_RDWR_DRIVER21(fileName,ptrMesh);
337       else if (version == V22)
338         driver = new MED_MESH_RDWR_DRIVER22(fileName,ptrMesh);
339       driver->setMeshName(driverName);
340       return driver;
341     }
342     default:
343       throw MED_EXCEPTION ("access type has not been properly specified to the method");
344     }
345 }