From b7a0497a4afc48d5230c279aeab20eb756e4427b Mon Sep 17 00:00:00 2001 From: riveros Date: Wed, 18 Apr 2012 08:00:13 +0200 Subject: [PATCH] NOTES: New set of boxes created. First Generates a single box from a single shared library. Second takes a complete directory and gnereates all boxes from shared libs within. BUGS: Apparently bbEditor does not recognizes the inputs for visualization.... --- bbtk_Slicer_PKG/src/CreationTool.cxx | 160 +++++----- bbtk_Slicer_PKG/src/CreationTool.h | 3 +- .../src/bbSlicerSlicerLibDirToBlackBoxSrc.cxx | 275 ++++++++++++++++++ .../src/bbSlicerSlicerLibDirToBlackBoxSrc.h | 69 +++++ .../src/bbSlicerSlicerLibToBlackBoxSrc.cxx | 241 +++++++++++++++ .../src/bbSlicerSlicerLibToBlackBoxSrc.h | 69 +++++ 6 files changed, 740 insertions(+), 77 deletions(-) create mode 100644 bbtk_Slicer_PKG/src/bbSlicerSlicerLibDirToBlackBoxSrc.cxx create mode 100644 bbtk_Slicer_PKG/src/bbSlicerSlicerLibDirToBlackBoxSrc.h create mode 100644 bbtk_Slicer_PKG/src/bbSlicerSlicerLibToBlackBoxSrc.cxx create mode 100644 bbtk_Slicer_PKG/src/bbSlicerSlicerLibToBlackBoxSrc.h diff --git a/bbtk_Slicer_PKG/src/CreationTool.cxx b/bbtk_Slicer_PKG/src/CreationTool.cxx index 8203b4d..b3c7095 100644 --- a/bbtk_Slicer_PKG/src/CreationTool.cxx +++ b/bbtk_Slicer_PKG/src/CreationTool.cxx @@ -8,157 +8,165 @@ #include "CreationTool.h" std::string Mthd::Aux::toString ( float n ) { - std::ostringstream oss; - oss << n; - return oss.str( ); + std::ostringstream oss ; + oss << n ; + return oss.str( ) ; } std::string Mthd::Aux::toString ( double n ) { - std::ostringstream oss; - oss << n; - return oss.str( ); + std::ostringstream oss ; + oss << n ; + return oss.str( ) ; } std::string Mthd::Aux::toString ( long double n ) { - std::ostringstream oss; - oss << n; - return oss.str( ); + std::ostringstream oss ; + oss << n ; + return oss.str( ) ; } std::string Mthd::Aux::toString ( char n ) { - std::ostringstream oss; - oss << n; - return oss.str( ); + std::ostringstream oss ; + oss << n ; + return oss.str( ) ; } std::string Mthd::Aux::toString ( unsigned char n ) { - std::ostringstream oss; - oss << n; - return oss.str( ); + std::ostringstream oss ; + oss << n ; + return oss.str( ) ; } std::string Mthd::Aux::toString ( short n ) { - std::ostringstream oss; - oss << n; - return oss.str( ); + std::ostringstream oss ; + oss << n ; + return oss.str( ) ; } std::string Mthd::Aux::toString ( unsigned short n ) { - std::ostringstream oss; - oss << n; - return oss.str( ); + std::ostringstream oss ; + oss << n ; + return oss.str( ) ; } std::string Mthd::Aux::toString ( int n ) { - std::ostringstream oss; - oss << n; - return oss.str( ); + std::ostringstream oss ; + oss << n ; + return oss.str( ) ; } std::string Mthd::Aux::toString ( long int n ) { - std::ostringstream oss; - oss << n; - return oss.str( ); + std::ostringstream oss ; + oss << n ; + return oss.str( ) ; } std::string Mthd::Aux::toString ( unsigned int n ) { - std::ostringstream oss; - oss << n; - return oss.str( ); + std::ostringstream oss ; + oss << n ; + return oss.str( ) ; } std::string Mthd::Aux::toString ( unsigned long n ) { - std::ostringstream oss; - oss << n; - return oss.str( ); + std::ostringstream oss ; + oss << n ; + return oss.str( ) ; } std::string Mthd::Aux::toString ( unsigned long long n ) { - std::ostringstream oss; - oss << n; - return oss.str( ); + std::ostringstream oss ; + oss << n ; + return oss.str( ) ; } std::string Mthd::Aux::toString ( std::string n ) { - return n; + return n ; } char* Mthd::Aux::toCharArrray ( float n ) { - std::ostringstream oss; - oss << n; - return const_cast < char* > ( oss.str( ).data( ) ); + std::ostringstream oss ; + oss << n ; + return const_cast < char* > ( oss.str( ).data( ) ) ; } char* Mthd::Aux::toCharArrray ( double n ) { - std::ostringstream oss; - oss << n; - return const_cast < char* > ( oss.str( ).data( ) ); + std::ostringstream oss ; + oss << n ; + return const_cast < char* > ( oss.str( ).data( ) ) ; } char* Mthd::Aux::toCharArrray ( long double n ) { - std::ostringstream oss; - oss << n; - return const_cast < char* > ( oss.str( ).data( ) ); + std::ostringstream oss ; + oss << n ; + return const_cast < char* > ( oss.str( ).data( ) ) ; } char* Mthd::Aux::toCharArrray ( char n ) { - std::ostringstream oss; - oss << n; - return const_cast < char* > ( oss.str( ).data( ) ); + std::ostringstream oss ; + oss << n ; + return const_cast < char* > ( oss.str( ).data( ) ) ; } char* Mthd::Aux::toCharArrray ( unsigned char n ) { - std::ostringstream oss; - oss << n; - return const_cast < char* > ( oss.str( ).data( ) ); + std::ostringstream oss ; + oss << n ; + return const_cast < char* > ( oss.str( ).data( ) ) ; } char* Mthd::Aux::toCharArrray ( short n ) { - std::ostringstream oss; - oss << n; - return const_cast < char* > ( oss.str( ).data( ) ); + std::ostringstream oss ; + oss << n ; + return const_cast < char* > ( oss.str( ).data( ) ) ; } char* Mthd::Aux::toCharArrray ( unsigned short n ) { - std::ostringstream oss; - oss << n; - return const_cast < char* > ( oss.str( ).data( ) ); + std::ostringstream oss ; + oss << n ; + return const_cast < char* > ( oss.str( ).data( ) ) ; } char* Mthd::Aux::toCharArrray ( int n ) { - std::ostringstream oss; - oss << n; - return const_cast < char* > ( oss.str( ).data( ) ); + std::ostringstream oss ; + oss << n ; + return const_cast < char* > ( oss.str( ).data( ) ) ; } char* Mthd::Aux::toCharArrray ( long int n ) { - std::ostringstream oss; - oss << n; - return const_cast < char* > ( oss.str( ).data( ) ); + std::ostringstream oss ; + oss << n ; + return const_cast < char* > ( oss.str( ).data( ) ) ; } char* Mthd::Aux::toCharArrray ( unsigned int n ) { - std::ostringstream oss; - oss << n; - return const_cast < char* > ( oss.str( ).data( ) ); + std::ostringstream oss ; + oss << n ; + return const_cast < char* > ( oss.str( ).data( ) ) ; } char* Mthd::Aux::toCharArrray ( long unsigned int n ) { - std::ostringstream oss; - oss << n; - return const_cast < char* > ( oss.str( ).data( ) ); + std::ostringstream oss ; + oss << n ; + return const_cast < char* > ( oss.str( ).data( ) ) ; } char* Mthd::Aux::toCharArrray ( std::string n ) { - return const_cast < char* > ( n.data( ) ); + return const_cast < char* > ( n.data( ) ) ; } -std::string Mthd::Aux::replace_str ( std::string input, std::string old_str, std::string new_str ) { - size_t found = input.find( old_str ); +std::string Mthd::Aux::replace_str ( std::string input , std::string old_str , std::string new_str ) { + size_t found = input.find( old_str ) ; while ( found != std::string::npos ) { - input.replace( found, old_str.length( ), new_str ); - found = input.find( old_str ); + input.replace( found , old_str.length( ) , new_str ) ; + found = input.find( old_str ) ; } - return input; + return input ; +} + +bool Mthd::Aux::str_ends_with ( std::string total_str , std::string sub_str ) { + size_t found = total_str.find( sub_str ) ; + if ( found != std::string::npos ) { + return true ; + } + return false ; } diff --git a/bbtk_Slicer_PKG/src/CreationTool.h b/bbtk_Slicer_PKG/src/CreationTool.h index 1859178..27dcb82 100644 --- a/bbtk_Slicer_PKG/src/CreationTool.h +++ b/bbtk_Slicer_PKG/src/CreationTool.h @@ -51,7 +51,8 @@ namespace Mthd { static char* toCharArrray ( std::string n ) ; - static std::string replace_str ( std::string input , std::string old_str , std::string new_str ) ; + static bool str_ends_with ( std::string total_str , std::string sub_str ) ; + static std::string replace_str ( std::string input , std::string old_str , std::string new_str ) ; } ; diff --git a/bbtk_Slicer_PKG/src/bbSlicerSlicerLibDirToBlackBoxSrc.cxx b/bbtk_Slicer_PKG/src/bbSlicerSlicerLibDirToBlackBoxSrc.cxx new file mode 100644 index 0000000..0708033 --- /dev/null +++ b/bbtk_Slicer_PKG/src/bbSlicerSlicerLibDirToBlackBoxSrc.cxx @@ -0,0 +1,275 @@ +#include "bbSlicerSlicerLibDirToBlackBoxSrc.h" +#include "bbSlicerPackage.h" +namespace bbSlicer { + + BBTK_ADD_BLACK_BOX_TO_PACKAGE ( Slicer , SlicerLibDirToBlackBoxSrc ) + BBTK_BLACK_BOX_IMPLEMENTATION ( SlicerLibDirToBlackBoxSrc , bbtk::AtomicBlackBox ) ; + + /// STD LD_OPEN CALLS /// + + char* SlicerLibDirToBlackBoxSrc:: + getModuleDescription ( const std::string lib ) { + + void* handle = dlopen( lib.c_str( ) , RTLD_NOW | RTLD_LOCAL ) ; + if ( ! handle ) { + std::cerr << "CAN'T OPEN LIBRARY: " << dlerror( ) << '\n' ; + return NULL ; + } + typedef char* ( *method_t )( void ) ; + // RESET ERRORS + dlerror( ) ; + method_t myMethod = ( method_t ) dlsym( handle , "GetXMLModuleDescription" ) ; + const char *dlsym_error = dlerror( ) ; + if ( dlsym_error ) { + std::cerr << "CAN'T LOAD SYMBOL 'GetXMLModuleDescription: " << dlsym_error << '\n' ; + dlclose( handle ) ; + return NULL ; + } + // CALLING METHOD + char* descrption = myMethod( ) ; + // CLOSING LIB + + //dlclose( handle ) ; + + return descrption ; + + } + + /// FILE RELATED /// + + void SlicerLibDirToBlackBoxSrc:: + saveFile ( const std::string content , const std::string file_name ) { + std::ofstream myfile ; + myfile.open( file_name.c_str( ) ) ; + myfile << content ; + myfile.close( ) ; + } + + std::string SlicerLibDirToBlackBoxSrc:: + loadFile ( const std::string filename ) { + std::string line = std::string( "" ) ; + std::string content = std::string( "" ) ; + + std::ifstream infile ; + infile.open( filename.c_str( ) ) ; + while ( ! infile.eof( ) ) { + getline( infile , line ) ; + content += line + "\n" ; + line.clear( ) ; + } + infile.close( ) ; + return content ; + + } + + std::string SlicerLibDirToBlackBoxSrc:: + loadDummyBody ( ) { + return loadFile( "bbSlicerDummy.dummy_cxx" ) ; + } + + std::string SlicerLibDirToBlackBoxSrc:: + loadDummyHeader ( ) { + return loadFile( "bbSlicerDummy.dummy_h" ) ; + } + + /// BBTK BOX CREATION /// + + std::string SlicerLibDirToBlackBoxSrc:: + updateBoxDescription ( const ModuleDescription* module , std::string _header ) { + _header = Mthd::Aux::replace_str( _header , "_NNNNN_" , Mthd::Aux::replace_str( module->GetTitle( ) , " " , "" ) ) ; + _header = Mthd::Aux::replace_str( _header , "_AAAAA_" , module->GetContributor( ) ) ; + _header = Mthd::Aux::replace_str( _header , "_DDDDD_" , module->GetDescription( ) ) ; + _header = Mthd::Aux::replace_str( _header , "_CCCCC_" , module->GetCategory( ) ) ; + return _header ; + } + + std::string SlicerLibDirToBlackBoxSrc:: + updateBoxName ( const ModuleDescription* module , std::string content ) { + std::string _new_box_name = Mthd::Aux::replace_str( module->GetTitle( ) , " " , "" ) ; + content = Mthd::Aux::replace_str( content , "Dummy" , _new_box_name ) ; + return content ; + } + + std::string SlicerLibDirToBlackBoxSrc:: + getHeaderFileName ( const ModuleDescription* module ) { + std::string name = Mthd::Aux::replace_str( module->GetTitle( ) , " " , "" ) ; + return "bbSlicer" + name + ".h" ; + } + + std::string SlicerLibDirToBlackBoxSrc:: + getBodyFileName ( const ModuleDescription* module ) { + std::string name = Mthd::Aux::replace_str( module->GetTitle( ) , " " , "" ) ; + + return "bbSlicer" + name + ".cxx" ; + } + + std::string SlicerLibDirToBlackBoxSrc:: + updateBoxInputs ( const ModuleDescription* module , std::string content ) { + + std::string _cxx_inputs = std::string( "\n" ) ; + std::string _cxx_inputs_end = std::string( "\n" ) ; + std::string _box_name = Mthd::Aux::replace_str( module->GetTitle( ) , " " , "" ) ; + + for ( unsigned long int i = 0 ; i < module->GetParameterGroups( ).size( ) ; i ++ ) { + ModuleParameterGroup pGroup = module->GetParameterGroups( ).at( i ) ; + for ( unsigned long int j = 0 ; j < pGroup.GetParameters( ).size( ) ; j ++ ) { + ModuleParameter mPara = pGroup.GetParameters( ).at( j ) ; + _cxx_inputs += + "BBTK_DECLARE_INPUT ( " + + mPara.GetName( ) + " , " + + mPara.GetCPPType( ) + " );\n" ; + + _cxx_inputs_end += + "BBTK_INPUT(" + + _box_name + " , " + + mPara.GetName( ) + " , " + + "\"" + mPara.GetName( ) + "\"" + " , " + + mPara.GetCPPType( ) + ", \"\");\n" ; + } + } + content = Mthd::Aux::replace_str( content , "_11111_" , _cxx_inputs ) ; + content = Mthd::Aux::replace_str( content , "_22222_" , _cxx_inputs_end ) ; + + return content ; + } + + const int SlicerLibDirToBlackBoxSrc:: + getNumberOfArguments ( const ModuleDescription* module ) { + int number = 0 ; + for ( unsigned long int i = 0 ; i < module->GetParameterGroups( ).size( ) ; i ++ ) { + ModuleParameterGroup pGroup = module->GetParameterGroups( ).at( i ) ; + for ( unsigned long int j = 0 ; j < pGroup.GetParameters( ).size( ) ; j ++ ) { + + ModuleParameter mPara = pGroup.GetParameters( ).at( j ) ; + number ++ ; + } + } + return number ; + } + + std::string SlicerLibDirToBlackBoxSrc:: + updateProcessMethod ( const ModuleDescription* module , std::string content , std::string lib ) { + int number = 0 ; + std::string arg_list = std::string( "" ) ; + for ( unsigned long int i = 0 ; i < module->GetParameterGroups( ).size( ) ; i ++ ) { + ModuleParameterGroup pGroup = module->GetParameterGroups( ).at( i ) ; + for ( unsigned long int j = 0 ; j < pGroup.GetParameters( ).size( ) ; j ++ ) { + + ModuleParameter mPara = pGroup.GetParameters( ).at( j ) ; + + arg_list += "Mthd::Aux::toCharArrray( " ; + if ( mPara.GetFlag( ) != "" ) { + arg_list += "Mthd::Aux::toString( \"" ; + arg_list += "-" + mPara.GetFlag( ) + "\" " ; + arg_list += " ) + " ; + + } else if ( mPara.GetLongFlag( ) != "" ) { + arg_list += "Mthd::Aux::toString( \"" ; + arg_list += "--" + mPara.GetLongFlag( ) + "\" " ; + arg_list += " ) + " ; + } + + arg_list += "Mthd::Aux::toString( bbGetInput" + mPara.GetName( ) + "( ) )" ; + arg_list += " ),\n" ; + number ++ ; + } + + } + arg_list += "_EEENNNDDD_" ; + arg_list = Mthd::Aux::replace_str( arg_list , ",\n_EEENNNDDD_" , "" ) ; + std::string _argc = "\nint _argc =" + Mthd::Aux::toString( number ) + ";\n" ; + std::string _slib = "std::string lib = \"" + lib + "\";\n" ; + std::string _argv = "char * _argv[ ] = { " + arg_list + " };\n" ; + return Mthd::Aux::replace_str( content , "_33333_" , _argc + _slib + _argv ) ; + } + + std::vector < std::string > SlicerLibDirToBlackBoxSrc:: + getFilesByExtention ( std::string dir , std::string ext ) { + std::vector < std::string > files ; + files.clear( ) ; + DIR *dp ; + struct dirent *dirp ; + if ( ( dp = opendir( dir.c_str( ) ) ) == NULL ) { + std::cout << "ERROR OPENING " << dir << std::endl ; + return files ; + } + + while ( ( dirp = readdir( dp ) ) != NULL ) { + std::string file = std::string( dirp->d_name ) ; + if ( Mthd::Aux::str_ends_with( file , ext ) ) { + files.push_back( file ) ; + } + } + closedir( dp ) ; + return files ; + } + + void SlicerLibDirToBlackBoxSrc:: + Process ( ) { + const std::string dummy_body_str = loadDummyBody( ) ; + const std::string dummy_header_str = loadDummyHeader( ) ; + + const std::string directory = bbGetInputLibDirectoryFullPath( ) ; + const std::vector < std::string > files = getFilesByExtention( directory , ".so" ) ; + + for ( unsigned int i = 0 ; i < files.size( ) ; i ++ ) { + + std::string gblib = directory + files.at( i ) ; + std::cout << "/// " << files.at( i ) << " ///" << std::endl ; + + /// MODULE INFORMATION /// + std::string des = getModuleDescription( gblib ) ; + std::cout << " getModuleDescription( gblib )...OK" << std::endl ; + + ModuleDescription module ; + ModuleDescriptionParser parser ; + + parser.Parse( des , module ) ; + std::cout << " parser.Parse( des , module )...OK" << std::endl ; + + /// LOADING DUMMY FILES /// + std::string _body = std::string( dummy_body_str ) ; + std::cout << " loadDummyBody( )...OK" << std::endl ; + std::string _header = std::string( dummy_header_str ) ; + std::cout << " loadDummyHeader( )...OK" << std::endl ; + + /// SETTING HEADER FILE /// + _header = updateBoxName( &module , _header ) ; + std::cout << " updateBoxName( &module , _header )...OK" << std::endl ; + _header = updateBoxDescription( &module , _header ) ; + std::cout << " updateBoxDescription( &module , _header )...OK" << std::endl ; + _header = updateBoxInputs( &module , _header ) ; + std::cout << " updateBoxInputs( &module , _header )...OK" << std::endl ; + saveFile( _header , getHeaderFileName( &module ) ) ; + + /// SETTING BODY FILE /// + _body = updateBoxName( &module , _body ) ; + std::cout << " updateBoxName( &module , _body )...OK" << std::endl ; + _body = updateProcessMethod( &module , _body , gblib ) ; + std::cout << " updateProcessMethod( &module , _body , gblib )...OK" << std::endl ; + saveFile( _body , bbGetInputSrcDestinationDir( ) + "/" + getBodyFileName( &module ) ) ; + + std::cout << "/// EO " << files.at( i ) << " ///" << std::endl << std::endl ; + + gblib.clear( ) ; + des.clear( ) ; + //while ( getchar( ) != '\n' ) ; + + } + } + + void SlicerLibDirToBlackBoxSrc:: + bbUserSetDefaultValues ( ) { + } + + void SlicerLibDirToBlackBoxSrc:: + bbUserInitializeProcessing ( ) { + } + + void SlicerLibDirToBlackBoxSrc:: + bbUserFinalizeProcessing ( ) { + } +} +// EO namespace bbSlicer + + diff --git a/bbtk_Slicer_PKG/src/bbSlicerSlicerLibDirToBlackBoxSrc.h b/bbtk_Slicer_PKG/src/bbSlicerSlicerLibDirToBlackBoxSrc.h new file mode 100644 index 0000000..0742501 --- /dev/null +++ b/bbtk_Slicer_PKG/src/bbSlicerSlicerLibDirToBlackBoxSrc.h @@ -0,0 +1,69 @@ +#ifndef __bbSlicerSlicerLibDirToBlackBoxSrc_h_INCLUDED__ +#define __bbSlicerSlicerLibDirToBlackBoxSrc_h_INCLUDED__ + +#include "bbSlicer_EXPORT.h" +#include "bbtkAtomicBlackBox.h" + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +#include "CreationTool.h" + +namespace bbSlicer { + + class bbSlicer_EXPORT SlicerLibDirToBlackBoxSrc + : + public bbtk::AtomicBlackBox { + BBTK_BLACK_BOX_INTERFACE ( SlicerLibDirToBlackBoxSrc , bbtk::AtomicBlackBox ) ; + BBTK_DECLARE_INPUT ( LibDirectoryFullPath , std::string ) ; + BBTK_DECLARE_INPUT ( SrcDestinationDir , std::string ) ; + BBTK_PROCESS ( Process ) ; + void Process ( ) ; + + /// STD LD_OPEN CALLS /// + char* getModuleDescription ( const std::string lib ) ; + /// FILE RELATED /// + void saveFile ( const std::string content , const std::string file_name ) ; + std::string loadFile ( const std::string filename ) ; + std::string loadDummyBody ( ) ; + std::string loadDummyHeader ( ) ; + /// BBTK BOX CREATION /// + std::string updateBoxDescription ( const ModuleDescription* module , std::string _header ) ; + std::string updateBoxName ( const ModuleDescription* module , std::string content ) ; + std::string getHeaderFileName ( const ModuleDescription* module ) ; + std::string getBodyFileName ( const ModuleDescription* module ) ; + std::string updateBoxInputs ( const ModuleDescription* module , std::string content ) ; + const int getNumberOfArguments ( const ModuleDescription* module ) ; + std::string updateProcessMethod ( const ModuleDescription* module , std::string content , std::string lib ) ; + std::vector < std::string > getFilesByExtention ( std::string dir , std::string ext ) ; + } ; + + BBTK_BEGIN_DESCRIBE_BLACK_BOX ( SlicerLibDirToBlackBoxSrc , bbtk::AtomicBlackBox ) ; + BBTK_NAME ( "SlicerLibDirToBlackBoxSrc" ) ; + BBTK_AUTHOR ( "Juan gabriel RIVEROS REYES" ) ; + BBTK_DESCRIPTION ( "Generates all C++ source code from all Slicers shared libs in a directory for later compilation" ) ; + BBTK_CATEGORY ( "" ) ; + BBTK_INPUT ( SlicerLibDirToBlackBoxSrc , LibDirectoryFullPath , "Full path to shared libraries' directory" , std::string , "" ) ; + BBTK_INPUT ( SlicerLibDirToBlackBoxSrc , SrcDestinationDir , "Source code destination directory" , std::string , "" ) ; + BBTK_END_DESCRIBE_BLACK_BOX ( SlicerLibDirToBlackBoxSrc ) ; + +} + +#endif + diff --git a/bbtk_Slicer_PKG/src/bbSlicerSlicerLibToBlackBoxSrc.cxx b/bbtk_Slicer_PKG/src/bbSlicerSlicerLibToBlackBoxSrc.cxx new file mode 100644 index 0000000..df7074a --- /dev/null +++ b/bbtk_Slicer_PKG/src/bbSlicerSlicerLibToBlackBoxSrc.cxx @@ -0,0 +1,241 @@ + +#include "bbSlicerSlicerLibToBlackBoxSrc.h" +#include "bbSlicerPackage.h" +namespace bbSlicer { + + BBTK_ADD_BLACK_BOX_TO_PACKAGE ( Slicer , SlicerLibToBlackBoxSrc ) + BBTK_BLACK_BOX_IMPLEMENTATION ( SlicerLibToBlackBoxSrc , bbtk::AtomicBlackBox ) ; + + /// STD LD_OPEN CALLS /// + + char* SlicerLibToBlackBoxSrc:: + getModuleDescription ( const std::string lib ) { + + void* handle = dlopen( lib.c_str( ) , RTLD_NOW | RTLD_LOCAL ) ; + if ( ! handle ) { + std::cerr << "CAN'T OPEN LIBRARY: " << dlerror( ) << '\n' ; + return NULL ; + } + typedef char* ( *method_t )( void ) ; + // RESET ERRORS + dlerror( ) ; + method_t myMethod = ( method_t ) dlsym( handle , "GetXMLModuleDescription" ) ; + const char *dlsym_error = dlerror( ) ; + if ( dlsym_error ) { + std::cerr << "CAN'T LOAD SYMBOL 'GetXMLModuleDescription: " << dlsym_error << '\n' ; + dlclose( handle ) ; + return NULL ; + } + // CALLING METHOD + char* descrption = myMethod( ) ; + // CLOSING LIB + + //dlclose( handle ) ; + + return descrption ; + + } + + /// FILE RELATED /// + + void SlicerLibToBlackBoxSrc:: + saveFile ( const std::string content , const std::string file_name ) { + std::ofstream myfile ; + myfile.open( file_name.c_str( ) ) ; + myfile << content ; + myfile.close( ) ; + } + + std::string SlicerLibToBlackBoxSrc:: + loadFile ( const std::string filename ) { + std::string line = std::string( "" ) ; + std::string content = std::string( "" ) ; + + std::ifstream infile ; + infile.open( filename.c_str( ) ) ; + while ( ! infile.eof( ) ) { + getline( infile , line ) ; + content += line + "\n" ; + line.clear( ) ; + } + infile.close( ) ; + return content ; + + } + + std::string SlicerLibToBlackBoxSrc:: + loadDummyBody ( ) { + return loadFile( "bbSlicerDummy.dummy_cxx" ) ; + } + + std::string SlicerLibToBlackBoxSrc:: + loadDummyHeader ( ) { + return loadFile( "bbSlicerDummy.dummy_h" ) ; + } + + /// BBTK BOX CREATION /// + + std::string SlicerLibToBlackBoxSrc:: + updateBoxDescription ( const ModuleDescription* module , std::string _header ) { + _header = Mthd::Aux::replace_str( _header , "_NNNNN_" , Mthd::Aux::replace_str( module->GetTitle( ) , " " , "" ) ) ; + _header = Mthd::Aux::replace_str( _header , "_AAAAA_" , module->GetContributor( ) ) ; + _header = Mthd::Aux::replace_str( _header , "_DDDDD_" , module->GetDescription( ) ) ; + _header = Mthd::Aux::replace_str( _header , "_CCCCC_" , module->GetCategory( ) ) ; + return _header ; + } + + std::string SlicerLibToBlackBoxSrc:: + updateBoxName ( const ModuleDescription* module , std::string content ) { + std::string _new_box_name = Mthd::Aux::replace_str( module->GetTitle( ) , " " , "" ) ; + content = Mthd::Aux::replace_str( content , "Dummy" , _new_box_name ) ; + return content ; + } + + std::string SlicerLibToBlackBoxSrc:: + getHeaderFileName ( const ModuleDescription* module ) { + std::string name = Mthd::Aux::replace_str( module->GetTitle( ) , " " , "" ) ; + return "bbSlicer" + name + ".h" ; + } + + std::string SlicerLibToBlackBoxSrc:: + getBodyFileName ( const ModuleDescription* module ) { + std::string name = Mthd::Aux::replace_str( module->GetTitle( ) , " " , "" ) ; + + return "bbSlicer" + name + ".cxx" ; + } + + std::string SlicerLibToBlackBoxSrc:: + updateBoxInputs ( const ModuleDescription* module , std::string content ) { + + std::string _cxx_inputs = std::string( "\n" ) ; + std::string _cxx_inputs_end = std::string( "\n" ) ; + std::string _box_name = Mthd::Aux::replace_str( module->GetTitle( ) , " " , "" ) ; + + for ( unsigned long int i = 0 ; i < module->GetParameterGroups( ).size( ) ; i ++ ) { + ModuleParameterGroup pGroup = module->GetParameterGroups( ).at( i ) ; + for ( unsigned long int j = 0 ; j < pGroup.GetParameters( ).size( ) ; j ++ ) { + ModuleParameter mPara = pGroup.GetParameters( ).at( j ) ; + _cxx_inputs += + "BBTK_DECLARE_INPUT ( " + + mPara.GetName( ) + " , " + + mPara.GetCPPType( ) + " );\n" ; + + _cxx_inputs_end += + "BBTK_INPUT(" + + _box_name + " , " + + mPara.GetName( ) + " , " + + "\"" + mPara.GetName( ) + "\"" + " , " + + mPara.GetCPPType( ) + ", \"\");\n" ; + } + } + content = Mthd::Aux::replace_str( content , "_11111_" , _cxx_inputs ) ; + content = Mthd::Aux::replace_str( content , "_22222_" , _cxx_inputs_end ) ; + + return content ; + } + + const int SlicerLibToBlackBoxSrc:: + getNumberOfArguments ( const ModuleDescription* module ) { + int number = 0 ; + for ( unsigned long int i = 0 ; i < module->GetParameterGroups( ).size( ) ; i ++ ) { + ModuleParameterGroup pGroup = module->GetParameterGroups( ).at( i ) ; + for ( unsigned long int j = 0 ; j < pGroup.GetParameters( ).size( ) ; j ++ ) { + + ModuleParameter mPara = pGroup.GetParameters( ).at( j ) ; + number ++ ; + } + } + return number ; + } + + std::string SlicerLibToBlackBoxSrc:: + updateProcessMethod ( const ModuleDescription* module , std::string content , std::string lib ) { + int number = 0 ; + std::string arg_list = std::string( "" ) ; + for ( unsigned long int i = 0 ; i < module->GetParameterGroups( ).size( ) ; i ++ ) { + ModuleParameterGroup pGroup = module->GetParameterGroups( ).at( i ) ; + for ( unsigned long int j = 0 ; j < pGroup.GetParameters( ).size( ) ; j ++ ) { + + ModuleParameter mPara = pGroup.GetParameters( ).at( j ) ; + + arg_list += "Mthd::Aux::toCharArrray( " ; + if ( mPara.GetFlag( ) != "" ) { + arg_list += "Mthd::Aux::toString( \"" ; + arg_list += "-" + mPara.GetFlag( ) + "\" " ; + arg_list += " ) + " ; + + } else if ( mPara.GetLongFlag( ) != "" ) { + arg_list += "Mthd::Aux::toString( \"" ; + arg_list += "--" + mPara.GetLongFlag( ) + "\" " ; + arg_list += " ) + " ; + } + + arg_list += "Mthd::Aux::toString( bbGetInput" + mPara.GetName( ) + "( ) )" ; + arg_list += " ),\n" ; + number ++ ; + } + + } + arg_list += "_EEENNNDDD_" ; + arg_list = Mthd::Aux::replace_str( arg_list , ",\n_EEENNNDDD_" , "" ) ; + std::string _argc = "\nint _argc =" + Mthd::Aux::toString( number ) + ";\n" ; + std::string _slib = "std::string lib = \"" + lib + "\";\n" ; + std::string _argv = "char * _argv[ ] = { " + arg_list + " };\n" ; + return Mthd::Aux::replace_str( content , "_33333_" , _argc + _slib + _argv ) ; + } + + std::vector < std::string > SlicerLibToBlackBoxSrc:: + getFilesByExtention ( std::string dir , std::string ext ) { + std::vector < std::string > files ; + files.clear( ) ; + DIR *dp ; + struct dirent *dirp ; + if ( ( dp = opendir( dir.c_str( ) ) ) == NULL ) { + std::cout << "ERROR OPENING " << dir << std::endl ; + return files ; + } + + while ( ( dirp = readdir( dp ) ) != NULL ) { + std::string file = std::string( dirp->d_name ) ; + if ( Mthd::Aux::str_ends_with( file , ext ) ) { + files.push_back( file ) ; + } + } + closedir( dp ) ; + return files ; + } + + void SlicerLibToBlackBoxSrc::Process ( ) { + std::string gblib = bbGetInputLibFullPath( ) ; + /// MODULE INFORMATION /// + std::string des = getModuleDescription( gblib ) ; + ModuleDescription module ; + ModuleDescriptionParser parser ; + parser.Parse( des , module ) ; + /// LOADING DUMMY FILES /// + std::string _body = loadDummyBody( ) ; + std::string _header = loadDummyHeader( ) ; + /// SETTING HEADER FILE /// + _header = updateBoxName( &module , _header ) ; + _header = updateBoxDescription( &module , _header ) ; + _header = updateBoxInputs( &module , _header ) ; + saveFile( _header , getHeaderFileName( &module ) ) ; + /// SETTING BODY FILE /// + _body = updateBoxName( &module , _body ) ; + _body = updateProcessMethod( &module , _body , gblib ) ; + saveFile( _body , bbGetInputSrcDestinationDir( ) + "/" + getBodyFileName( &module ) ) ; + + } + + void SlicerLibToBlackBoxSrc::bbUserSetDefaultValues ( ) { + } + + void SlicerLibToBlackBoxSrc::bbUserInitializeProcessing ( ) { + } + + void SlicerLibToBlackBoxSrc::bbUserFinalizeProcessing ( ) { + } +} + + + diff --git a/bbtk_Slicer_PKG/src/bbSlicerSlicerLibToBlackBoxSrc.h b/bbtk_Slicer_PKG/src/bbSlicerSlicerLibToBlackBoxSrc.h new file mode 100644 index 0000000..1fa3670 --- /dev/null +++ b/bbtk_Slicer_PKG/src/bbSlicerSlicerLibToBlackBoxSrc.h @@ -0,0 +1,69 @@ +#ifndef __bbSlicerSlicerLibToBlackBoxSrc_h_INCLUDED__ +#define __bbSlicerSlicerLibToBlackBoxSrc_h_INCLUDED__ +#include "bbSlicer_EXPORT.h" +#include "bbtkAtomicBlackBox.h" + + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +#include "CreationTool.h" + +namespace bbSlicer { + + class bbSlicer_EXPORT SlicerLibToBlackBoxSrc + : + public bbtk::AtomicBlackBox { + BBTK_BLACK_BOX_INTERFACE ( SlicerLibToBlackBoxSrc , bbtk::AtomicBlackBox ) ; + BBTK_DECLARE_INPUT ( LibFullPath , std::string ) ; + BBTK_DECLARE_INPUT ( SrcDestinationDir , std::string ) ; + BBTK_PROCESS ( Process ) ; + void Process ( ) ; + + /// STD LD_OPEN CALLS /// + char* getModuleDescription ( const std::string lib ) ; + /// FILE RELATED /// + void saveFile ( const std::string content , const std::string file_name ) ; + std::string loadFile ( const std::string filename ) ; + std::string loadDummyBody ( ) ; + std::string loadDummyHeader ( ) ; + /// BBTK BOX CREATION /// + std::string updateBoxDescription ( const ModuleDescription* module , std::string _header ) ; + std::string updateBoxName ( const ModuleDescription* module , std::string content ) ; + std::string getHeaderFileName ( const ModuleDescription* module ) ; + std::string getBodyFileName ( const ModuleDescription* module ) ; + std::string updateBoxInputs ( const ModuleDescription* module , std::string content ) ; + const int getNumberOfArguments ( const ModuleDescription* module ) ; + std::string updateProcessMethod ( const ModuleDescription* module , std::string content , std::string lib ) ; + std::vector < std::string > getFilesByExtention ( std::string dir , std::string ext ) ; + } ; + + BBTK_BEGIN_DESCRIBE_BLACK_BOX ( SlicerLibToBlackBoxSrc , bbtk::AtomicBlackBox ) ; + BBTK_NAME ( "SlicerLibToBlackBoxSrc" ) ; + BBTK_AUTHOR ( "Juan Gabriel RIVEROS REYES" ) ; + BBTK_DESCRIPTION ( "Creates a sigle bbtk box from a single slicers shared library The code generated by this box is intended for later compilation and plug in" ) ; + BBTK_CATEGORY ( "" ) ; + BBTK_INPUT ( SlicerLibToBlackBoxSrc , LibFullPath , "Library full path" , std::string , "" ) ; + BBTK_INPUT ( SlicerLibToBlackBoxSrc , SrcDestinationDir , "Source code destination directory" , std::string , "" ) ; + BBTK_END_DESCRIBE_BLACK_BOX ( SlicerLibToBlackBoxSrc ) ; + +} + +#endif + -- 2.45.0