5 * Created on 15 février 2012, 19:50
15 #include <ModuleDescriptionParser.h>
16 #include <ModuleParameterGroup.h>
17 #include <ModuleDescription.h>
18 #include <ModuleParameter.h>
21 #include <ModuleDescriptionUtilities.h>
23 #include "CreationTool.h"
26 /// STD LD_OPEN CALLS ///
28 char* getModuleDescription ( std::string lib ) {
29 void* handle = dlopen( lib.c_str( ), RTLD_NOW | RTLD_GLOBAL );
31 std::cerr << "CAN'T OPEN LIBRARY: " << dlerror( ) << '\n';
34 typedef char* ( *method_t )( void );
37 method_t myMethod = ( method_t ) dlsym( handle, "GetXMLModuleDescription" );
38 const char *dlsym_error = dlerror( );
40 std::cerr << "CAN'T LOAD SYMBOL 'GetXMLModuleDescription: " << dlsym_error << '\n';
45 char* descrption = myMethod( );
52 void execute ( std::string lib, int _argc, char * _argv[] ) {
53 void* handle = dlopen( lib.c_str( ), RTLD_NOW | RTLD_GLOBAL );
55 std::cerr << "CAN'T OPEN LIBRARY: " << dlerror( ) << '\n';
58 typedef int (*method_t )( int argc, char * argv[] );
61 method_t myMethod = ( method_t ) dlsym( handle, "ModuleEntryPoint" );
62 const char *dlsym_error = dlerror( );
64 std::cerr << "CAN'T LOAD SYMBOL 'ModuleEntryPoint' " << dlsym_error << '\n';
69 myMethod( _argc, _argv );
76 void saveFile ( std::string content, std::string file_name ) {
78 myfile.open( file_name.c_str( ) );
83 std::string loadFile ( std::string filename ) {
84 std::string line = std::string( "" );
85 std::string content = std::string( "" );
88 infile.open( filename.c_str( ) );
89 while ( ! infile.eof( ) ) {
90 getline( infile, line );
91 content += line + "\n";
99 std::string loadDummyBody ( ) {
100 return loadFile( "bbSlicerDummy.dummy_cxx" );
103 std::string loadDummyHeader ( ) {
104 return loadFile( "bbSlicerDummy.dummy_h" );
107 /// BBTK BOX CREATION ///
109 std::string updateBoxDescription ( const ModuleDescription* module, std::string _header ) {
110 _header = Mthd::Aux::replace_str( _header, "_NNNNN_", Mthd::Aux::replace_str( module->GetTitle( ), " ", "" ) );
111 _header = Mthd::Aux::replace_str( _header, "_AAAAA_", module->GetContributor( ) );
112 _header = Mthd::Aux::replace_str( _header, "_DDDDD_", module->GetDescription( ) );
113 _header = Mthd::Aux::replace_str( _header, "_CCCCC_", module->GetCategory( ) );
117 std::string updateBoxName ( const ModuleDescription* module, std::string content ) {
118 std::string _new_box_name = Mthd::Aux::replace_str( module->GetTitle( ), " ", "" );
119 content = Mthd::Aux::replace_str( content, "Dummy", _new_box_name );
123 std::string getHeaderFileName ( const ModuleDescription* module ) {
124 std::string name = Mthd::Aux::replace_str( module->GetTitle( ), " ", "" );
126 return "bbSlicer" + name + ".h";
129 std::string getBodyFileName ( const ModuleDescription* module ) {
130 std::string name = Mthd::Aux::replace_str( module->GetTitle( ), " ", "" );
132 return "bbSlicer" + name + ".cxx";
135 std::string updateBoxInputs ( const ModuleDescription* module, std::string content ) {
137 std::string _cxx_inputs = std::string( "\n" );
138 std::string _cxx_inputs_end = std::string( "\n" );
139 std::string _box_name = Mthd::Aux::replace_str( module->GetTitle( ), " ", "" );
141 for ( unsigned long int i = 0; i < module->GetParameterGroups( ).size( ); i ++ ) {
142 ModuleParameterGroup pGroup = module->GetParameterGroups( ).at( i );
143 for ( unsigned long int j = 0; j < pGroup.GetParameters( ).size( ); j ++ ) {
144 ModuleParameter mPara = pGroup.GetParameters( ).at( j );
146 "BBTK_DECLARE_INPUT ( " +
147 mPara.GetName( ) + " , " +
148 mPara.GetCPPType( ) + " );\n";
153 mPara.GetName( ) + " , " +
154 "\"" + mPara.GetName( ) + "\"" + " , " +
155 mPara.GetCPPType( ) + ", \"\");\n";
158 content = Mthd::Aux::replace_str( content, "_11111_", _cxx_inputs );
159 content = Mthd::Aux::replace_str( content, "_22222_", _cxx_inputs_end );
164 const int getNumberOfArguments ( const ModuleDescription* module ) {
166 for ( unsigned long int i = 0; i < module->GetParameterGroups( ).size( ); i ++ ) {
167 ModuleParameterGroup pGroup = module->GetParameterGroups( ).at( i );
168 for ( unsigned long int j = 0; j < pGroup.GetParameters( ).size( ); j ++ ) {
170 ModuleParameter mPara = pGroup.GetParameters( ).at( j );
177 std::string updateProcessMethod ( const ModuleDescription* module, std::string content, std::string lib ) {
179 std::string arg_list = std::string( "" );
180 for ( unsigned long int i = 0; i < module->GetParameterGroups( ).size( ); i ++ ) {
181 ModuleParameterGroup pGroup = module->GetParameterGroups( ).at( i );
182 for ( unsigned long int j = 0; j < pGroup.GetParameters( ).size( ); j ++ ) {
184 ModuleParameter mPara = pGroup.GetParameters( ).at( j );
186 arg_list += "Mthd::Aux::toCharArrray( ";
187 if ( mPara.GetFlag( ) != "" ) {
188 arg_list += "Mthd::Aux::toString( \"";
189 arg_list += "-" + mPara.GetFlag( ) + "\" ";
192 } else if ( mPara.GetLongFlag( ) != "" ) {
193 arg_list += "Mthd::Aux::toString( \"";
194 arg_list += "--" + mPara.GetLongFlag( ) + "\" ";
198 arg_list += "Mthd::Aux::toString( bbGetInput" + mPara.GetName( ) + "( ) )";
204 arg_list += "_EEENNNDDD_";
205 arg_list = Mthd::Aux::replace_str( arg_list, ",\n_EEENNNDDD_", "" );
206 std::string _argc = "\nint _argc =" + Mthd::Aux::toString( number ) + ";\n";
207 std::string _slib = "std::string lib = \"" + lib + "\";\n";
208 std::string _argv = "char * _argv[ ] = { " + arg_list + " };\n";
209 return Mthd::Aux::replace_str( content, "_33333_", _argc + _slib + _argv );
212 int main ( int argc, char* argv[] ) {
215 char * _argv[] = { "-s 20", "/home/riveros/Desktop/Experiments/RA1.mhd", "/home/riveros/Desktop/Experiments/RA1-OUT-MAIN.mhd" };
216 std::string gblib = "/home/riveros/.slicer/Slicer4-bin/Slicer-build/lib/Slicer-4.0/cli-modules/libGaussianBlurImageFilterLib.so";
218 execute( gblib, _argc, _argv );
220 /// MODULE INFORMATION ///
221 const std::string des = getModuleDescription( gblib );
223 // std::cout << "====================================" << std::endl << std::endl;
224 // std::cout << "====================================" << std::endl << std::endl;
225 // std::cout << "====================================" << std::endl << std::endl;
226 // std::cout << "====================================" << std::endl << std::endl;
228 // std::cout << des << std::endl << std::endl;
230 ModuleDescription module;
231 ModuleDescriptionParser parser;
233 parser.Parse( des, module );
235 /// LOADING DUMMY FILES ///
236 std::string _body = loadDummyBody( );
237 std::string _header = loadDummyHeader( );
239 /// SETTING HEADER FILE ///
240 _header = updateBoxName( &module, _header );
241 _header = updateBoxDescription( &module, _header );
242 _header = updateBoxInputs( &module, _header );
243 saveFile( _header, getHeaderFileName( &module ) );
245 /// SETTING BODY FILE ///
246 _body = updateBoxName( &module, _body );
247 _body = updateProcessMethod( &module, _body, gblib );
248 saveFile( _body, getBodyFileName( &module ) );
251 // std::cout << "====================================" << std::endl << std::endl;
252 // std::cout << "====================================" << std::endl << std::endl;
253 // std::cout << "====================================" << std::endl << std::endl;
254 // std::cout << "====================================" << std::endl << std::endl;
256 // std::cout << _header << std::endl;
258 // std::cout << "====================================" << std::endl << std::endl;
259 // std::cout << "====================================" << std::endl << std::endl;
260 // std::cout << "====================================" << std::endl << std::endl;
261 // std::cout << "====================================" << std::endl << std::endl;
263 // std::cout << _body << std::endl;