1 #include <cpPlugins_Config.h>
6 // -------------------------------------------------------------------------
7 typedef std::vector< std::string > TLines;
8 typedef std::map< char, TLines > TParsedLines;
9 typedef std::map< std::string, TLines > TVariables;
11 // -------------------------------------------------------------------------
12 bool ReadFile( TParsedLines& lines, const std::string& fname );
13 void ExpandGroups( TLines& res, const TLines& lines );
14 void ExpandDefinitions(
15 TLines& res, const TLines& lines, const TVariables& vars
18 const std::string& prefix, const std::string& suffix,
19 const TLines& lines, std::ostream& out
22 // -------------------------------------------------------------------------
23 int main( int argc, char* argv[] )
28 << "Usage: " << argv[ 0 ]
29 << " input_definitions library_name header_file source_file"
34 std::string input_definitions_fname = argv[ 1 ];
35 std::string library_name = argv[ 2 ];
36 std::string header_file_fname = argv[ 3 ];
37 std::string source_file_fname = argv[ 4 ];
39 // Read file and simple parse it
41 if( !ReadFile( lines, input_definitions_fname ) )
44 << "Error opening file: \""
45 << input_definitions_fname << "\""
53 for( auto dIt = lines[ 'd' ].begin( ); dIt != lines[ 'd' ].end( ); ++dIt )
56 cpPlugins::TokenizeString( tokens, *dIt, "=;" );
57 auto tIt = tokens.begin( );
60 for( ; tIt != tokens.end( ); ++tIt )
61 vars[ vName ].push_back( *tIt );
64 ExpandDefinitions( res, vars[ vName ], vars );
70 TLines f_includes_groups, includes_groups, templates_groups, classes_groups;
71 ExpandGroups( f_includes_groups, lines[ 'f' ] );
72 ExpandGroups( includes_groups, lines[ 'i' ] );
73 ExpandGroups( templates_groups, lines[ 't' ] );
74 ExpandGroups( classes_groups, lines[ 'c' ] );
77 TLines f_includes_list, includes_list, templates_list, classes_list;
78 ExpandDefinitions( f_includes_list, f_includes_groups, vars );
79 ExpandDefinitions( includes_list, includes_groups, vars );
80 ExpandDefinitions( templates_list, templates_groups, vars );
81 ExpandDefinitions( classes_list, classes_groups, vars );
84 std::ofstream header_file( header_file_fname.c_str( ) );
88 << "Error opening \"" << header_file_fname
89 << "\" for writing." << std::endl;
96 << "#ifndef __" << library_name << "__H__" << std::endl
97 << "#define __" << library_name << "__H__" << std::endl<< std::endl
98 << "#include <cpPlugins_Config.h>" << std::endl
99 << "#include <" << library_name << "_Export.h>" << std::endl << std::endl;
100 PrintLines( "", "", lines[ 'b' ], header_file );
101 header_file << std::endl;
102 PrintLines( "#include <", ">", f_includes_list, header_file );
104 << "#ifdef " << library_name << "_EXPORTS" << std::endl
105 << "# define " << library_name << "_PREFIX template class "
106 << library_name << "_EXPORT" << std::endl
107 << "#else // " << library_name << "_EXPORTS" << std::endl
108 << "# define " << library_name
109 << "_PREFIX extern template class" << std::endl
111 << library_name << "_EXPORTS" << std::endl << std::endl;
112 PrintLines( "#include <", ">", includes_list, header_file );
113 PrintLines( "#include <", ">", templates_list, header_file );
115 << std::endl << "#ifdef " << library_name << "_EXPORTS" << std::endl;
116 PrintLines( "#include <", "xx>", templates_list, header_file );
118 << "#endif // " << library_name << "_EXPORTS" << std::endl << std::endl;
120 library_name + std::string( "_PREFIX " ), ";", classes_list, header_file
123 << std::endl << "#endif // __" << library_name << "__H__" << std::endl;
124 header_file.close( );
127 std::ofstream source_file( source_file_fname );
131 << "Error opening \"" << header_file_fname << "\" for writing." << std::endl;
136 << "#include \"" << header_file_fname << "\"" << std::endl;
137 source_file.close( );
142 // -------------------------------------------------------------------------
143 bool ReadFile( TParsedLines& lines, const std::string& fname )
146 if( cpPlugins::ReadFileIntoString( buffer, fname ) )
148 std::istringstream input_stream( buffer );
152 while( std::getline( input_stream, line ) )
154 auto cmd_pos = line.end( );
155 auto arg_pos = line.end( );
156 auto lIt = line.begin( );
157 while( lIt != line.end( ) )
159 if( !cpPlugins::IsBlank( *lIt ) )
161 if( cmd_pos == line.end( ) )
166 else if( arg_pos == line.end( ) )
179 arg.resize( line.end( ) - arg_pos );
180 std::copy( arg_pos, line.end( ), arg.begin( ) );
181 lines[ cmd ].push_back( arg );
190 // -------------------------------------------------------------------------
191 void ExpandGroups( TLines& res, const TLines& lines )
193 for( auto lIt = lines.begin( ); lIt != lines.end( ); ++lIt )
195 auto b_pos = lIt->find( "@{" );
196 if( b_pos != std::string::npos )
198 unsigned int braces_count = 1;
201 while( braces_count != 0 && e_pos < lIt->size( ) )
203 auto v = ( *lIt )[ e_pos ];
204 braces_count += ( v == '{' )? 1: ( ( v == '}' )? -1: 0 );
208 if( braces_count == 0 )
210 auto replace = lIt->substr( b_pos, e_pos - b_pos );
211 auto expansion = replace.substr( 2, replace.size( ) - 3 );
213 cpPlugins::TokenizeString( tokens, expansion, ";" );
214 for( auto tIt = tokens.begin( ); tIt != tokens.end( ); ++tIt )
215 *tIt = cpPlugins::ReplaceString( *lIt, replace, *tIt );
216 ExpandGroups( res, tokens );
221 res.push_back( *lIt );
226 // -------------------------------------------------------------------------
227 void ExpandDefinitions(
228 TLines& res, const TLines& lines, const TVariables& vars
231 std::string seps = " ,;:{}[]()\"$&<>*.";
233 for( auto lIt = lines.begin( ); lIt != lines.end( ); ++lIt )
235 auto b_pos = lIt->find( "#" );
236 if( b_pos != std::string::npos )
239 cpPlugins::TokenizeString( tokens, lIt->substr( b_pos ), seps );
240 std::string cmd = tokens[ 0 ];
241 auto vIt = vars.find( cmd );
242 if( vIt != vars.end( ) )
244 if( vIt->second.size( ) > 0 )
248 auto wIt = vIt->second.begin( ); wIt != vIt->second.end( ); ++wIt
250 new_res.push_back( cpPlugins::ReplaceString( *lIt, cmd, *wIt ) );
251 ExpandDefinitions( res, new_res, vars );
259 cpPlugins::ReplaceString(
260 cpPlugins::ReplaceString( *lIt, "{", "" ), "}", ""
267 // -------------------------------------------------------------------------
269 const std::string& prefix, const std::string& suffix,
270 const TLines& lines, std::ostream& out
273 for( auto i = lines.begin( ); i != lines.end( ); ++i )
274 out << prefix << *i << suffix << std::endl;