DynareMain1.cc 1.86 KB
Newer Older
1
/*
2
 * Copyright © 2015-2019 Dynare Team
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * This file is part of Dynare.
 *
 * Dynare is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Dynare is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <sstream>
#include <fstream>

#include "macro/MacroDriver.hh"

25 26 27 28
bool compareNewline (int i, int j) {
  return i == '\n' && j == '\n';
}

29
void
30
main1(const string &filename, const string &basename, istream &modfile, bool debug, bool save_macro, string &save_macro_file,
31
      bool no_line_macro, bool no_empty_line_macro, const vector<pair<string, string>> &defines, const vector<string> &path, stringstream &macro_output)
32 33 34 35
{
  // Do macro processing
  MacroDriver m;

36
  m.parse(filename, basename, modfile, macro_output, debug, no_line_macro, defines, path);
37 38 39 40
  if (save_macro)
    {
      if (save_macro_file.empty())
        save_macro_file = basename + "-macroexp.mod";
41
      ofstream macro_output_file(save_macro_file);
42 43 44 45 46
      if (macro_output_file.fail())
        {
          cerr << "Cannot open " << save_macro_file << " for macro output" << endl;
          exit(EXIT_FAILURE);
        }
47 48 49

      string str (macro_output.str());
      if (no_empty_line_macro)
50 51 52 53
        {
          str.erase(0, str.find_first_not_of('\n'));
          str.erase(unique(str.begin(), str.end(), compareNewline), str.end());
        }
54
      macro_output_file << str;
55 56 57
      macro_output_file.close();
    }
}