2 #include "bbSlicerSlicerLibToBlackBoxSrc.h"
3 #include "bbSlicerPackage.h"
6 BBTK_ADD_BLACK_BOX_TO_PACKAGE ( Slicer , SlicerLibToBlackBoxSrc )
7 BBTK_BLACK_BOX_IMPLEMENTATION ( SlicerLibToBlackBoxSrc , bbtk::AtomicBlackBox ) ;
9 /// STD LD_OPEN CALLS ///
11 char* SlicerLibToBlackBoxSrc::
12 getModuleDescription ( const std::string lib ) {
14 void* handle = dlopen( lib.c_str( ) , RTLD_NOW | RTLD_LOCAL ) ;
16 std::cerr << "CAN'T OPEN LIBRARY: " << dlerror( ) << '\n' ;
19 typedef char* ( *method_t )( void ) ;
22 method_t myMethod = ( method_t ) dlsym( handle , "GetXMLModuleDescription" ) ;
23 const char *dlsym_error = dlerror( ) ;
25 std::cerr << "CAN'T LOAD SYMBOL 'GetXMLModuleDescription: " << dlsym_error << '\n' ;
30 char* descrption = myMethod( ) ;
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( ) ) ;
49 std::string SlicerLibToBlackBoxSrc::
50 loadFile ( const std::string filename ) {
51 std::string line = std::string( "" ) ;
52 std::string content = std::string( "" ) ;
54 std::ifstream infile ;
55 infile.open( filename.c_str( ) ) ;
56 while ( ! infile.eof( ) ) {
57 getline( infile , line ) ;
58 content += line + "\n" ;
66 std::string SlicerLibToBlackBoxSrc::
68 return loadFile( "bbSlicerDummy.dummy_cxx" ) ;
71 std::string SlicerLibToBlackBoxSrc::
73 return loadFile( "bbSlicerDummy.dummy_h" ) ;
76 /// BBTK BOX CREATION ///
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( ) ) ;
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 ) ;
94 std::string SlicerLibToBlackBoxSrc::
95 getHeaderFileName ( const ModuleDescription* module ) {
96 std::string name = Mthd::Aux::replace_str( module->GetTitle( ) , " " , "" ) ;
97 return "bbSlicer" + name + ".h" ;
100 std::string SlicerLibToBlackBoxSrc::
101 getBodyFileName ( const ModuleDescription* module ) {
102 std::string name = Mthd::Aux::replace_str( module->GetTitle( ) , " " , "" ) ;
104 return "bbSlicer" + name + ".cxx" ;
107 std::string SlicerLibToBlackBoxSrc::
108 updateBoxInputs ( const ModuleDescription* module , std::string content ) {
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( ) , " " , "" ) ;
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 ) ;
119 "BBTK_DECLARE_INPUT ( " +
120 mPara.GetName( ) + " , " +
121 mPara.GetCPPType( ) + " );\n" ;
126 mPara.GetName( ) + " , " +
127 "\"" + mPara.GetName( ) + "\"" + " , " +
128 mPara.GetCPPType( ) + ", \"\");\n" ;
131 content = Mthd::Aux::replace_str( content , "_11111_" , _cxx_inputs ) ;
132 content = Mthd::Aux::replace_str( content , "_22222_" , _cxx_inputs_end ) ;
137 const int SlicerLibToBlackBoxSrc::
138 getNumberOfArguments ( const ModuleDescription* module ) {
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 ++ ) {
144 ModuleParameter mPara = pGroup.GetParameters( ).at( j ) ;
151 std::string SlicerLibToBlackBoxSrc::
152 updateProcessMethod ( const ModuleDescription* module , std::string content , std::string lib ) {
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 ++ ) {
159 ModuleParameter mPara = pGroup.GetParameters( ).at( j ) ;
161 arg_list += "Mthd::Aux::toCharArrray( " ;
162 if ( mPara.GetFlag( ) != "" ) {
163 arg_list += "Mthd::Aux::toString( \"" ;
164 arg_list += "-" + mPara.GetFlag( ) + "\" " ;
165 arg_list += " ) + " ;
167 } else if ( mPara.GetLongFlag( ) != "" ) {
168 arg_list += "Mthd::Aux::toString( \"" ;
169 arg_list += "--" + mPara.GetLongFlag( ) + "\" " ;
170 arg_list += " ) + " ;
173 arg_list += "Mthd::Aux::toString( bbGetInput" + mPara.GetName( ) + "( ) )" ;
174 arg_list += " ),\n" ;
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 ) ;
187 std::vector < std::string > SlicerLibToBlackBoxSrc::
188 getFilesByExtention ( std::string dir , std::string ext ) {
189 std::vector < std::string > files ;
192 struct dirent *dirp ;
193 if ( ( dp = opendir( dir.c_str( ) ) ) == NULL ) {
194 std::cout << "ERROR OPENING " << dir << std::endl ;
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 ) ;
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 ) ) ;
230 void SlicerLibToBlackBoxSrc::bbUserSetDefaultValues ( ) {
233 void SlicerLibToBlackBoxSrc::bbUserInitializeProcessing ( ) {
236 void SlicerLibToBlackBoxSrc::bbUserFinalizeProcessing ( ) {