]> Creatis software - cpPlugins.git/blob - appli/bash/HostCreator.cxx
Cast image filter added. ROI filter modified.
[cpPlugins.git] / appli / bash / HostCreator.cxx
1 #include <iostream>
2 #include <map>
3 #include <sstream>
4 #include <utility>
5 #include <vector>
6 #include <bash/Config.h>
7
8 // -------------------------------------------------------------------------
9 typedef std::pair< std::string, std::string > TPair;
10 typedef std::map< std::string, TPair >        TInfo;
11
12 // -------------------------------------------------------------------------
13 bool is_valid_class( const std::string& str )
14 {
15   return( str.find( "cpPluginsObject" ) != std::string::npos );
16 }
17
18 // -------------------------------------------------------------------------
19 void process_header( TInfo& info, const std::string& file_name )
20 {
21   std::string buffer;
22   if( !( cpPlugins_bash::Read( buffer, file_name ) ) )
23     return;
24
25   auto prev_pos = std::string::npos;
26   prev_pos = 0;
27   auto pos = buffer.find( "cpPluginsObject" );
28   while( pos != std::string::npos )
29   {
30     // Get class names
31     auto op = buffer.find( "(", pos );
32     auto cl = buffer.find( ")", pos );
33     std::vector< std::string > tokens;
34     cpPlugins_bash::Tokenize(
35       tokens,
36       buffer.substr( op + 1, cl - op - 2 ),
37       ",\n "
38       );
39
40     std::string namespace_name = "";
41     auto preamble = buffer.substr( prev_pos, pos );
42     auto napos = preamble.find( "namespace" );
43     while( napos != std::string::npos )
44     {
45       auto enapos = preamble.find( "{", napos + 1 );
46       auto tmp = preamble.substr( napos, enapos - napos + 1 );
47       std::vector< std::string > tokens2;
48       cpPlugins_bash::Tokenize( tokens2, tmp, " \n\t{" );
49       namespace_name = tokens2.back( );
50       napos = preamble.find( "namespace", napos + 1 );
51
52     } // elihw
53
54     auto class_name = tokens[ 0 ];
55     auto superclass_name = tokens[ 1 ];
56     auto category_name = tokens[ 2 ];
57     if( info.find( class_name ) == info.end( ) )
58       info[ class_name ] = TPair( category_name, namespace_name );
59     prev_pos = pos;
60     pos = buffer.find( "cpPluginsObject", pos + 1 );
61
62   } // elihw
63 }
64
65 // -------------------------------------------------------------------------
66 int main( int argc, char* argv[] )
67 {
68   if( argc < 4 )
69   {
70     std::cerr
71       << "Usage: " << argv[ 0 ]
72       << " plugins_name output_file header_file_0.h header_file_0.h ..."
73       << std::endl;
74     return( 1 );
75
76   } // fi
77   std::string plugins_name = argv[ 1 ];
78   std::string output_filename = argv[ 2 ];
79
80   // Parse all header files
81   TInfo info;
82   for( int i = 3; i < argc; ++i )
83     process_header( info, argv[ i ] );
84   if( info.size( ) == 0 )
85   {
86     std::cerr << "ERROR: No valid input headers." << std::endl;
87     return( 1 );
88
89   } // fi
90
91   // Output data
92   std::stringstream out;
93
94   // Include section
95   for( int i = 3; i < argc; ++i )
96     out << "#include \"" << argv[ i ] << "\"" <<std::endl;
97
98   // Prepare prefix
99 #if defined( WIN32 ) || defined( _WIN32 ) || defined( __WIN32__ )
100   std::string export_prefix = "__declspec(dllexport)";
101 #else // defined( WIN32 ) || defined( _WIN32 ) || defined( __WIN32__ )
102   std::string export_prefix = "__attribute__((visibility(\"default\")))";
103 #endif // defined( WIN32 ) || defined( _WIN32 ) || defined( __WIN32__ )
104
105   // Plugins name function
106   out
107     << std::endl
108     << "extern \"C\" " << export_prefix << " const char* "
109     << "cpPlugins_Name( )" << std::endl << "{" << std::endl
110     << "  return( \"" << plugins_name << "\" );" << std::endl
111     << "}" << std::endl << std::endl;
112
113   // Access function
114   out
115     << std::endl
116     << "extern \"C\" " << export_prefix << " const char* "
117     << "cpPlugins_LoadedFilters( )" << std::endl << "{" << std::endl
118     << "  static std::string classes;"
119     << std::endl
120     << "  classes = \"\";" << std::endl;
121   for( auto iIt = info.begin( ); iIt != info.end( ); ++iIt )
122     out
123       << "  classes += std::string( \"" << iIt->second.first
124       << ":" << iIt->first << ";\" );" << std::endl;
125   out
126     << "  return( classes.c_str( ) );" << std::endl
127     << "}" << std::endl << std::endl;
128
129   // Creators
130   for( auto iIt = info.begin( ); iIt != info.end( ); ++iIt )
131   {
132     std::string class_name = iIt->second.second;
133     if( class_name != "" )
134       class_name += std::string( "::" );
135     class_name += iIt->first;
136     
137     out
138       << "extern \"C\" " << export_prefix << " void* "
139       << iIt->second.first << "_" << iIt->first
140       << "( )" << std::endl << "{" << std::endl
141       << "  static " << class_name << "::Pointer f;" << std::endl
142       << "  f = " << class_name << "::New( );" << std::endl
143       << "  return( &f );"
144       << std::endl
145       << "}" << std::endl << std::endl;
146
147   } // rof
148
149   // Real write
150   if( !( cpPlugins_bash::Write( out.str( ), output_filename ) ) )
151   {
152     std::cerr << "ERROR: Could not write file." << std::endl;
153     return( 1 );
154
155   } // fi
156   return( 0 );
157 }
158
159 // eof - $RCSfile$