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