]> Creatis software - creaCLI.git/blob - ModuleCall/main.cpp
All Slicer modules succesfully compiled into BBTK boxes :P
[creaCLI.git] / ModuleCall / main.cpp
1 /* 
2  * File:   main.cpp
3  * Author: gabriel
4  *
5  * Created on 15 fĂ©vrier 2012, 19:50
6  */
7
8 #include <vector>
9
10 #include <cstdlib>
11 #include <sstream>
12 #include <iostream>
13 #include <fstream>
14
15 #include <stdio.h>
16 #include <dlfcn.h>
17 #include <string.h>
18 #include <dirent.h>
19
20 #include <ModuleDescriptionParser.h>
21 #include <ModuleParameterGroup.h>
22 #include <ModuleDescription.h>
23 #include <ModuleParameter.h>
24
25 #include <ModuleDescriptionUtilities.h>
26
27 #include "CreationTool.h"
28
29
30
31 ///     STD LD_OPEN CALLS       ///
32
33 char* getModuleDescription ( const std::string lib ) {
34
35     void* handle = dlopen ( lib.c_str ( ) , RTLD_NOW | RTLD_LOCAL ) ;
36     if ( ! handle ) {
37         std::cerr << "CAN'T OPEN LIBRARY: " << dlerror ( ) << '\n' ;
38         return NULL ;
39     }
40     typedef char* ( *method_t )( void ) ;
41     // RESET ERRORS
42     dlerror ( ) ;
43     method_t myMethod = ( method_t ) dlsym ( handle , "GetXMLModuleDescription" ) ;
44     const char *dlsym_error = dlerror ( ) ;
45     if ( dlsym_error ) {
46         std::cerr << "CAN'T LOAD SYMBOL 'GetXMLModuleDescription: " << dlsym_error << '\n' ;
47         dlclose ( handle ) ;
48         return NULL ;
49     }
50     // CALLING METHOD
51     char* descrption = myMethod ( ) ;
52     // CLOSING LIB
53
54     //dlclose( handle ) ;
55
56     return descrption ;
57
58 }
59
60 void execute ( const std::string lib , int _argc , char * _argv[] ) {
61     void* handle = dlopen ( lib.c_str ( ) , RTLD_NOW | RTLD_LOCAL ) ;
62     if ( ! handle ) {
63         std::cerr << "CAN'T OPEN LIBRARY: " << dlerror ( ) << '\n' ;
64         return ;
65     }
66     typedef int (*method_t )( int argc , char * argv[] ) ;
67     // RESET ERRORS
68     dlerror ( ) ;
69     method_t myMethod = ( method_t ) dlsym ( handle , "ModuleEntryPoint" ) ;
70     const char *dlsym_error = dlerror ( ) ;
71     if ( dlsym_error ) {
72         std::cerr << "CAN'T LOAD SYMBOL 'ModuleEntryPoint' " << dlsym_error << '\n' ;
73         dlclose ( handle ) ;
74         return ;
75     }
76     // CALLING METHOD
77     myMethod ( _argc , _argv ) ;
78     // CLOSING LIB
79     dlclose ( handle ) ;
80 }
81
82 ///     FILE RELATED     ///
83
84 void saveFile ( const std::string content , const std::string file_name ) {
85     std::ofstream myfile ;
86     myfile.open ( file_name.c_str ( ) ) ;
87     myfile << content ;
88     myfile.close ( ) ;
89 }
90
91 std::string loadFile ( const std::string filename ) {
92     std::string line = std::string ( "" ) ;
93     std::string content = std::string ( "" ) ;
94
95     std::ifstream infile ;
96     infile.open ( filename.c_str ( ) ) ;
97     while ( ! infile.eof ( ) ) {
98         getline ( infile , line ) ;
99         content += line + "\n" ;
100         line.clear ( ) ;
101     }
102     infile.close ( ) ;
103     return content ;
104
105 }
106
107 std::string loadDummyBody ( ) {
108     return loadFile ( "bbSlicerDummy.dummy_cxx" ) ;
109 }
110
111 std::string loadDummyHeader ( ) {
112     return loadFile ( "bbSlicerDummy.dummy_h" ) ;
113 }
114
115 ///     BBTK BOX CREATION       /// 
116
117 std::string updateBoxDescription ( const ModuleDescription* module , std::string _header ) {
118
119     std::string tmp_str = std::string ( "" ) ;
120     tmp_str = module->GetTitle ( ) ;
121
122     tmp_str = Mthd::Aux::replace_str ( tmp_str , " " , "" ) ;
123     tmp_str = Mthd::Aux::replace_str ( tmp_str , "<" , "" ) ;
124     tmp_str = Mthd::Aux::replace_str ( tmp_str , ">" , "" ) ;
125     tmp_str = Mthd::Aux::replace_str ( tmp_str , "(" , "" ) ;
126     tmp_str = Mthd::Aux::replace_str ( tmp_str , ")" , "" ) ;
127     tmp_str = Mthd::Aux::replace_str ( tmp_str , "\n" , "" ) ;
128     _header = Mthd::Aux::replace_str ( _header , "_NNNNN_" , tmp_str ) ;
129
130     tmp_str.clear ( ) ;
131     tmp_str = module->GetContributor ( ) ;
132
133     tmp_str = Mthd::Aux::replace_str ( tmp_str , "<" , "" ) ;
134     tmp_str = Mthd::Aux::replace_str ( tmp_str , ">" , "" ) ;
135     tmp_str = Mthd::Aux::replace_str ( tmp_str , "(" , "" ) ;
136     tmp_str = Mthd::Aux::replace_str ( tmp_str , ")" , "" ) ;
137     tmp_str = Mthd::Aux::replace_str ( tmp_str , "\n" , "" ) ;
138     _header = Mthd::Aux::replace_str ( _header , "_AAAAA_" , tmp_str ) ;
139
140     tmp_str.clear ( ) ;
141     tmp_str = module->GetDescription ( ) ;
142
143     tmp_str = Mthd::Aux::replace_str ( tmp_str , "<" , "" ) ;
144     tmp_str = Mthd::Aux::replace_str ( tmp_str , ">" , "" ) ;
145     tmp_str = Mthd::Aux::replace_str ( tmp_str , "(" , "" ) ;
146     tmp_str = Mthd::Aux::replace_str ( tmp_str , ")" , "" ) ;
147     tmp_str = Mthd::Aux::replace_str ( tmp_str , "\n" , "" ) ;
148     _header = Mthd::Aux::replace_str ( _header , "_DDDDD_" , tmp_str ) ;
149
150
151     _header = Mthd::Aux::replace_str ( _header , "_CCCCC_" , module->GetCategory ( ) ) ;
152     return _header ;
153 }
154
155 std::string updateBoxName ( const ModuleDescription* module , std::string content ) {
156     std::string _new_box_name = module->GetTitle ( ) ;
157     _new_box_name = Mthd::Aux::replace_str ( _new_box_name , " " , "" ) ;
158     _new_box_name = Mthd::Aux::replace_str ( _new_box_name , "<" , "" ) ;
159     _new_box_name = Mthd::Aux::replace_str ( _new_box_name , ">" , "" ) ;
160     _new_box_name = Mthd::Aux::replace_str ( _new_box_name , "(" , "" ) ;
161     _new_box_name = Mthd::Aux::replace_str ( _new_box_name , ")" , "" ) ;
162     _new_box_name = Mthd::Aux::replace_str ( _new_box_name , "\n" , "" ) ;
163     content = Mthd::Aux::replace_str ( content , "Dummy" , _new_box_name ) ;
164     return content ;
165 }
166
167 std::string getHeaderFileName ( const ModuleDescription* module ) {
168     std::string name = Mthd::Aux::replace_str ( module->GetTitle ( ) , " " , "" ) ;
169     name = Mthd::Aux::replace_str ( name , "<" , "" ) ;
170     name = Mthd::Aux::replace_str ( name , ">" , "" ) ;
171     name = Mthd::Aux::replace_str ( name , "(" , "" ) ;
172     name = Mthd::Aux::replace_str ( name , ")" , "" ) ;
173     name = Mthd::Aux::replace_str ( name , "\n" , "" ) ;
174     return "bbSlicer" + name + ".h" ;
175 }
176
177 std::string getBodyFileName ( const ModuleDescription* module ) {
178     std::string name = module->GetTitle ( ) ;
179     name = Mthd::Aux::replace_str ( name , " " , "" ) ;
180     name = Mthd::Aux::replace_str ( name , "<" , "" ) ;
181     name = Mthd::Aux::replace_str ( name , ">" , "" ) ;
182     name = Mthd::Aux::replace_str ( name , "(" , "" ) ;
183     name = Mthd::Aux::replace_str ( name , ")" , "" ) ;
184     name = Mthd::Aux::replace_str ( name , "\n" , "" ) ;
185     return "bbSlicer" + name + ".cxx" ;
186 }
187
188 std::string updateBoxInputs ( const ModuleDescription* module , std::string content ) {
189
190     std::string _cxx_inputs = std::string ( "\n" ) ;
191     std::string _cxx_inputs_end = std::string ( "\n" ) ;
192
193     std::string _box_name = module->GetTitle ( ) ;
194     _box_name = Mthd::Aux::replace_str ( _box_name , " " , "" ) ;
195     _box_name = Mthd::Aux::replace_str ( _box_name , "<" , "" ) ;
196     _box_name = Mthd::Aux::replace_str ( _box_name , ">" , "" ) ;
197     _box_name = Mthd::Aux::replace_str ( _box_name , "(" , "" ) ;
198     _box_name = Mthd::Aux::replace_str ( _box_name , ")" , "" ) ;
199     _box_name = Mthd::Aux::replace_str ( _box_name , "\n" , "" ) ;
200
201     for ( unsigned long int i = 0 ; i < module->GetParameterGroups ( ).size ( ) ; i ++ ) {
202         ModuleParameterGroup pGroup = module->GetParameterGroups ( ).at ( i ) ;
203         for ( unsigned long int j = 0 ; j < pGroup.GetParameters ( ).size ( ) ; j ++ ) {
204
205             ModuleParameter mPara = pGroup.GetParameters ( ).at ( j ) ;
206
207             _cxx_inputs +=
208                     "BBTK_DECLARE_INPUT ( " +
209                     mPara.GetName ( ) + " , " +
210                     mPara.GetCPPType ( ) + " );\n" ;
211
212             _cxx_inputs_end +=
213                     "BBTK_INPUT(" +
214                     _box_name + " , " +
215                     mPara.GetName ( ) + " , " +
216                     "\"" + mPara.GetName ( ) + "\"" + " , " +
217                     mPara.GetCPPType ( ) + ", \"\");\n" ;
218         }
219     }
220     content = Mthd::Aux::replace_str ( content , "_11111_" , _cxx_inputs ) ;
221     content = Mthd::Aux::replace_str ( content , "_22222_" , _cxx_inputs_end ) ;
222
223     return content ;
224 }
225
226 const int getNumberOfArguments ( const ModuleDescription* module ) {
227     int number = 0 ;
228     for ( unsigned long int i = 0 ; i < module->GetParameterGroups ( ).size ( ) ; i ++ ) {
229         ModuleParameterGroup pGroup = module->GetParameterGroups ( ).at ( i ) ;
230         for ( unsigned long int j = 0 ; j < pGroup.GetParameters ( ).size ( ) ; j ++ ) {
231
232             ModuleParameter mPara = pGroup.GetParameters ( ).at ( j ) ;
233             number ++ ;
234         }
235     }
236     return number ;
237 }
238
239 std::string updateProcessMethod ( const ModuleDescription* module , std::string content , std::string lib ) {
240     int number = 0 ;
241     std::string arg_list = std::string ( "" ) ;
242     for ( unsigned long int i = 0 ; i < module->GetParameterGroups ( ).size ( ) ; i ++ ) {
243         ModuleParameterGroup pGroup = module->GetParameterGroups ( ).at ( i ) ;
244         for ( unsigned long int j = 0 ; j < pGroup.GetParameters ( ).size ( ) ; j ++ ) {
245
246             std::string flag = std::string ( "\"\"" ) ;
247             ModuleParameter mPara = pGroup.GetParameters ( ).at ( j ) ;
248             arg_list += "Mthd::Aux::toCharArrray( " ;
249             if ( mPara.GetFlag ( ) != "" ) {
250                 arg_list += "Mthd::Aux::toString( \"" ;
251                 arg_list += "-" + mPara.GetFlag ( ) + "\" " ;
252                 arg_list += " ) + " ;
253                 flag = "\" -" + mPara.GetFlag ( ) +" \"" ;
254
255             } else if ( mPara.GetLongFlag ( ) != "" ) {
256                 arg_list += "Mthd::Aux::toString( \"" ;
257                 arg_list += "--" + mPara.GetLongFlag ( ) + "\" " ;
258                 arg_list += " ) + " ;
259                 flag = "\" --" + mPara.GetLongFlag ( ) +" \"" ;
260             }
261
262             arg_list += "Mthd::Aux::replace_str ( ( Mthd::Aux::toString( bbGetInput" + mPara.GetName ( ) + "( ) )" ;
263             arg_list += " ) ,\"@@@@@\"," + flag + ") ),\n" ;
264             number ++ ;
265         }
266
267     }
268     arg_list += "_EEENNNDDD_" ;
269     arg_list = Mthd::Aux::replace_str ( arg_list , ",\n_EEENNNDDD_" , "" ) ;
270     std::string _argc = "\nint _argc =" + Mthd::Aux::toString ( number ) + ";\n" ;
271     std::string _slib = "std::string lib = \"" + lib + "\";\n" ;
272     std::string _argv = "char * _argv[ ] = { " + arg_list + " };\n" ;
273     return Mthd::Aux::replace_str ( content , "_33333_" , _argc + _slib + _argv ) ;
274 }
275
276 std::vector < std::string > getFilesByExtention ( std::string dir , std::string ext ) {
277     std::vector < std::string > files ;
278     files.clear ( ) ;
279     DIR *dp ;
280     struct dirent *dirp ;
281     if ( ( dp = opendir ( dir.c_str ( ) ) ) == NULL ) {
282         std::cout << "ERROR OPENING " << dir << std::endl ;
283         return files ;
284     }
285
286     while ( ( dirp = readdir ( dp ) ) != NULL ) {
287         std::string file = std::string ( dirp->d_name ) ;
288         if ( Mthd::Aux::str_ends_with ( file , ext ) ) {
289             files.push_back ( file ) ;
290         }
291     }
292     closedir ( dp ) ;
293     return files ;
294 }
295
296 int main ( int argc , char* argv[] ) {
297
298     //    int _argc = 3 ;
299     //    char * _argv[] = { "-s 20" , "/home/riveros/Desktop/Experiments/RA1.mhd" , "/home/riveros/Desktop/Experiments/RA1-OUT-MAIN.mhd" } ;
300     //    execute( gblib , _argc , _argv ) ;
301
302     const std::string dummy_body_str = loadDummyBody ( ) ;
303     const std::string dummy_header_str = loadDummyHeader ( ) ;
304
305     const std::string directory = "/home/riveros/.slicer/Slicer4-bin/Slicer-build/lib/Slicer-4.0/cli-modules/" ;
306     const std::vector < std::string > files = getFilesByExtention ( directory , ".so" ) ;
307
308     for ( unsigned int i = 0 ; i < files.size ( ) ; i ++ ) {
309
310         std::string gblib = directory + files.at ( i ) ;
311         std::cout << "/// " << files.at ( i ) << " ///" << std::endl ;
312
313         ///     MODULE INFORMATION      ///
314         std::string des = getModuleDescription ( gblib ) ;
315         std::cout << "  getModuleDescription( gblib )...OK" << std::endl ;
316
317         ModuleDescription module ;
318         ModuleDescriptionParser parser ;
319
320         parser.Parse ( des , module ) ;
321         std::cout << "  parser.Parse( des , module )...OK" << std::endl ;
322
323         ///     LOADING DUMMY FILES     ///
324         std::string _body = std::string ( dummy_body_str ) ;
325         std::cout << "  loadDummyBody( )...OK" << std::endl ;
326         std::string _header = std::string ( dummy_header_str ) ;
327         std::cout << "  loadDummyHeader( )...OK" << std::endl ;
328
329         ///     SETTING HEADER FILE     ///
330         _header = updateBoxName ( &module , _header ) ;
331         std::cout << "  updateBoxName( &module , _header )...OK" << std::endl ;
332         _header = updateBoxDescription ( &module , _header ) ;
333         std::cout << "  updateBoxDescription( &module , _header )...OK" << std::endl ;
334         _header = updateBoxInputs ( &module , _header ) ;
335         std::cout << "  updateBoxInputs( &module , _header )...OK" << std::endl ;
336         saveFile ( _header , "./GenSrc/" + getHeaderFileName ( &module ) ) ;
337
338         ///     SETTING BODY FILE       ///
339         _body = updateBoxName ( &module , _body ) ;
340         std::cout << "  updateBoxName( &module , _body )...OK" << std::endl ;
341         _body = updateProcessMethod ( &module , _body , gblib ) ;
342         std::cout << "  updateProcessMethod( &module , _body , gblib )...OK" << std::endl ;
343         saveFile ( _body , "./GenSrc/" + getBodyFileName ( &module ) ) ;
344
345         std::cout << "/// EO " << files.at ( i ) << " ///" << std::endl << std::endl ;
346
347         gblib.clear ( ) ;
348         des.clear ( ) ;
349         //while ( getchar( ) != '\n' ) ;
350
351     }
352     return EXIT_SUCCESS ;
353 }