1 #include "bbSlicerModuleWrapper.h"
2 #include "bbSlicerPackage.h"
6 BBTK_ADD_BLACK_BOX_TO_PACKAGE ( Slicer, ModuleWrapper )
7 BBTK_BLACK_BOX_IMPLEMENTATION ( ModuleWrapper, bbtk::AtomicBlackBox );
9 void ModuleWrapper::Process ( ) {
11 /// MODULE INFORMATION ///
13 std::string gblib = bbGetInputPath_to_library( );
14 const std::string des = getModuleDescription( gblib );
16 ModuleDescription module;
17 ModuleDescriptionParser parser;
19 parser.Parse( des, module );
21 /// LOADING DUMMY FILES ///
22 std::string _body = loadDummyBody( );
23 std::string _header = loadDummyHeader( );
25 /// SETTING HEADER FILE ///
26 _header = updateBoxName( &module, _header );
27 _header = updateBoxDescription( &module, _header );
28 _header = updateBoxInputs( &module, _header );
29 saveFile( _header, getHeaderFileName( &module ) );
31 /// SETTING BODY FILE ///
32 _body = updateBoxName( &module, _body );
33 _body = updateProcessMethod( &module, _body, gblib );
34 saveFile( _body, getBodyFileName( &module ) );
38 /// STD LD_OPEN CALLS ///
40 char* ModuleWrapper::getModuleDescription ( std::string lib ) {
41 void* handle = dlopen( lib.c_str( ), RTLD_NOW | RTLD_GLOBAL );
43 std::cerr << "CAN'T OPEN LIBRARY: " << dlerror( ) << '\n';
46 typedef char* ( *method_t )( void );
49 method_t myMethod = ( method_t ) dlsym( handle, "GetXMLModuleDescription" );
50 const char *dlsym_error = dlerror( );
52 std::cerr << "CAN'T LOAD SYMBOL 'GetXMLModuleDescription: " << dlsym_error << '\n';
57 char* descrption = myMethod( );
66 void ModuleWrapper::saveFile ( std::string content, std::string file_name ) {
68 myfile.open( file_name.c_str( ) );
73 std::string ModuleWrapper::loadFile ( std::string filename ) {
74 std::string line = std::string( "" );
75 std::string content = std::string( "" );
78 infile.open( filename.c_str( ) );
79 while ( ! infile.eof( ) ) {
80 getline( infile, line );
81 content += line + "\n";
89 std::string ModuleWrapper::loadDummyBody ( ) {
90 return loadFile( "bbSlicerDummy.dummy_cxx" );
93 std::string ModuleWrapper::loadDummyHeader ( ) {
94 return loadFile( "bbSlicerDummy.dummy_h" );
97 /// BBTK BOX CREATION ///
99 std::string ModuleWrapper::updateBoxDescription ( const ModuleDescription* module, std::string _header ) {
100 _header = Mthd::Aux::replace_str( _header, "_NNNNN_", Mthd::Aux::replace_str( module->GetTitle( ), " ", "" ) );
101 _header = Mthd::Aux::replace_str( _header, "_AAAAA_", module->GetContributor( ) );
102 _header = Mthd::Aux::replace_str( _header, "_DDDDD_", module->GetDescription( ) );
103 _header = Mthd::Aux::replace_str( _header, "_CCCCC_", module->GetCategory( ) );
107 std::string ModuleWrapper::updateBoxName ( const ModuleDescription* module, std::string content ) {
108 std::string _new_box_name = Mthd::Aux::replace_str( module->GetTitle( ), " ", "" );
109 content = Mthd::Aux::replace_str( content, "Dummy", _new_box_name );
113 std::string ModuleWrapper::getHeaderFileName ( const ModuleDescription* module ) {
114 std::string name = Mthd::Aux::replace_str( module->GetTitle( ), " ", "" );
116 return "bbSlicer" + name + ".h";
119 std::string ModuleWrapper::getBodyFileName ( const ModuleDescription* module ) {
120 std::string name = Mthd::Aux::replace_str( module->GetTitle( ), " ", "" );
122 return "bbSlicer" + name + ".cxx";
125 std::string ModuleWrapper::updateBoxInputs ( const ModuleDescription* module, std::string content ) {
127 std::string _cxx_inputs = std::string( "\n" );
128 std::string _cxx_inputs_end = std::string( "\n" );
129 std::string _box_name = Mthd::Aux::replace_str( module->GetTitle( ), " ", "" );
131 for ( unsigned long int i = 0; i < module->GetParameterGroups( ).size( ); i ++ ) {
132 ModuleParameterGroup pGroup = module->GetParameterGroups( ).at( i );
133 for ( unsigned long int j = 0; j < pGroup.GetParameters( ).size( ); j ++ ) {
134 ModuleParameter mPara = pGroup.GetParameters( ).at( j );
136 "BBTK_DECLARE_INPUT ( " +
137 mPara.GetName( ) + " , " +
138 mPara.GetCPPType( ) + " );\n";
143 mPara.GetName( ) + " , " +
144 "\"" + mPara.GetName( ) + "\"" + " , " +
145 mPara.GetCPPType( ) + ", \"\");\n";
148 content = Mthd::Aux::replace_str( content, "_11111_", _cxx_inputs );
149 content = Mthd::Aux::replace_str( content, "_22222_", _cxx_inputs_end );
154 const int ModuleWrapper::getNumberOfArguments ( const ModuleDescription* module ) {
156 for ( unsigned long int i = 0; i < module->GetParameterGroups( ).size( ); i ++ ) {
157 ModuleParameterGroup pGroup = module->GetParameterGroups( ).at( i );
158 for ( unsigned long int j = 0; j < pGroup.GetParameters( ).size( ); j ++ ) {
160 ModuleParameter mPara = pGroup.GetParameters( ).at( j );
167 std::string ModuleWrapper::updateProcessMethod ( const ModuleDescription* module, std::string content, std::string lib ) {
169 std::string arg_list = std::string( "" );
170 for ( unsigned long int i = 0; i < module->GetParameterGroups( ).size( ); i ++ ) {
171 ModuleParameterGroup pGroup = module->GetParameterGroups( ).at( i );
172 for ( unsigned long int j = 0; j < pGroup.GetParameters( ).size( ); j ++ ) {
174 ModuleParameter mPara = pGroup.GetParameters( ).at( j );
176 arg_list += "Mthd::Aux::toCharArrray( ";
177 if ( mPara.GetFlag( ) != "" ) {
178 arg_list += "Mthd::Aux::toString( \"";
179 arg_list += "-" + mPara.GetFlag( ) + "\" ";
182 } else if ( mPara.GetLongFlag( ) != "" ) {
183 arg_list += "Mthd::Aux::toString( \"";
184 arg_list += "--" + mPara.GetLongFlag( ) + "\" ";
188 arg_list += "Mthd::Aux::toString( bbGetInput" + mPara.GetName( ) + "( ) )";
194 arg_list += "_EEENNNDDD_";
195 arg_list = Mthd::Aux::replace_str( arg_list, ",\n_EEENNNDDD_", "" );
196 std::string _argc = "\nint _argc =" + Mthd::Aux::toString( number ) + ";\n";
197 std::string _slib = "std::string lib = \"" + lib + "\";\n";
198 std::string _argv = "char * _argv[ ] = { " + arg_list + " };\n";
199 return Mthd::Aux::replace_str( content, "_33333_", _argc + _slib + _argv );
202 void ModuleWrapper::bbUserSetDefaultValues ( ) {
205 void ModuleWrapper::bbUserInitializeProcessing ( ) {
208 void ModuleWrapper::bbUserFinalizeProcessing ( ) {
211 // EO namespace bbSlicer