]> Creatis software - creaCLI.git/blob - bbtk_Slicer_PKG/src/bbSlicerModuleWrapper.cxx
bbtk-Slicer Module JGRR
[creaCLI.git] / bbtk_Slicer_PKG / src / bbSlicerModuleWrapper.cxx
1 #include "bbSlicerModuleWrapper.h"
2 #include "bbSlicerPackage.h"
3
4 namespace bbSlicer {
5
6     BBTK_ADD_BLACK_BOX_TO_PACKAGE ( Slicer, ModuleWrapper )
7     BBTK_BLACK_BOX_IMPLEMENTATION ( ModuleWrapper, bbtk::AtomicBlackBox );
8
9     void ModuleWrapper::Process ( ) {
10
11         ///     MODULE INFORMATION      ///
12
13         std::string gblib = bbGetInputPath_to_library( );
14         const std::string des = getModuleDescription( gblib );
15
16         ModuleDescription module;
17         ModuleDescriptionParser parser;
18
19         parser.Parse( des, module );
20
21         ///     LOADING DUMMY FILES     ///
22         std::string _body = loadDummyBody( );
23         std::string _header = loadDummyHeader( );
24
25         ///     SETTING HEADER FILE     ///
26         _header = updateBoxName( &module, _header );
27         _header = updateBoxDescription( &module, _header );
28         _header = updateBoxInputs( &module, _header );
29         saveFile( _header, getHeaderFileName( &module ) );
30
31         ///     SETTING BODY FILE       ///
32         _body = updateBoxName( &module, _body );
33         _body = updateProcessMethod( &module, _body, gblib );
34         saveFile( _body, getBodyFileName( &module ) );
35     }
36
37
38     ///     STD LD_OPEN CALLS       ///
39
40     char* ModuleWrapper::getModuleDescription ( std::string lib ) {
41         void* handle = dlopen( lib.c_str( ), RTLD_NOW | RTLD_GLOBAL );
42         if ( ! handle ) {
43             std::cerr << "CAN'T OPEN LIBRARY: " << dlerror( ) << '\n';
44             return NULL;
45         }
46         typedef char* ( *method_t )( void );
47         // RESET ERRORS
48         dlerror( );
49         method_t myMethod = ( method_t ) dlsym( handle, "GetXMLModuleDescription" );
50         const char *dlsym_error = dlerror( );
51         if ( dlsym_error ) {
52             std::cerr << "CAN'T LOAD SYMBOL 'GetXMLModuleDescription: " << dlsym_error << '\n';
53             dlclose( handle );
54             return NULL;
55         }
56         // CALLING METHOD
57         char* descrption = myMethod( );
58         // CLOSING LIB
59         dlclose( handle );
60         return descrption;
61
62     }
63
64     ///     FILE RELATED     ///
65
66     void ModuleWrapper::saveFile ( std::string content, std::string file_name ) {
67         std::ofstream myfile;
68         myfile.open( file_name.c_str( ) );
69         myfile << content;
70         myfile.close( );
71     }
72
73     std::string ModuleWrapper::loadFile ( std::string filename ) {
74         std::string line = std::string( "" );
75         std::string content = std::string( "" );
76
77         std::ifstream infile;
78         infile.open( filename.c_str( ) );
79         while ( ! infile.eof( ) ) {
80             getline( infile, line );
81             content += line + "\n";
82             line.clear( );
83         }
84         infile.close( );
85         return content;
86
87     }
88
89     std::string ModuleWrapper::loadDummyBody ( ) {
90         return loadFile( "bbSlicerDummy.dummy_cxx" );
91     }
92
93     std::string ModuleWrapper::loadDummyHeader ( ) {
94         return loadFile( "bbSlicerDummy.dummy_h" );
95     }
96
97     ///     BBTK BOX CREATION       /// 
98
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( ) );
104         return _header;
105     }
106
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 );
110         return content;
111     }
112
113     std::string ModuleWrapper::getHeaderFileName ( const ModuleDescription* module ) {
114         std::string name = Mthd::Aux::replace_str( module->GetTitle( ), " ", "" );
115
116         return "bbSlicer" + name + ".h";
117     }
118
119     std::string ModuleWrapper::getBodyFileName ( const ModuleDescription* module ) {
120         std::string name = Mthd::Aux::replace_str( module->GetTitle( ), " ", "" );
121
122         return "bbSlicer" + name + ".cxx";
123     }
124
125     std::string ModuleWrapper::updateBoxInputs ( const ModuleDescription* module, std::string content ) {
126
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( ), " ", "" );
130
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 );
135                 _cxx_inputs +=
136                         "BBTK_DECLARE_INPUT ( " +
137                         mPara.GetName( ) + " , " +
138                         mPara.GetCPPType( ) + " );\n";
139
140                 _cxx_inputs_end +=
141                         "BBTK_INPUT(" +
142                         _box_name + " , " +
143                         mPara.GetName( ) + " , " +
144                         "\"" + mPara.GetName( ) + "\"" + " , " +
145                         mPara.GetCPPType( ) + ", \"\");\n";
146             }
147         }
148         content = Mthd::Aux::replace_str( content, "_11111_", _cxx_inputs );
149         content = Mthd::Aux::replace_str( content, "_22222_", _cxx_inputs_end );
150
151         return content;
152     }
153
154     const int ModuleWrapper::getNumberOfArguments ( const ModuleDescription* module ) {
155         int number = 0;
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 ++ ) {
159
160                 ModuleParameter mPara = pGroup.GetParameters( ).at( j );
161                 number ++;
162             }
163         }
164         return number;
165     }
166
167     std::string ModuleWrapper::updateProcessMethod ( const ModuleDescription* module, std::string content, std::string lib ) {
168         int number = 0;
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 ++ ) {
173
174                 ModuleParameter mPara = pGroup.GetParameters( ).at( j );
175
176                 arg_list += "Mthd::Aux::toCharArrray( ";
177                 if ( mPara.GetFlag( ) != "" ) {
178                     arg_list += "Mthd::Aux::toString( \"";
179                     arg_list += "-" + mPara.GetFlag( ) + "\" ";
180                     arg_list += " ) + ";
181
182                 } else if ( mPara.GetLongFlag( ) != "" ) {
183                     arg_list += "Mthd::Aux::toString( \"";
184                     arg_list += "--" + mPara.GetLongFlag( ) + "\" ";
185                     arg_list += " ) + ";
186                 }
187
188                 arg_list += "Mthd::Aux::toString( bbGetInput" + mPara.GetName( ) + "( ) )";
189                 arg_list += " ),\n";
190                 number ++;
191             }
192
193         }
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 );
200     }
201
202     void ModuleWrapper::bbUserSetDefaultValues ( ) {
203     }
204
205     void ModuleWrapper::bbUserInitializeProcessing ( ) {
206     }
207
208     void ModuleWrapper::bbUserFinalizeProcessing ( ) {
209     }
210 }
211 // EO namespace bbSlicer
212
213