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 ) {
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( ) ) ;
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 ) ;
131 std::string getHeaderFileName ( const ModuleDescription* module ) {
132 std::string name = Mthd::Aux::replace_str( module->GetTitle( ) , " " , "" ) ;
133 return "bbSlicer" + name + ".h" ;
136 std::string getBodyFileName ( const ModuleDescription* module ) {
137 std::string name = Mthd::Aux::replace_str( module->GetTitle( ) , " " , "" ) ;
139 return "bbSlicer" + name + ".cxx" ;
142 std::string updateBoxInputs ( const ModuleDescription* module , std::string content ) {
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( ) , " " , "" ) ;
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 ) ;
153 "BBTK_DECLARE_INPUT ( " +
154 mPara.GetName( ) + " , " +
155 mPara.GetCPPType( ) + " );\n" ;
160 mPara.GetName( ) + " , " +
161 "\"" + mPara.GetName( ) + "\"" + " , " +
162 mPara.GetCPPType( ) + ", \"\");\n" ;
165 content = Mthd::Aux::replace_str( content , "_11111_" , _cxx_inputs ) ;
166 content = Mthd::Aux::replace_str( content , "_22222_" , _cxx_inputs_end ) ;
171 const int getNumberOfArguments ( const ModuleDescription* module ) {
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 ++ ) {
177 ModuleParameter mPara = pGroup.GetParameters( ).at( j ) ;
184 std::string updateProcessMethod ( const ModuleDescription* module , std::string content , std::string lib ) {
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 ++ ) {
191 ModuleParameter mPara = pGroup.GetParameters( ).at( j ) ;
193 arg_list += "Mthd::Aux::toCharArrray( " ;
194 if ( mPara.GetFlag( ) != "" ) {
195 arg_list += "Mthd::Aux::toString( \"" ;
196 arg_list += "-" + mPara.GetFlag( ) + "\" " ;
197 arg_list += " ) + " ;
199 } else if ( mPara.GetLongFlag( ) != "" ) {
200 arg_list += "Mthd::Aux::toString( \"" ;
201 arg_list += "--" + mPara.GetLongFlag( ) + "\" " ;
202 arg_list += " ) + " ;
205 arg_list += "Mthd::Aux::toString( bbGetInput" + mPara.GetName( ) + "( ) )" ;
206 arg_list += " ),\n" ;
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 ) ;
219 std::vector < std::string > getFilesByExtention ( std::string dir , std::string ext ) {
220 std::vector < std::string > files ;
223 struct dirent *dirp ;
224 if ( ( dp = opendir( dir.c_str( ) ) ) == NULL ) {
225 std::cout << "ERROR OPENING " << dir << std::endl ;
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 ) ;
239 int main ( int argc , char* argv[] ) {
242 // char * _argv[] = { "-s 20" , "/home/riveros/Desktop/Experiments/RA1.mhd" , "/home/riveros/Desktop/Experiments/RA1-OUT-MAIN.mhd" } ;
243 // execute( gblib , _argc , _argv ) ;
245 const std::string dummy_body_str = loadDummyBody( ) ;
246 const std::string dummy_header_str = loadDummyHeader( ) ;
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" ) ;
251 for ( unsigned int i = 0 ; i < files.size( ) ; i ++ ) {
253 std::string gblib = directory + files.at( i ) ;
254 std::cout << "/// " << files.at( i ) << " ///" << std::endl ;
256 /// MODULE INFORMATION ///
257 std::string des = getModuleDescription( gblib ) ;
258 std::cout << " getModuleDescription( gblib )...OK" << std::endl ;
260 ModuleDescription module ;
261 ModuleDescriptionParser parser ;
263 parser.Parse( des , module ) ;
264 std::cout << " parser.Parse( des , module )...OK" << std::endl ;
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 ;
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 ) ) ;
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 ) ) ;
288 std::cout << "/// EO " << files.at( i ) << " ///" << std::endl << std::endl ;
292 //while ( getchar( ) != '\n' ) ;
295 return EXIT_SUCCESS ;