]> Creatis software - creaCLI.git/blob - ModuleCall/main.cpp
Simple DL-Open example
[creaCLI.git] / ModuleCall / main.cpp
1 /* 
2  * File:   main.cpp
3  * Author: gabriel
4  *
5  * Created on 15 fĂ©vrier 2012, 19:50
6  */
7
8 #include <vector>
9 #include <cstdlib>
10 #include <dlfcn.h>
11 #include <sstream>
12 #include <iostream>
13
14
15 #include <ModuleDescriptionParser.h>
16 #include <ModuleParameterGroup.h>
17 #include <ModuleDescription.h>
18 #include <ModuleParameter.h>
19
20 #include <fstream>
21 #include <ModuleDescriptionUtilities.h>
22
23 #include "CreationTool.h"
24
25
26 ///     STD LD_OPEN CALLS       ///
27
28 char* getModuleDescription ( std::string lib ) {
29     void* handle = dlopen( lib.c_str( ), RTLD_NOW | RTLD_GLOBAL );
30     if ( ! handle ) {
31         std::cerr << "CAN'T OPEN LIBRARY: " << dlerror( ) << '\n';
32         return NULL;
33     }
34     typedef char* ( *method_t )( void );
35     // RESET ERRORS
36     dlerror( );
37     method_t myMethod = ( method_t ) dlsym( handle, "GetXMLModuleDescription" );
38     const char *dlsym_error = dlerror( );
39     if ( dlsym_error ) {
40         std::cerr << "CAN'T LOAD SYMBOL 'GetXMLModuleDescription: " << dlsym_error << '\n';
41         dlclose( handle );
42         return NULL;
43     }
44     // CALLING METHOD
45     char* descrption = myMethod( );
46     // CLOSING LIB
47     dlclose( handle );
48     return descrption;
49
50 }
51
52 void execute ( std::string lib, int _argc, char * _argv[] ) {
53     void* handle = dlopen( lib.c_str( ), RTLD_NOW | RTLD_GLOBAL );
54     if ( ! handle ) {
55         std::cerr << "CAN'T OPEN LIBRARY: " << dlerror( ) << '\n';
56         return;
57     }
58     typedef int (*method_t )( int argc, char * argv[] );
59     // RESET ERRORS
60     dlerror( );
61     method_t myMethod = ( method_t ) dlsym( handle, "ModuleEntryPoint" );
62     const char *dlsym_error = dlerror( );
63     if ( dlsym_error ) {
64         std::cerr << "CAN'T LOAD SYMBOL 'ModuleEntryPoint' " << dlsym_error << '\n';
65         dlclose( handle );
66         return;
67     }
68     // CALLING METHOD
69     myMethod( _argc, _argv );
70     // CLOSING LIB
71     dlclose( handle );
72 }
73
74 ///     FILE RELATED     ///
75
76 void saveFile ( std::string content, std::string file_name ) {
77     std::ofstream myfile;
78     myfile.open( file_name.c_str( ) );
79     myfile << content;
80     myfile.close( );
81 }
82
83 std::string loadFile ( std::string filename ) {
84     std::string line = std::string( "" );
85     std::string content = std::string( "" );
86
87     std::ifstream infile;
88     infile.open( filename.c_str( ) );
89     while ( ! infile.eof( ) ) {
90         getline( infile, line );
91         content += line + "\n";
92         line.clear( );
93     }
94     infile.close( );
95     return content;
96
97 }
98
99 std::string loadDummyBody ( ) {
100     return loadFile( "bbSlicerDummy.dummy_cxx" );
101 }
102
103 std::string loadDummyHeader ( ) {
104     return loadFile( "bbSlicerDummy.dummy_h" );
105 }
106
107 ///     BBTK BOX CREATION       /// 
108
109 std::string updateBoxDescription ( const ModuleDescription* module, std::string _header ) {
110     _header = Mthd::Aux::replace_str( _header, "_NNNNN_", Mthd::Aux::replace_str( module->GetTitle( ), " ", "" ) );
111     _header = Mthd::Aux::replace_str( _header, "_AAAAA_", module->GetContributor( ) );
112     _header = Mthd::Aux::replace_str( _header, "_DDDDD_", module->GetDescription( ) );
113     _header = Mthd::Aux::replace_str( _header, "_CCCCC_", module->GetCategory( ) );
114     return _header;
115 }
116
117 std::string updateBoxName ( const ModuleDescription* module, std::string content ) {
118     std::string _new_box_name = Mthd::Aux::replace_str( module->GetTitle( ), " ", "" );
119     content = Mthd::Aux::replace_str( content, "Dummy", _new_box_name );
120     return content;
121 }
122
123 std::string getHeaderFileName ( const ModuleDescription* module ) {
124     std::string name = Mthd::Aux::replace_str( module->GetTitle( ), " ", "" );
125
126     return "bbSlicer" + name + ".h";
127 }
128
129 std::string getBodyFileName ( const ModuleDescription* module ) {
130     std::string name = Mthd::Aux::replace_str( module->GetTitle( ), " ", "" );
131
132     return "bbSlicer" + name + ".cxx";
133 }
134
135 std::string updateBoxInputs ( const ModuleDescription* module, std::string content ) {
136
137     std::string _cxx_inputs = std::string( "\n" );
138     std::string _cxx_inputs_end = std::string( "\n" );
139     std::string _box_name = Mthd::Aux::replace_str( module->GetTitle( ), " ", "" );
140
141     for ( unsigned long int i = 0; i < module->GetParameterGroups( ).size( ); i ++ ) {
142         ModuleParameterGroup pGroup = module->GetParameterGroups( ).at( i );
143         for ( unsigned long int j = 0; j < pGroup.GetParameters( ).size( ); j ++ ) {
144             ModuleParameter mPara = pGroup.GetParameters( ).at( j );
145             _cxx_inputs +=
146                     "BBTK_DECLARE_INPUT ( " +
147                     mPara.GetName( ) + " , " +
148                     mPara.GetCPPType( ) + " );\n";
149
150             _cxx_inputs_end +=
151                     "BBTK_INPUT(" +
152                     _box_name + " , " +
153                     mPara.GetName( ) + " , " +
154                     "\"" + mPara.GetName( ) + "\"" + " , " +
155                     mPara.GetCPPType( ) + ", \"\");\n";
156         }
157     }
158     content = Mthd::Aux::replace_str( content, "_11111_", _cxx_inputs );
159     content = Mthd::Aux::replace_str( content, "_22222_", _cxx_inputs_end );
160
161     return content;
162 }
163
164 const int getNumberOfArguments ( const ModuleDescription* module ) {
165     int number = 0;
166     for ( unsigned long int i = 0; i < module->GetParameterGroups( ).size( ); i ++ ) {
167         ModuleParameterGroup pGroup = module->GetParameterGroups( ).at( i );
168         for ( unsigned long int j = 0; j < pGroup.GetParameters( ).size( ); j ++ ) {
169
170             ModuleParameter mPara = pGroup.GetParameters( ).at( j );
171             number ++;
172         }
173     }
174     return number;
175 }
176
177 std::string updateProcessMethod ( const ModuleDescription* module, std::string content, std::string lib ) {
178     int number = 0;
179     std::string arg_list = std::string( "" );
180     for ( unsigned long int i = 0; i < module->GetParameterGroups( ).size( ); i ++ ) {
181         ModuleParameterGroup pGroup = module->GetParameterGroups( ).at( i );
182         for ( unsigned long int j = 0; j < pGroup.GetParameters( ).size( ); j ++ ) {
183
184             ModuleParameter mPara = pGroup.GetParameters( ).at( j );
185
186             arg_list += "Mthd::Aux::toCharArrray( ";
187             if ( mPara.GetFlag( ) != "" ) {
188                 arg_list += "Mthd::Aux::toString( \"";
189                 arg_list += "-" + mPara.GetFlag( ) + "\" ";
190                 arg_list += " ) + ";
191
192             } else if ( mPara.GetLongFlag( ) != "" ) {
193                 arg_list += "Mthd::Aux::toString( \"";
194                 arg_list += "--" + mPara.GetLongFlag( ) + "\" ";
195                 arg_list += " ) + ";
196             }
197
198             arg_list += "Mthd::Aux::toString( bbGetInput" + mPara.GetName( ) + "( ) )";
199             arg_list += " ),\n";
200             number ++;
201         }
202
203     }
204     arg_list += "_EEENNNDDD_";
205     arg_list = Mthd::Aux::replace_str( arg_list, ",\n_EEENNNDDD_", "" );
206     std::string _argc = "\nint _argc =" + Mthd::Aux::toString( number ) + ";\n";
207     std::string _slib = "std::string lib = \"" + lib + "\";\n";
208     std::string _argv = "char * _argv[ ] = { " + arg_list + " };\n";
209     return Mthd::Aux::replace_str( content, "_33333_", _argc + _slib + _argv );
210 }
211
212 int main ( int argc, char* argv[] ) {
213
214     int _argc = 3;
215     char * _argv[] = { "-s 20", "/home/riveros/Desktop/Experiments/RA1.mhd", "/home/riveros/Desktop/Experiments/RA1-OUT-MAIN.mhd" };
216     std::string gblib = "/home/riveros/.slicer/Slicer4-bin/Slicer-build/lib/Slicer-4.0/cli-modules/libGaussianBlurImageFilterLib.so";
217
218     execute( gblib, _argc, _argv );
219
220     ///     MODULE INFORMATION      ///
221     const std::string des = getModuleDescription( gblib );
222
223     //    std::cout << "====================================" << std::endl << std::endl;
224     //    std::cout << "====================================" << std::endl << std::endl;
225     //    std::cout << "====================================" << std::endl << std::endl;
226     //    std::cout << "====================================" << std::endl << std::endl;
227     //
228     //    std::cout << des << std::endl << std::endl;
229
230     ModuleDescription module;
231     ModuleDescriptionParser parser;
232
233     parser.Parse( des, module );
234
235     ///     LOADING DUMMY FILES     ///
236     std::string _body = loadDummyBody( );
237     std::string _header = loadDummyHeader( );
238
239     ///     SETTING HEADER FILE     ///
240     _header = updateBoxName( &module, _header );
241     _header = updateBoxDescription( &module, _header );
242     _header = updateBoxInputs( &module, _header );
243     saveFile( _header, getHeaderFileName( &module ) );
244
245     ///     SETTING BODY FILE       ///
246     _body = updateBoxName( &module, _body );
247     _body = updateProcessMethod( &module, _body, gblib );
248     saveFile( _body, getBodyFileName( &module ) );
249
250
251     //    std::cout << "====================================" << std::endl << std::endl;
252     //    std::cout << "====================================" << std::endl << std::endl;
253     //    std::cout << "====================================" << std::endl << std::endl;
254     //    std::cout << "====================================" << std::endl << std::endl;
255     //
256     //    std::cout << _header << std::endl;
257     //
258     //    std::cout << "====================================" << std::endl << std::endl;
259     //    std::cout << "====================================" << std::endl << std::endl;
260     //    std::cout << "====================================" << std::endl << std::endl;
261     //    std::cout << "====================================" << std::endl << std::endl;
262     //
263     //    std::cout << _body << std::endl;
264
265     return EXIT_SUCCESS;
266 }