]> Creatis software - creaCLI.git/blobdiff - bbtk_Slicer_PKG/src/bbSlicerModuleWrapper.cxx
bbtk-Slicer Module JGRR
[creaCLI.git] / bbtk_Slicer_PKG / src / bbSlicerModuleWrapper.cxx
diff --git a/bbtk_Slicer_PKG/src/bbSlicerModuleWrapper.cxx b/bbtk_Slicer_PKG/src/bbSlicerModuleWrapper.cxx
new file mode 100644 (file)
index 0000000..51cbcf8
--- /dev/null
@@ -0,0 +1,213 @@
+#include "bbSlicerModuleWrapper.h"
+#include "bbSlicerPackage.h"
+
+namespace bbSlicer {
+
+    BBTK_ADD_BLACK_BOX_TO_PACKAGE ( Slicer, ModuleWrapper )
+    BBTK_BLACK_BOX_IMPLEMENTATION ( ModuleWrapper, bbtk::AtomicBlackBox );
+
+    void ModuleWrapper::Process ( ) {
+
+        ///     MODULE INFORMATION      ///
+
+        std::string gblib = bbGetInputPath_to_library( );
+        const 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, getBodyFileName( &module ) );
+    }
+
+
+    ///     STD LD_OPEN CALLS       ///
+
+    char* ModuleWrapper::getModuleDescription ( std::string lib ) {
+        void* handle = dlopen( lib.c_str( ), RTLD_NOW | RTLD_GLOBAL );
+        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 ModuleWrapper::saveFile ( std::string content, std::string file_name ) {
+        std::ofstream myfile;
+        myfile.open( file_name.c_str( ) );
+        myfile << content;
+        myfile.close( );
+    }
+
+    std::string ModuleWrapper::loadFile ( 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 ModuleWrapper::loadDummyBody ( ) {
+        return loadFile( "bbSlicerDummy.dummy_cxx" );
+    }
+
+    std::string ModuleWrapper::loadDummyHeader ( ) {
+        return loadFile( "bbSlicerDummy.dummy_h" );
+    }
+
+    ///     BBTK BOX CREATION       /// 
+
+    std::string ModuleWrapper::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 ModuleWrapper::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 ModuleWrapper::getHeaderFileName ( const ModuleDescription* module ) {
+        std::string name = Mthd::Aux::replace_str( module->GetTitle( ), " ", "" );
+
+        return "bbSlicer" + name + ".h";
+    }
+
+    std::string ModuleWrapper::getBodyFileName ( const ModuleDescription* module ) {
+        std::string name = Mthd::Aux::replace_str( module->GetTitle( ), " ", "" );
+
+        return "bbSlicer" + name + ".cxx";
+    }
+
+    std::string ModuleWrapper::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 ModuleWrapper::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 ModuleWrapper::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 );
+    }
+
+    void ModuleWrapper::bbUserSetDefaultValues ( ) {
+    }
+
+    void ModuleWrapper::bbUserInitializeProcessing ( ) {
+    }
+
+    void ModuleWrapper::bbUserFinalizeProcessing ( ) {
+    }
+}
+// EO namespace bbSlicer
+
+