1 #include "bbSlicerSlicerLibDirToBlackBoxSrc.h"
2 #include "bbSlicerPackage.h"
5 BBTK_ADD_BLACK_BOX_TO_PACKAGE ( Slicer , SlicerLibDirToBlackBoxSrc )
6 BBTK_BLACK_BOX_IMPLEMENTATION ( SlicerLibDirToBlackBoxSrc , bbtk::AtomicBlackBox ) ;
8 /// STD LD_OPEN CALLS ///
10 char* SlicerLibDirToBlackBoxSrc::
11 getModuleDescription ( const std::string lib ) {
13 void* handle = dlopen( lib.c_str( ) , RTLD_NOW | RTLD_LOCAL ) ;
15 std::cerr << "CAN'T OPEN LIBRARY: " << dlerror( ) << '\n' ;
18 typedef char* ( *method_t )( void ) ;
21 method_t myMethod = ( method_t ) dlsym( handle , "GetXMLModuleDescription" ) ;
22 const char *dlsym_error = dlerror( ) ;
24 std::cerr << "CAN'T LOAD SYMBOL 'GetXMLModuleDescription: " << dlsym_error << '\n' ;
29 char* descrption = myMethod( ) ;
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( ) ) ;
48 std::string SlicerLibDirToBlackBoxSrc::
49 loadFile ( const std::string filename ) {
50 std::string line = std::string( "" ) ;
51 std::string content = std::string( "" ) ;
53 std::ifstream infile ;
54 infile.open( filename.c_str( ) ) ;
55 while ( ! infile.eof( ) ) {
56 getline( infile , line ) ;
57 content += line + "\n" ;
65 std::string SlicerLibDirToBlackBoxSrc::
67 return loadFile( "bbSlicerDummy.dummy_cxx" ) ;
70 std::string SlicerLibDirToBlackBoxSrc::
72 return loadFile( "bbSlicerDummy.dummy_h" ) ;
75 /// BBTK BOX CREATION ///
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( ) ) ;
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 ) ;
93 std::string SlicerLibDirToBlackBoxSrc::
94 getHeaderFileName ( const ModuleDescription* module ) {
95 std::string name = Mthd::Aux::replace_str( module->GetTitle( ) , " " , "" ) ;
96 return "bbSlicer" + name + ".h" ;
99 std::string SlicerLibDirToBlackBoxSrc::
100 getBodyFileName ( const ModuleDescription* module ) {
101 std::string name = Mthd::Aux::replace_str( module->GetTitle( ) , " " , "" ) ;
103 return "bbSlicer" + name + ".cxx" ;
106 std::string SlicerLibDirToBlackBoxSrc::
107 updateBoxInputs ( const ModuleDescription* module , std::string content ) {
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( ) , " " , "" ) ;
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 ) ;
118 "BBTK_DECLARE_INPUT ( " +
119 mPara.GetName( ) + " , " +
120 mPara.GetCPPType( ) + " );\n" ;
125 mPara.GetName( ) + " , " +
126 "\"" + mPara.GetName( ) + "\"" + " , " +
127 mPara.GetCPPType( ) + ", \"\");\n" ;
130 content = Mthd::Aux::replace_str( content , "_11111_" , _cxx_inputs ) ;
131 content = Mthd::Aux::replace_str( content , "_22222_" , _cxx_inputs_end ) ;
136 const int SlicerLibDirToBlackBoxSrc::
137 getNumberOfArguments ( const ModuleDescription* module ) {
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 ++ ) {
143 ModuleParameter mPara = pGroup.GetParameters( ).at( j ) ;
150 std::string SlicerLibDirToBlackBoxSrc::
151 updateProcessMethod ( const ModuleDescription* module , std::string content , std::string lib ) {
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 ++ ) {
158 ModuleParameter mPara = pGroup.GetParameters( ).at( j ) ;
160 arg_list += "Mthd::Aux::toCharArrray( " ;
161 if ( mPara.GetFlag( ) != "" ) {
162 arg_list += "Mthd::Aux::toString( \"" ;
163 arg_list += "-" + mPara.GetFlag( ) + "\" " ;
164 arg_list += " ) + " ;
166 } else if ( mPara.GetLongFlag( ) != "" ) {
167 arg_list += "Mthd::Aux::toString( \"" ;
168 arg_list += "--" + mPara.GetLongFlag( ) + "\" " ;
169 arg_list += " ) + " ;
172 arg_list += "Mthd::Aux::toString( bbGetInput" + mPara.GetName( ) + "( ) )" ;
173 arg_list += " ),\n" ;
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 ) ;
186 std::vector < std::string > SlicerLibDirToBlackBoxSrc::
187 getFilesByExtention ( std::string dir , std::string ext ) {
188 std::vector < std::string > files ;
191 struct dirent *dirp ;
192 if ( ( dp = opendir( dir.c_str( ) ) ) == NULL ) {
193 std::cout << "ERROR OPENING " << dir << std::endl ;
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 ) ;
207 void SlicerLibDirToBlackBoxSrc::
209 const std::string dummy_body_str = loadDummyBody( ) ;
210 const std::string dummy_header_str = loadDummyHeader( ) ;
212 const std::string directory = bbGetInputLibDirectoryFullPath( ) ;
213 const std::vector < std::string > files = getFilesByExtention( directory , ".so" ) ;
215 for ( unsigned int i = 0 ; i < files.size( ) ; i ++ ) {
217 std::string gblib = directory + files.at( i ) ;
218 std::cout << "/// " << files.at( i ) << " ///" << std::endl ;
220 /// MODULE INFORMATION ///
221 std::string des = getModuleDescription( gblib ) ;
222 std::cout << " getModuleDescription( gblib )...OK" << std::endl ;
224 ModuleDescription module ;
225 ModuleDescriptionParser parser ;
227 parser.Parse( des , module ) ;
228 std::cout << " parser.Parse( des , module )...OK" << std::endl ;
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 ;
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 ) ) ;
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 ) ) ;
252 std::cout << "/// EO " << files.at( i ) << " ///" << std::endl << std::endl ;
256 //while ( getchar( ) != '\n' ) ;
261 void SlicerLibDirToBlackBoxSrc::
262 bbUserSetDefaultValues ( ) {
265 void SlicerLibDirToBlackBoxSrc::
266 bbUserInitializeProcessing ( ) {
269 void SlicerLibDirToBlackBoxSrc::
270 bbUserFinalizeProcessing ( ) {
273 // EO namespace bbSlicer