]> Creatis software - creaCLI.git/blob - bbtk_Slicer_PKG/src/bbSlicerSlicerLibDirToBlackBoxSrc.cxx
NOTES:
[creaCLI.git] / bbtk_Slicer_PKG / src / bbSlicerSlicerLibDirToBlackBoxSrc.cxx
1 #include "bbSlicerSlicerLibDirToBlackBoxSrc.h"
2 #include "bbSlicerPackage.h"
3 namespace bbSlicer {
4
5     BBTK_ADD_BLACK_BOX_TO_PACKAGE ( Slicer , SlicerLibDirToBlackBoxSrc )
6     BBTK_BLACK_BOX_IMPLEMENTATION ( SlicerLibDirToBlackBoxSrc , bbtk::AtomicBlackBox ) ;
7
8     ///     STD LD_OPEN CALLS       ///
9
10     char* SlicerLibDirToBlackBoxSrc::
11     getModuleDescription ( const std::string lib ) {
12
13         void* handle = dlopen( lib.c_str( ) , RTLD_NOW | RTLD_LOCAL ) ;
14         if ( ! handle ) {
15             std::cerr << "CAN'T OPEN LIBRARY: " << dlerror( ) << '\n' ;
16             return NULL ;
17         }
18         typedef char* ( *method_t )( void ) ;
19         // RESET ERRORS
20         dlerror( ) ;
21         method_t myMethod = ( method_t ) dlsym( handle , "GetXMLModuleDescription" ) ;
22         const char *dlsym_error = dlerror( ) ;
23         if ( dlsym_error ) {
24             std::cerr << "CAN'T LOAD SYMBOL 'GetXMLModuleDescription: " << dlsym_error << '\n' ;
25             dlclose( handle ) ;
26             return NULL ;
27         }
28         // CALLING METHOD
29         char* descrption = myMethod( ) ;
30         // CLOSING LIB
31
32         //dlclose( handle ) ;
33
34         return descrption ;
35
36     }
37
38     ///     FILE RELATED     ///
39
40     void SlicerLibDirToBlackBoxSrc::
41     saveFile ( const std::string content , const std::string file_name ) {
42         std::ofstream myfile ;
43         myfile.open( file_name.c_str( ) ) ;
44         myfile << content ;
45         myfile.close( ) ;
46     }
47
48     std::string SlicerLibDirToBlackBoxSrc::
49     loadFile ( const std::string filename ) {
50         std::string line = std::string( "" ) ;
51         std::string content = std::string( "" ) ;
52
53         std::ifstream infile ;
54         infile.open( filename.c_str( ) ) ;
55         while ( ! infile.eof( ) ) {
56             getline( infile , line ) ;
57             content += line + "\n" ;
58             line.clear( ) ;
59         }
60         infile.close( ) ;
61         return content ;
62
63     }
64
65     std::string SlicerLibDirToBlackBoxSrc::
66     loadDummyBody ( ) {
67         return loadFile( "bbSlicerDummy.dummy_cxx" ) ;
68     }
69
70     std::string SlicerLibDirToBlackBoxSrc::
71     loadDummyHeader ( ) {
72         return loadFile( "bbSlicerDummy.dummy_h" ) ;
73     }
74
75     ///     BBTK BOX CREATION       /// 
76
77     std::string SlicerLibDirToBlackBoxSrc::
78     updateBoxDescription ( const ModuleDescription* module , std::string _header ) {
79         _header = Mthd::Aux::replace_str( _header , "_NNNNN_" , Mthd::Aux::replace_str( module->GetTitle( ) , " " , "" ) ) ;
80         _header = Mthd::Aux::replace_str( _header , "_AAAAA_" , module->GetContributor( ) ) ;
81         _header = Mthd::Aux::replace_str( _header , "_DDDDD_" , module->GetDescription( ) ) ;
82         _header = Mthd::Aux::replace_str( _header , "_CCCCC_" , module->GetCategory( ) ) ;
83         return _header ;
84     }
85
86     std::string SlicerLibDirToBlackBoxSrc::
87     updateBoxName ( const ModuleDescription* module , std::string content ) {
88         std::string _new_box_name = Mthd::Aux::replace_str( module->GetTitle( ) , " " , "" ) ;
89         content = Mthd::Aux::replace_str( content , "Dummy" , _new_box_name ) ;
90         return content ;
91     }
92
93     std::string SlicerLibDirToBlackBoxSrc::
94     getHeaderFileName ( const ModuleDescription* module ) {
95         std::string name = Mthd::Aux::replace_str( module->GetTitle( ) , " " , "" ) ;
96         return "bbSlicer" + name + ".h" ;
97     }
98
99     std::string SlicerLibDirToBlackBoxSrc::
100     getBodyFileName ( const ModuleDescription* module ) {
101         std::string name = Mthd::Aux::replace_str( module->GetTitle( ) , " " , "" ) ;
102
103         return "bbSlicer" + name + ".cxx" ;
104     }
105
106     std::string SlicerLibDirToBlackBoxSrc::
107     updateBoxInputs ( const ModuleDescription* module , std::string content ) {
108
109         std::string _cxx_inputs = std::string( "\n" ) ;
110         std::string _cxx_inputs_end = std::string( "\n" ) ;
111         std::string _box_name = Mthd::Aux::replace_str( module->GetTitle( ) , " " , "" ) ;
112
113         for ( unsigned long int i = 0 ; i < module->GetParameterGroups( ).size( ) ; i ++ ) {
114             ModuleParameterGroup pGroup = module->GetParameterGroups( ).at( i ) ;
115             for ( unsigned long int j = 0 ; j < pGroup.GetParameters( ).size( ) ; j ++ ) {
116                 ModuleParameter mPara = pGroup.GetParameters( ).at( j ) ;
117                 _cxx_inputs +=
118                         "BBTK_DECLARE_INPUT ( " +
119                         mPara.GetName( ) + " , " +
120                         mPara.GetCPPType( ) + " );\n" ;
121
122                 _cxx_inputs_end +=
123                         "BBTK_INPUT(" +
124                         _box_name + " , " +
125                         mPara.GetName( ) + " , " +
126                         "\"" + mPara.GetName( ) + "\"" + " , " +
127                         mPara.GetCPPType( ) + ", \"\");\n" ;
128             }
129         }
130         content = Mthd::Aux::replace_str( content , "_11111_" , _cxx_inputs ) ;
131         content = Mthd::Aux::replace_str( content , "_22222_" , _cxx_inputs_end ) ;
132
133         return content ;
134     }
135
136     const int SlicerLibDirToBlackBoxSrc::
137     getNumberOfArguments ( const ModuleDescription* module ) {
138         int number = 0 ;
139         for ( unsigned long int i = 0 ; i < module->GetParameterGroups( ).size( ) ; i ++ ) {
140             ModuleParameterGroup pGroup = module->GetParameterGroups( ).at( i ) ;
141             for ( unsigned long int j = 0 ; j < pGroup.GetParameters( ).size( ) ; j ++ ) {
142
143                 ModuleParameter mPara = pGroup.GetParameters( ).at( j ) ;
144                 number ++ ;
145             }
146         }
147         return number ;
148     }
149
150     std::string SlicerLibDirToBlackBoxSrc::
151     updateProcessMethod ( const ModuleDescription* module , std::string content , std::string lib ) {
152         int number = 0 ;
153         std::string arg_list = std::string( "" ) ;
154         for ( unsigned long int i = 0 ; i < module->GetParameterGroups( ).size( ) ; i ++ ) {
155             ModuleParameterGroup pGroup = module->GetParameterGroups( ).at( i ) ;
156             for ( unsigned long int j = 0 ; j < pGroup.GetParameters( ).size( ) ; j ++ ) {
157
158                 ModuleParameter mPara = pGroup.GetParameters( ).at( j ) ;
159
160                 arg_list += "Mthd::Aux::toCharArrray( " ;
161                 if ( mPara.GetFlag( ) != "" ) {
162                     arg_list += "Mthd::Aux::toString( \"" ;
163                     arg_list += "-" + mPara.GetFlag( ) + "\" " ;
164                     arg_list += " ) + " ;
165
166                 } else if ( mPara.GetLongFlag( ) != "" ) {
167                     arg_list += "Mthd::Aux::toString( \"" ;
168                     arg_list += "--" + mPara.GetLongFlag( ) + "\" " ;
169                     arg_list += " ) + " ;
170                 }
171
172                 arg_list += "Mthd::Aux::toString( bbGetInput" + mPara.GetName( ) + "( ) )" ;
173                 arg_list += " ),\n" ;
174                 number ++ ;
175             }
176
177         }
178         arg_list += "_EEENNNDDD_" ;
179         arg_list = Mthd::Aux::replace_str( arg_list , ",\n_EEENNNDDD_" , "" ) ;
180         std::string _argc = "\nint _argc =" + Mthd::Aux::toString( number ) + ";\n" ;
181         std::string _slib = "std::string lib = \"" + lib + "\";\n" ;
182         std::string _argv = "char * _argv[ ] = { " + arg_list + " };\n" ;
183         return Mthd::Aux::replace_str( content , "_33333_" , _argc + _slib + _argv ) ;
184     }
185
186     std::vector < std::string > SlicerLibDirToBlackBoxSrc::
187     getFilesByExtention ( std::string dir , std::string ext ) {
188         std::vector < std::string > files ;
189         files.clear( ) ;
190         DIR *dp ;
191         struct dirent *dirp ;
192         if ( ( dp = opendir( dir.c_str( ) ) ) == NULL ) {
193             std::cout << "ERROR OPENING " << dir << std::endl ;
194             return files ;
195         }
196
197         while ( ( dirp = readdir( dp ) ) != NULL ) {
198             std::string file = std::string( dirp->d_name ) ;
199             if ( Mthd::Aux::str_ends_with( file , ext ) ) {
200                 files.push_back( file ) ;
201             }
202         }
203         closedir( dp ) ;
204         return files ;
205     }
206
207     void SlicerLibDirToBlackBoxSrc::
208     Process ( ) {
209         const std::string dummy_body_str = loadDummyBody( ) ;
210         const std::string dummy_header_str = loadDummyHeader( ) ;
211
212         const std::string directory = bbGetInputLibDirectoryFullPath( ) ;
213         const std::vector < std::string > files = getFilesByExtention( directory , ".so" ) ;
214
215         for ( unsigned int i = 0 ; i < files.size( ) ; i ++ ) {
216
217             std::string gblib = directory + files.at( i ) ;
218             std::cout << "/// " << files.at( i ) << " ///" << std::endl ;
219
220             ///     MODULE INFORMATION      ///
221             std::string des = getModuleDescription( gblib ) ;
222             std::cout << "  getModuleDescription( gblib )...OK" << std::endl ;
223
224             ModuleDescription module ;
225             ModuleDescriptionParser parser ;
226
227             parser.Parse( des , module ) ;
228             std::cout << "  parser.Parse( des , module )...OK" << std::endl ;
229
230             ///     LOADING DUMMY FILES     ///
231             std::string _body = std::string( dummy_body_str ) ;
232             std::cout << "  loadDummyBody( )...OK" << std::endl ;
233             std::string _header = std::string( dummy_header_str ) ;
234             std::cout << "  loadDummyHeader( )...OK" << std::endl ;
235
236             ///     SETTING HEADER FILE     ///
237             _header = updateBoxName( &module , _header ) ;
238             std::cout << "  updateBoxName( &module , _header )...OK" << std::endl ;
239             _header = updateBoxDescription( &module , _header ) ;
240             std::cout << "  updateBoxDescription( &module , _header )...OK" << std::endl ;
241             _header = updateBoxInputs( &module , _header ) ;
242             std::cout << "  updateBoxInputs( &module , _header )...OK" << std::endl ;
243             saveFile( _header , getHeaderFileName( &module ) ) ;
244
245             ///     SETTING BODY FILE       ///
246             _body = updateBoxName( &module , _body ) ;
247             std::cout << "  updateBoxName( &module , _body )...OK" << std::endl ;
248             _body = updateProcessMethod( &module , _body , gblib ) ;
249             std::cout << "  updateProcessMethod( &module , _body , gblib )...OK" << std::endl ;
250             saveFile( _body , bbGetInputSrcDestinationDir( ) + "/" + getBodyFileName( &module ) ) ;
251
252             std::cout << "/// EO " << files.at( i ) << " ///" << std::endl << std::endl ;
253
254             gblib.clear( ) ;
255             des.clear( ) ;
256             //while ( getchar( ) != '\n' ) ;
257
258         }
259     }
260
261     void SlicerLibDirToBlackBoxSrc::
262     bbUserSetDefaultValues ( ) {
263     }
264
265     void SlicerLibDirToBlackBoxSrc::
266     bbUserInitializeProcessing ( ) {
267     }
268
269     void SlicerLibDirToBlackBoxSrc::
270     bbUserFinalizeProcessing ( ) {
271     }
272 }
273 // EO namespace bbSlicer
274
275