diff --git a/DynareMain.cc b/DynareMain.cc index 8176d6f9..d37b2314 100644 --- a/DynareMain.cc +++ b/DynareMain.cc @@ -48,9 +48,8 @@ void main2(stringstream &in, string &basename, bool debug, bool clear_all, bool , JsonOutputPointType json, JsonFileOutputType json_output_mode, bool onlyjson, bool jsonderivsimple ); -void main1(char *modfile, string &basename, bool debug, bool save_macro, string &save_macro_file, - bool no_line_macro, - map &defines, vector &path, stringstream ¯o_output); +void main1(string &modfile, string &basename, string &modfiletxt, bool debug, bool save_macro, string &save_macro_file, + bool no_line_macro, map &defines, vector &path, stringstream ¯o_output); void usage() @@ -339,9 +338,34 @@ main(int argc, char **argv) // Construct basename (i.e. remove file extension if there is one) string basename = argv[1]; - size_t pos = basename.find_last_of('.'); - if (pos != string::npos) - basename.erase(pos); + string modfile, modfiletxt; + size_t fsc = basename.find_first_of(';'); + if (fsc != string::npos) + { + // If a semicolon is found in argv[1], treat it as the text of the modfile + modfile = "mod_file_passed_as_string.mod"; + basename = "mod_file_passed_as_string"; + modfiletxt = argv[1]; + } + else + { + // If a semicolon is NOT found in argv[1], treat it as the name of the modfile + modfile = argv[1]; + size_t pos = basename.find_last_of('.'); + if (pos != string::npos) + basename.erase(pos); + + ifstream modfile(argv[1], ios::binary); + if (modfile.fail()) + { + cerr << "ERROR: Could not open file: " << argv[1] << endl; + exit(EXIT_FAILURE); + } + + stringstream buffer; + buffer << modfile.rdbuf(); + modfiletxt = buffer.str(); + } WarningConsolidation warnings(no_warn); @@ -360,7 +384,7 @@ main(int argc, char **argv) // Do macro processing stringstream macro_output; - main1(argv[1], basename, debug, save_macro, save_macro_file, no_line_macro, defines, path, macro_output); + main1(modfile, basename, modfiletxt, debug, save_macro, save_macro_file, no_line_macro, defines, path, macro_output); if (only_macro) return EXIT_SUCCESS; diff --git a/DynareMain1.cc b/DynareMain1.cc index 3cbd33f3..fe87ba83 100644 --- a/DynareMain1.cc +++ b/DynareMain1.cc @@ -23,13 +23,13 @@ #include "macro/MacroDriver.hh" void -main1(char *modfile, string &basename, bool debug, bool save_macro, string &save_macro_file, bool no_line_macro, - map &defines, vector &path, stringstream ¯o_output) +main1(string &modfile, string &basename, string &modfiletxt, bool debug, bool save_macro, string &save_macro_file, + bool no_line_macro, map &defines, vector &path, stringstream ¯o_output) { // Do macro processing MacroDriver m; - m.parse(modfile, macro_output, debug, no_line_macro, defines, path); + m.parse(modfile, modfiletxt, macro_output, debug, no_line_macro, defines, path); if (save_macro) { if (save_macro_file.empty()) diff --git a/macro/MacroDriver.cc b/macro/MacroDriver.cc index 71d53b9c..4d2399ec 100644 --- a/macro/MacroDriver.cc +++ b/macro/MacroDriver.cc @@ -37,18 +37,11 @@ MacroDriver::~MacroDriver() } void -MacroDriver::parse(const string &f, ostream &out, bool debug, bool no_line_macro, +MacroDriver::parse(const string &f, const string &modfiletxt, ostream &out, bool debug, bool no_line_macro, map defines, vector path) { file = f; - ifstream in(f.c_str(), ios::binary); - if (in.fail()) - { - cerr << "ERROR: Could not open file: " << f << endl; - exit(EXIT_FAILURE); - } - /* Copy the file into a stringstream, and add an extra end-of-line. This is a workaround for trac ticket #73: with this workaround, MOD files ending with @@ -66,7 +59,7 @@ MacroDriver::parse(const string &f, ostream &out, bool debug, bool no_line_macro { file_with_endl << "@#define " << it->first << " = \"" << it->second << "\"" << endl; } - file_with_endl << in.rdbuf() << endl; + file_with_endl << modfiletxt << endl; lexer = new MacroFlex(&file_with_endl, &out, no_line_macro, path); lexer->set_debug(debug); diff --git a/macro/MacroDriver.hh b/macro/MacroDriver.hh index 773a99dc..c76947de 100644 --- a/macro/MacroDriver.hh +++ b/macro/MacroDriver.hh @@ -182,7 +182,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, const string &modfiletxt, ostream &out, bool debug, bool no_line_macro, map defines, vector path); //! Name of main file being parsed