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