From c209c1cdbf0709288621a7c121acc8100b2ac046 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 29 Mar 2011 15:53:10 +0200 Subject: [PATCH] implement the possibility of passing macro-processor defines on the command-line (ticket 171) --- DynareMain.cc | 27 +++++++++++++++++++++++++-- macro/MacroDriver.cc | 5 ++++- macro/MacroDriver.hh | 2 +- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/DynareMain.cc b/DynareMain.cc index 5aec0a9b..e40afaad 100644 --- a/DynareMain.cc +++ b/DynareMain.cc @@ -46,7 +46,8 @@ void usage() { cerr << "Dynare usage: dynare mod_file [debug] [noclearall] [savemacro[=macro_file]] [onlymacro] [nolinemacro] [notmpterms] [warn_uninit]" - << " [console] [parallel[=cluster_name]] [conffile=parallel_config_path_and_filename] [parallel_slave_open_mode] [parallel_test]" + << " [console] [parallel[=cluster_name]] [conffile=parallel_config_path_and_filename] [parallel_slave_open_mode] [parallel_test] " + << " [-D[=]]" #if defined(_WIN32) || defined(__CYGWIN32__) << " [cygwin] [msvc]" #endif @@ -81,6 +82,7 @@ main(int argc, char **argv) string cluster_name; bool parallel_slave_open_mode = false; bool parallel_test = false; + map defines; // Parse options for (int arg = 2; arg < argc; arg++) @@ -144,6 +146,27 @@ main(int argc, char **argv) cluster_name = string(argv[arg] + 9); } } + else if (strlen(argv[arg]) >= 2 && !strncmp(argv[arg], "-D", 2)) + { + if (strlen(argv[arg]) == 2) + { + cerr << "Incorrect syntax for command line define: the defined variable " + << "must not be separated from -D by whitespace." << endl; + usage(); + } + + size_t equal_index = string(argv[arg]).find('='); + if (equal_index != string::npos) + { + string key = string(argv[arg]).erase(equal_index).erase(0,2); + defines[key] = string(argv[arg]).erase(0, equal_index+1); + } + else + { + string key = string(argv[arg]).erase(0,2); + defines[key] = "1"; + } + } else { cerr << "Unknown option: " << argv[arg] << endl; @@ -164,7 +187,7 @@ main(int argc, char **argv) MacroDriver m; stringstream macro_output; - m.parse(argv[1], macro_output, debug, no_line_macro); + m.parse(argv[1], macro_output, debug, no_line_macro, defines); if (save_macro) { if (save_macro_file.empty()) diff --git a/macro/MacroDriver.cc b/macro/MacroDriver.cc index dc1a1496..f2f2477e 100644 --- a/macro/MacroDriver.cc +++ b/macro/MacroDriver.cc @@ -36,7 +36,7 @@ MacroDriver::~MacroDriver() } void -MacroDriver::parse(const string &f, ostream &out, bool debug, bool no_line_macro) +MacroDriver::parse(const string &f, ostream &out, bool debug, bool no_line_macro, map defines) { file = f; @@ -53,6 +53,9 @@ MacroDriver::parse(const string &f, ostream &out, bool debug, bool no_line_macro an @#endif or an @#endfor - but no newline - no longer trigger an error. */ stringstream file_with_endl; + for (map::iterator it=defines.begin(); + it!=defines.end(); it++) + file_with_endl << "@#define " << it->first << " = " << it->second << endl; file_with_endl << in.rdbuf() << endl; lexer = new MacroFlex(&file_with_endl, &out, no_line_macro); diff --git a/macro/MacroDriver.hh b/macro/MacroDriver.hh index 345c8e51..92c95dfc 100644 --- a/macro/MacroDriver.hh +++ b/macro/MacroDriver.hh @@ -176,7 +176,7 @@ public: //! Starts parsing a file, returns output in out /*! \param no_line_macro should we omit the @#line statements ? */ - void parse(const string &f, ostream &out, bool debug, bool no_line_macro); + void parse(const string &f, ostream &out, bool debug, bool no_line_macro, map defines); //! Name of main file being parsed string file;