]> Creatis software - creaCLI.git/blob - ModuleCall/main.cpp
The following boxxes compilated with no errors
[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     _header = Mthd::Aux::replace_str( _header , "_NNNNN_" , Mthd::Aux::replace_str( module->GetTitle( ) , " " , "" ) ) ;
119     _header = Mthd::Aux::replace_str( _header , "_AAAAA_" , module->GetContributor( ) ) ;
120     _header = Mthd::Aux::replace_str( _header , "_DDDDD_" , module->GetDescription( ) ) ;
121     _header = Mthd::Aux::replace_str( _header , "_CCCCC_" , module->GetCategory( ) ) ;
122     return _header ;
123 }
124
125 std::string updateBoxName ( const ModuleDescription* module , std::string content ) {
126     std::string _new_box_name = Mthd::Aux::replace_str( module->GetTitle( ) , " " , "" ) ;
127     content = Mthd::Aux::replace_str( content , "Dummy" , _new_box_name ) ;
128     return content ;
129 }
130
131 std::string getHeaderFileName ( const ModuleDescription* module ) {
132     std::string name = Mthd::Aux::replace_str( module->GetTitle( ) , " " , "" ) ;
133     return "bbSlicer" + name + ".h" ;
134 }
135
136 std::string getBodyFileName ( const ModuleDescription* module ) {
137     std::string name = Mthd::Aux::replace_str( module->GetTitle( ) , " " , "" ) ;
138
139     return "bbSlicer" + name + ".cxx" ;
140 }
141
142 std::string updateBoxInputs ( const ModuleDescription* module , std::string content ) {
143
144     std::string _cxx_inputs = std::string( "\n" ) ;
145     std::string _cxx_inputs_end = std::string( "\n" ) ;
146     std::string _box_name = Mthd::Aux::replace_str( module->GetTitle( ) , " " , "" ) ;
147
148     for ( unsigned long int i = 0 ; i < module->GetParameterGroups( ).size( ) ; i ++ ) {
149         ModuleParameterGroup pGroup = module->GetParameterGroups( ).at( i ) ;
150         for ( unsigned long int j = 0 ; j < pGroup.GetParameters( ).size( ) ; j ++ ) {
151             ModuleParameter mPara = pGroup.GetParameters( ).at( j ) ;
152             _cxx_inputs +=
153                     "BBTK_DECLARE_INPUT ( " +
154                     mPara.GetName( ) + " , " +
155                     mPara.GetCPPType( ) + " );\n" ;
156
157             _cxx_inputs_end +=
158                     "BBTK_INPUT(" +
159                     _box_name + " , " +
160                     mPara.GetName( ) + " , " +
161                     "\"" + mPara.GetName( ) + "\"" + " , " +
162                     mPara.GetCPPType( ) + ", \"\");\n" ;
163         }
164     }
165     content = Mthd::Aux::replace_str( content , "_11111_" , _cxx_inputs ) ;
166     content = Mthd::Aux::replace_str( content , "_22222_" , _cxx_inputs_end ) ;
167
168     return content ;
169 }
170
171 const int getNumberOfArguments ( const ModuleDescription* module ) {
172     int number = 0 ;
173     for ( unsigned long int i = 0 ; i < module->GetParameterGroups( ).size( ) ; i ++ ) {
174         ModuleParameterGroup pGroup = module->GetParameterGroups( ).at( i ) ;
175         for ( unsigned long int j = 0 ; j < pGroup.GetParameters( ).size( ) ; j ++ ) {
176
177             ModuleParameter mPara = pGroup.GetParameters( ).at( j ) ;
178             number ++ ;
179         }
180     }
181     return number ;
182 }
183
184 std::string updateProcessMethod ( const ModuleDescription* module , std::string content , std::string lib ) {
185     int number = 0 ;
186     std::string arg_list = std::string( "" ) ;
187     for ( unsigned long int i = 0 ; i < module->GetParameterGroups( ).size( ) ; i ++ ) {
188         ModuleParameterGroup pGroup = module->GetParameterGroups( ).at( i ) ;
189         for ( unsigned long int j = 0 ; j < pGroup.GetParameters( ).size( ) ; j ++ ) {
190
191             ModuleParameter mPara = pGroup.GetParameters( ).at( j ) ;
192
193             arg_list += "Mthd::Aux::toCharArrray( " ;
194             if ( mPara.GetFlag( ) != "" ) {
195                 arg_list += "Mthd::Aux::toString( \"" ;
196                 arg_list += "-" + mPara.GetFlag( ) + "\" " ;
197                 arg_list += " ) + " ;
198
199             } else if ( mPara.GetLongFlag( ) != "" ) {
200                 arg_list += "Mthd::Aux::toString( \"" ;
201                 arg_list += "--" + mPara.GetLongFlag( ) + "\" " ;
202                 arg_list += " ) + " ;
203             }
204
205             arg_list += "Mthd::Aux::toString( bbGetInput" + mPara.GetName( ) + "( ) )" ;
206             arg_list += " ),\n" ;
207             number ++ ;
208         }
209
210     }
211     arg_list += "_EEENNNDDD_" ;
212     arg_list = Mthd::Aux::replace_str( arg_list , ",\n_EEENNNDDD_" , "" ) ;
213     std::string _argc = "\nint _argc =" + Mthd::Aux::toString( number ) + ";\n" ;
214     std::string _slib = "std::string lib = \"" + lib + "\";\n" ;
215     std::string _argv = "char * _argv[ ] = { " + arg_list + " };\n" ;
216     return Mthd::Aux::replace_str( content , "_33333_" , _argc + _slib + _argv ) ;
217 }
218
219 std::vector < std::string > getFilesByExtention ( std::string dir , std::string ext ) {
220     std::vector < std::string > files ;
221     files.clear( ) ;
222     DIR *dp ;
223     struct dirent *dirp ;
224     if ( ( dp = opendir( dir.c_str( ) ) ) == NULL ) {
225         std::cout << "ERROR OPENING " << dir << std::endl ;
226         return files ;
227     }
228
229     while ( ( dirp = readdir( dp ) ) != NULL ) {
230         std::string file = std::string( dirp->d_name ) ;
231         if ( Mthd::Aux::str_ends_with( file , ext ) ) {
232             files.push_back( file ) ;
233         }
234     }
235     closedir( dp ) ;
236     return files ;
237 }
238
239 int main ( int argc , char* argv[] ) {
240
241     //    int _argc = 3 ;
242     //    char * _argv[] = { "-s 20" , "/home/riveros/Desktop/Experiments/RA1.mhd" , "/home/riveros/Desktop/Experiments/RA1-OUT-MAIN.mhd" } ;
243     //    execute( gblib , _argc , _argv ) ;
244
245     const std::string dummy_body_str = loadDummyBody( ) ;
246     const std::string dummy_header_str = loadDummyHeader( ) ;
247
248     const std::string directory = "/home/riveros/.slicer/Slicer4-bin/Slicer-build/lib/Slicer-4.0/cli-modules/" ;
249     const std::vector < std::string > files = getFilesByExtention( directory , ".so" ) ;
250
251     for ( unsigned int i = 0 ; i < files.size( ) ; i ++ ) {
252
253         std::string gblib = directory + files.at( i ) ;
254         std::cout << "/// " << files.at( i ) << " ///" << std::endl ;
255
256         ///     MODULE INFORMATION      ///
257         std::string des = getModuleDescription( gblib ) ;
258         std::cout << "  getModuleDescription( gblib )...OK" << std::endl ;
259
260         ModuleDescription module ;
261         ModuleDescriptionParser parser ;
262
263         parser.Parse( des , module ) ;
264         std::cout << "  parser.Parse( des , module )...OK" << std::endl ;
265
266         ///     LOADING DUMMY FILES     ///
267         std::string _body = std::string( dummy_body_str ) ;
268         std::cout << "  loadDummyBody( )...OK" << std::endl ;
269         std::string _header = std::string( dummy_header_str ) ;
270         std::cout << "  loadDummyHeader( )...OK" << std::endl ;
271
272         ///     SETTING HEADER FILE     ///
273         _header = updateBoxName( &module , _header ) ;
274         std::cout << "  updateBoxName( &module , _header )...OK" << std::endl ;
275         _header = updateBoxDescription( &module , _header ) ;
276         std::cout << "  updateBoxDescription( &module , _header )...OK" << std::endl ;
277         _header = updateBoxInputs( &module , _header ) ;
278         std::cout << "  updateBoxInputs( &module , _header )...OK" << std::endl ;
279         saveFile( _header , getHeaderFileName( &module ) ) ;
280
281         ///     SETTING BODY FILE       ///
282         _body = updateBoxName( &module , _body ) ;
283         std::cout << "  updateBoxName( &module , _body )...OK" << std::endl ;
284         _body = updateProcessMethod( &module , _body , gblib ) ;
285         std::cout << "  updateProcessMethod( &module , _body , gblib )...OK" << std::endl ;
286         saveFile( _body , getBodyFileName( &module ) ) ;
287
288         std::cout << "/// EO " << files.at( i ) << " ///" << std::endl << std::endl ;
289
290         gblib.clear( ) ;
291         des.clear( ) ;
292         //while ( getchar( ) != '\n' ) ;
293
294     }
295     return EXIT_SUCCESS ;
296 }