5 * Created on 15 février 2012, 19:50
20 #include <ModuleDescriptionParser.h>
21 #include <ModuleParameterGroup.h>
22 #include <ModuleDescription.h>
23 #include <ModuleParameter.h>
25 #include <ModuleDescriptionUtilities.h>
27 #include "CreationTool.h"
31 /// STD LD_OPEN CALLS ///
33 char* getModuleDescription ( const std::string lib ) {
35 void* handle = dlopen ( lib.c_str ( ) , RTLD_NOW | RTLD_LOCAL ) ;
37 std::cerr << "CAN'T OPEN LIBRARY: " << dlerror ( ) << '\n' ;
40 typedef char* ( *method_t )( void ) ;
43 method_t myMethod = ( method_t ) dlsym ( handle , "GetXMLModuleDescription" ) ;
44 const char *dlsym_error = dlerror ( ) ;
46 std::cerr << "CAN'T LOAD SYMBOL 'GetXMLModuleDescription: " << dlsym_error << '\n' ;
51 char* descrption = myMethod ( ) ;
60 void execute ( const std::string lib , int _argc , char * _argv[] ) {
61 void* handle = dlopen ( lib.c_str ( ) , RTLD_NOW | RTLD_LOCAL ) ;
63 std::cerr << "CAN'T OPEN LIBRARY: " << dlerror ( ) << '\n' ;
66 typedef int (*method_t )( int argc , char * argv[] ) ;
69 method_t myMethod = ( method_t ) dlsym ( handle , "ModuleEntryPoint" ) ;
70 const char *dlsym_error = dlerror ( ) ;
72 std::cerr << "CAN'T LOAD SYMBOL 'ModuleEntryPoint' " << dlsym_error << '\n' ;
77 myMethod ( _argc , _argv ) ;
84 void saveFile ( const std::string content , const std::string file_name ) {
85 std::ofstream myfile ;
86 myfile.open ( file_name.c_str ( ) ) ;
91 std::string loadFile ( const std::string filename ) {
92 std::string line = std::string ( "" ) ;
93 std::string content = std::string ( "" ) ;
95 std::ifstream infile ;
96 infile.open ( filename.c_str ( ) ) ;
97 while ( ! infile.eof ( ) ) {
98 getline ( infile , line ) ;
99 content += line + "\n" ;
107 std::string loadDummyBody ( ) {
108 return loadFile ( "bbSlicerDummy.dummy_cxx" ) ;
111 std::string loadDummyHeader ( ) {
112 return loadFile ( "bbSlicerDummy.dummy_h" ) ;
115 /// BBTK BOX CREATION ///
117 std::string updateBoxDescription ( const ModuleDescription* module , std::string _header ) {
119 std::string tmp_str = std::string ( "" ) ;
120 tmp_str = module->GetTitle ( ) ;
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 ) ;
131 tmp_str = module->GetContributor ( ) ;
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 ) ;
141 tmp_str = module->GetDescription ( ) ;
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 ) ;
151 _header = Mthd::Aux::replace_str ( _header , "_CCCCC_" , module->GetCategory ( ) ) ;
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 ) ;
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" ;
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" ;
188 std::string updateBoxInputs ( const ModuleDescription* module , std::string content ) {
190 std::string _cxx_inputs = std::string ( "\n" ) ;
191 std::string _cxx_inputs_end = std::string ( "\n" ) ;
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" , "" ) ;
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 ++ ) {
205 ModuleParameter mPara = pGroup.GetParameters ( ).at ( j ) ;
208 "BBTK_DECLARE_INPUT ( " +
209 mPara.GetName ( ) + " , " +
210 mPara.GetCPPType ( ) + " );\n" ;
215 mPara.GetName ( ) + " , " +
216 "\"" + mPara.GetName ( ) + "\"" + " , " +
217 mPara.GetCPPType ( ) + ", \"\");\n" ;
220 content = Mthd::Aux::replace_str ( content , "_11111_" , _cxx_inputs ) ;
221 content = Mthd::Aux::replace_str ( content , "_22222_" , _cxx_inputs_end ) ;
226 const int getNumberOfArguments ( const ModuleDescription* module ) {
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 ++ ) {
232 ModuleParameter mPara = pGroup.GetParameters ( ).at ( j ) ;
239 std::string updateProcessMethod ( const ModuleDescription* module , std::string content , std::string lib ) {
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 ++ ) {
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 ( ) +" \"" ;
255 } else if ( mPara.GetLongFlag ( ) != "" ) {
256 arg_list += "Mthd::Aux::toString( \"" ;
257 arg_list += "--" + mPara.GetLongFlag ( ) + "\" " ;
258 arg_list += " ) + " ;
259 flag = "\" --" + mPara.GetLongFlag ( ) +" \"" ;
262 arg_list += "Mthd::Aux::replace_str ( ( Mthd::Aux::toString( bbGetInput" + mPara.GetName ( ) + "( ) )" ;
263 arg_list += " ) ,\"@@@@@\"," + flag + ") ),\n" ;
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 ) ;
276 std::vector < std::string > getFilesByExtention ( std::string dir , std::string ext ) {
277 std::vector < std::string > files ;
280 struct dirent *dirp ;
281 if ( ( dp = opendir ( dir.c_str ( ) ) ) == NULL ) {
282 std::cout << "ERROR OPENING " << dir << std::endl ;
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 ) ;
296 int main ( int argc , char* argv[] ) {
299 // char * _argv[] = { "-s 20" , "/home/riveros/Desktop/Experiments/RA1.mhd" , "/home/riveros/Desktop/Experiments/RA1-OUT-MAIN.mhd" } ;
300 // execute( gblib , _argc , _argv ) ;
302 const std::string dummy_body_str = loadDummyBody ( ) ;
303 const std::string dummy_header_str = loadDummyHeader ( ) ;
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" ) ;
308 for ( unsigned int i = 0 ; i < files.size ( ) ; i ++ ) {
310 std::string gblib = directory + files.at ( i ) ;
311 std::cout << "/// " << files.at ( i ) << " ///" << std::endl ;
313 /// MODULE INFORMATION ///
314 std::string des = getModuleDescription ( gblib ) ;
315 std::cout << " getModuleDescription( gblib )...OK" << std::endl ;
317 ModuleDescription module ;
318 ModuleDescriptionParser parser ;
320 parser.Parse ( des , module ) ;
321 std::cout << " parser.Parse( des , module )...OK" << std::endl ;
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 ;
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 ) ) ;
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 ) ) ;
345 std::cout << "/// EO " << files.at ( i ) << " ///" << std::endl << std::endl ;
349 //while ( getchar( ) != '\n' ) ;
352 return EXIT_SUCCESS ;