Salome HOME
sources v1.2
[modules/smesh.git] / src / Driver / Driver_dl.cxx
1 //  SMESH Driver : implementaion of driver for reading and writing      
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : Driver_dl.cxx
25 //  Module : SMESH
26
27 using namespace std;
28 #include "Driver.h"
29
30 #include <dlfcn.h>
31 #include <stdio.h>
32 #include <utilities.h>
33
34 #include "DriverDAT_R_SMESHDS_Document.h"
35 #include "Test.h"
36
37 /*!  extern "C"
38 {
39   void test() {
40
41                   void *handle;
42                   double (*cosine)(double);
43                   char *error;
44
45                   handle = dlopen ("/usr/lib/libm.so", RTLD_LAZY);
46                   if (!handle) {
47                       fputs (dlerror(), stderr);
48                       exit(1);
49                   }
50
51                   cosine = dlsym(handle, "cos");
52                   if ((error = dlerror()) != NULL)  {
53                       fprintf (stderr, "%s\n", error);
54                       exit(1);
55                   }
56
57                   printf ("%f\n", (*cosine)(2.0));
58                   dlclose(handle);
59
60   char* error;
61   string Extension=string("DAT");
62   string Class=string("SMESHDS_Document");
63   string myLibrary = string("/home/barberou/barberou/SALOME_3105/build/lib/libMeshDriver")+Extension+string(".so");
64   SCRUTE(myLibrary);
65   //Document_Reader* myDriver;//a caster ???
66   DriverDAT_R_SMESHDS_Document* myDriver;
67
68   string myClass = string("Driver")+Extension+string("_R_")+Class;
69   SCRUTE(myClass);
70   
71   void* handle = dlopen (myLibrary.c_str(), RTLD_LAZY);
72   if (!handle) {
73     fputs (dlerror(), stderr);
74     exit(1);
75   }
76   MESSAGE("Open ok");
77
78   //int* res = (int*)dlsym(handle, "getOne");
79   //SCRUTE(res);
80   SCRUTE(dlsym(handle, "getOne"));
81   //int res2= (*res)();
82   myDriver = (DriverDAT_R_SMESHDS_Document*) dlsym(handle, myClass.c_str());
83   MESSAGE("Reading 1");
84   SCRUTE(myDriver);
85   if ((error = dlerror()) != NULL)  {
86     fprintf (stderr, "%s\n", error);
87     exit(1);
88   }
89   MESSAGE("Reading 2");
90     
91   dlclose(handle);
92   MESSAGE("after close");
93                   
94   }
95   }*/
96
97 Document_Reader* Driver::GetDocumentReader(string Extension, string Class) {
98   test();
99   //p-e extern C ?
100   /*!
101   char* error;
102   string myLibrary = string("/home/barberou/barberou/SALOME_3105/build/lib/libMeshDriver")+Extension+string(".so");
103   SCRUTE(myLibrary);
104   //Document_Reader* myDriver;//a caster ???
105   DriverDAT_R_SMESHDS_Document* myDriver;
106
107   string myClass = string("Driver")+Extension+string("_R_")+Class;
108   SCRUTE(myClass);
109   
110   void* handle = dlopen (myLibrary.c_str(), RTLD_LAZY);
111   if (!handle) {
112     fputs (dlerror(), stderr);
113     exit(1);
114   }
115   MESSAGE("Open ok");
116   //myDriver = (Document_Reader*) dlsym(handle, myClass.c_str());
117   int* res = (int*) dlsym(handle, "getOne");
118   SCRUTE(res);
119   myDriver = (DriverDAT_R_SMESHDS_Document*) dlsym(handle, myClass.c_str());
120   MESSAGE("Reading 1");
121   SCRUTE(myDriver);
122   if ((error = dlerror()) != NULL)  {
123     fprintf (stderr, "%s\n", error);
124     exit(1);
125   }
126   MESSAGE("Reading 2");
127     
128   dlclose(handle);
129   MESSAGE("after close");
130   return (myDriver);
131   */
132
133 }
134
135 Document_Writer* Driver::GetDocumentWriter(string Extension, string Class) {
136
137   char* error;
138   string myLibrary = string("libMeshDriver")+Extension+string(".so");
139   Document_Writer* myDriver;//a caster ???
140   string myClass = string("Driver")+Extension+string("_W_")+Class;
141   
142   void* handle = dlopen (myLibrary.c_str(), RTLD_LAZY);
143   if (!handle) {
144     fputs (dlerror(), stderr);
145     exit(1);
146   }
147
148   myDriver = (Document_Writer*) dlsym(handle, myClass.c_str());
149   if ((error = dlerror()) != NULL)  {
150     fprintf (stderr, "%s\n", error);
151     exit(1);
152   }
153     
154   dlclose(handle);
155   
156   return (myDriver);
157
158 }
159
160 Mesh_Reader* Driver::GetMeshReader(string Extension, string Class) {
161
162   char* error;
163   string myLibrary = string("libMeshDriver")+Extension+string(".so");
164   Mesh_Reader* myDriver;//a caster ???
165   string myClass = string("Driver")+Extension+string("_R_")+Class;
166   
167   void* handle = dlopen (myLibrary.c_str(), RTLD_LAZY);
168   if (!handle) {
169     fputs (dlerror(), stderr);
170     exit(1);
171   }
172
173   myDriver = (Mesh_Reader*) dlsym(handle, myClass.c_str());
174   if ((error = dlerror()) != NULL)  {
175     fprintf (stderr, "%s\n", error);
176     exit(1);
177   }
178     
179   dlclose(handle);
180   
181   return (myDriver);
182
183 }
184
185 Mesh_Writer* Driver::GetMeshWriter(string Extension, string Class) {
186
187   char* error;
188   string myLibrary = string("libMeshDriver")+Extension+string(".so");
189   Mesh_Writer* myDriver;//a caster ???
190   string myClass = string("Driver")+Extension+string("_W_")+Class;
191   
192   void* handle = dlopen (myLibrary.c_str(), RTLD_LAZY);
193   if (!handle) {
194     fputs (dlerror(), stderr);
195     exit(1);
196   }
197
198   myDriver = (Mesh_Writer*) dlsym(handle, myClass.c_str());
199   if ((error = dlerror()) != NULL)  {
200     fprintf (stderr, "%s\n", error);
201     exit(1);
202   }
203     
204   dlclose(handle);
205   
206   return (myDriver);
207
208 }
209