6 #include <bash/Config.h>
8 // -------------------------------------------------------------------------
9 typedef std::pair< std::string, std::string > TPair;
10 typedef std::map< std::string, TPair > TInfo;
12 // -------------------------------------------------------------------------
13 bool is_valid_class( const std::string& str )
15 return( str.find( "cpPluginsObject" ) != std::string::npos );
18 // -------------------------------------------------------------------------
19 void process_header( TInfo& info, const std::string& file_name )
22 if( !( cpPlugins_bash::Read( buffer, file_name ) ) )
25 auto prev_pos = std::string::npos;
27 auto pos = buffer.find( "cpPluginsObject" );
28 while( pos != std::string::npos )
31 auto op = buffer.find( "(", pos );
32 auto cl = buffer.find( ")", pos );
33 std::vector< std::string > tokens;
34 cpPlugins_bash::Tokenize(
36 buffer.substr( op + 1, cl - op - 2 ),
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 )
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 );
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 );
60 pos = buffer.find( "cpPluginsObject", pos + 1 );
65 // -------------------------------------------------------------------------
66 int main( int argc, char* argv[] )
71 << "Usage: " << argv[ 0 ]
72 << " plugins_name output_file header_file_0.h header_file_0.h ..."
77 std::string plugins_name = argv[ 1 ];
78 std::string output_filename = argv[ 2 ];
80 // Parse all header files
82 for( int i = 3; i < argc; ++i )
83 process_header( info, argv[ i ] );
84 if( info.size( ) == 0 )
86 std::cerr << "ERROR: No valid input headers." << std::endl;
92 std::stringstream out;
95 for( int i = 3; i < argc; ++i )
96 out << "#include \"" << argv[ i ] << "\"" <<std::endl;
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__ )
105 // Plugins name function
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;
116 << "extern \"C\" " << export_prefix << " const char* "
117 << "cpPlugins_LoadedFilters( )" << std::endl << "{" << std::endl
118 << " static std::string classes;"
120 << " classes = \"\";" << std::endl;
121 for( auto iIt = info.begin( ); iIt != info.end( ); ++iIt )
123 << " classes += std::string( \"" << iIt->second.first
124 << ":" << iIt->first << ";\" );" << std::endl;
126 << " return( classes.c_str( ) );" << std::endl
127 << "}" << std::endl << std::endl;
130 for( auto iIt = info.begin( ); iIt != info.end( ); ++iIt )
132 std::string class_name = iIt->second.second;
133 if( class_name != "" )
134 class_name += std::string( "::" );
135 class_name += iIt->first;
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
145 << "}" << std::endl << std::endl;
150 if( !( cpPlugins_bash::Write( out.str( ), output_filename ) ) )
152 std::cerr << "ERROR: Could not write file." << std::endl;