]> Creatis software - cpPlugins.git/blob - appli/bash/CreateDemanglers.cxx
...
[cpPlugins.git] / appli / bash / CreateDemanglers.cxx
1 #include <bash/Config.h>
2
3 #define MAX_NUMBER_OF_INPUTS 9
4
5 // -------------------------------------------------------------------------
6 int main( int argc, char* argv[] )
7 {
8   // Get inputs
9   if( argc < 4 )
10   {
11     std::cerr
12       << "Usage: " << argv[ 0 ]
13       << " definitons_file object_name output_file"
14       << std::endl;
15     return( 1 );
16
17   } // fi
18   std::string definitions_filename = argv[ 1 ];
19   std::string object_name = argv[ 2 ];
20   std::string output_filename = argv[ 3 ];
21
22   // Read inputs
23   std::string definitions_buffer;
24   if( !( cpPlugins_bash::Read( definitions_buffer, definitions_filename ) ) )
25   {
26     std::cerr
27       << argv[ 0 ]
28       <<  ": Error reading definitions file \"" << definitions_filename
29       << "\"" << std::endl;
30     return( 1 );
31
32   } // fi
33
34   // Put it in a line-by-line structure
35   TStrings definitions_lines;
36   cpPlugins_bash::Tokenize( definitions_lines, definitions_buffer, "\n" );
37
38   // Parse input file
39   TCommands commands;
40   cpPlugins_bash::Parse( commands, definitions_lines );
41
42   // Load pre-compiled definitions
43   cpPlugins_bash::LoadDefinitions( commands );
44
45   // Expand definitions
46   TCommands definitions;
47   cpPlugins_bash::ExpandDefinitions( definitions, commands );
48
49   // Expand data
50   std::stringstream data;
51   data
52     << "#ifndef __cpPlugins__Demanglers__" << object_name << "__h__" << std::endl
53     << "#define __cpPlugins__Demanglers__" << object_name << "__h__" << std::endl
54     << std::endl;
55
56   for( auto cIt = commands.begin( ); cIt != commands.end( ); ++cIt )
57   {
58     if( cIt->first == "define" )
59       continue;
60
61     TStrings instances;
62     cpPlugins_bash::Expand( instances, definitions, commands, cIt->first );
63     TStrings toks;
64     cpPlugins_bash::Tokenize( toks, cIt->first, "|" );
65     for( unsigned int nIns = 1; nIns <= MAX_NUMBER_OF_INPUTS; ++nIns )
66     {
67       if( toks.size( ) > 1 )
68       {
69         data
70           << "#define cpPlugins_Demangle_" << object_name << "_"
71           << toks[ 0 ] << "_" << nIns << "( o, f";
72         for( unsigned int i = 1; i < toks.size( ); ++i )
73           data << ", " << toks[ i ];
74       }
75       else
76         data
77           << "#define cpPlugins_Demangle_" << object_name << "_"
78           << toks[ 0 ] << "_" << nIns << "( o, f";
79
80       for( unsigned int i = 1; i < nIns; ++i )
81         data << ", X" << i;
82       data << " ) \\";
83
84       data << std::endl;
85       std::string prefix = "";
86       for( auto iIt = instances.begin( ); iIt != instances.end( ); ++iIt )
87       {
88         data
89           << "  " << prefix
90           << "if( dynamic_cast< " << *iIt << "* >( o ) != NULL ) \\"
91           << std::endl
92           << "    this->f( dynamic_cast< " << *iIt << "* >( o )";
93
94         for( unsigned int i = 1; i < nIns; ++i )
95           data << ", X" << i;
96         data
97           << " ); \\"
98           << std::endl;
99         prefix = "else ";
100
101       } // rof
102       data << "  " << prefix << std::endl << std::endl;
103
104     } // rof
105
106   } // rof
107
108   data
109     << "#endif // __cpPlugins__Demanglers__" << object_name << "__h__"
110     << std::endl;
111   if( !( cpPlugins_bash::Write( data.str( ), output_filename ) ) )
112   {
113     std::cerr << "Error writing file." << std::endl;
114     return( 1 );
115
116   } // fi
117
118   return( 0 );
119 }
120
121 // eof - $RCSfile$