diff --git a/dynare++/integ/src/quadrature-points.cc b/dynare++/integ/src/quadrature-points.cc index dcdb9f687eaec2b0b501876561fed8e0a3fcf2bc..d514348462e695a701e91da54e224ccddc7d5a74 100644 --- a/dynare++/integ/src/quadrature-points.cc +++ b/dynare++/integ/src/quadrature-points.cc @@ -1,7 +1,6 @@ // Copyright (C) 2008-2011, Ondra Kamenik #include "parser/cc/matrix_parser.hh" -#include "utils/cc/memory_file.hh" #include "utils/cc/exception.hh" #include "sylv/cc/GeneralMatrix.hh" #include "sylv/cc/Vector.hh" @@ -16,6 +15,9 @@ #include <cmath> +#include <fstream> +#include <sstream> + struct QuadParams { const char *outname; @@ -112,13 +114,14 @@ main(int argc, char **argv) try { - - // open memory file for vcov - ogu::MemoryFile vcov_mf(params.vcovname); + std::ifstream f{params.vcovname}; + std::ostringstream buffer; + buffer << f.rdbuf(); + std::string contents{buffer.str()}; // parse the vcov matrix ogp::MatrixParser mp; - mp.parse(vcov_mf.length(), vcov_mf.base()); + mp.parse(contents.length(), contents.c_str()); if (mp.nrows() != mp.ncols()) throw ogu::Exception(__FILE__, __LINE__, "VCOV matrix not square"); diff --git a/dynare++/src/dynare3.cc b/dynare++/src/dynare3.cc index 7dfc2d2392efac361afc2612e850d50057bb477f..bb55d2c96257394d6a778861800bd25550e3013c 100644 --- a/dynare++/src/dynare3.cc +++ b/dynare++/src/dynare3.cc @@ -1,9 +1,11 @@ +#include <sstream> +#include <fstream> + #include "dynare3.hh" #include "dynare_exception.hh" #include "planner_builder.hh" #include "forw_subst_builder.hh" -#include "utils/cc/memory_file.hh" #include "utils/cc/exception.hh" #include "parser/cc/parser_exception.hh" #include "parser/cc/atom_substitutions.hh" @@ -43,33 +45,43 @@ Dynare::Dynare(const char *modname, int ord, double sstol, Journal &jr) : journal(jr), model(nullptr), ysteady(nullptr), md(1), dnl(nullptr), denl(nullptr), dsnl(nullptr), fe(nullptr), fde(nullptr), ss_tol(sstol) { - // make memory file - ogu::MemoryFile mf(modname); - if (mf.exists()) + std::ifstream f{modname}; + if (f.fail()) + throw DynareException(__FILE__, __LINE__, string{"Could not open model file "}+modname); + + std::ostringstream buffer; + buffer << f.rdbuf(); + std::string contents{buffer.str()}; + + try { - try - { - model = new ogdyn::DynareParser(mf.base(), mf.length(), ord); - } - catch (const ogp::ParserException &pe) - { - int line; - int col; - mf.line_and_col(pe.offset(), line, col); - throw DynareException(pe.message(), modname, line, col); - } - ysteady = new Vector(model->getAtoms().ny()); - dnl = new DynareNameList(*this); - denl = new DynareExogNameList(*this); - dsnl = new DynareStateNameList(*this, *dnl, *denl); - fe = new ogp::FormulaEvaluator(model->getParser()); - fde = new ogp::FormulaDerEvaluator(model->getParser()); - writeModelInfo(journal); + model = new ogdyn::DynareParser(contents.c_str(), contents.length(), ord); } - else + catch (const ogp::ParserException &pe) { - throw DynareException(__FILE__, __LINE__, string("Could not open model file ")+modname); + // Compute line and column, given the offset in the file + int line = 1; + int col = 0; + size_t i = 0; + while (i < contents.length() && i < (size_t) pe.offset()) + { + if (contents[i] == '\n') + { + line++; + col = 0; + } + i++; + col++; + } + throw DynareException(pe.message(), modname, line, col); } + ysteady = new Vector(model->getAtoms().ny()); + dnl = new DynareNameList(*this); + denl = new DynareExogNameList(*this); + dsnl = new DynareStateNameList(*this, *dnl, *denl); + fe = new ogp::FormulaEvaluator(model->getParser()); + fde = new ogp::FormulaDerEvaluator(model->getParser()); + writeModelInfo(journal); } Dynare::Dynare(const char **endo, int num_endo, diff --git a/dynare++/utils/cc/Makefile.am b/dynare++/utils/cc/Makefile.am index 220db615c8b8cb5aeaec624f3853f256460b0d6c..f484a6ceeeccf9a8389f690fa89c38211875c7da 100644 --- a/dynare++/utils/cc/Makefile.am +++ b/dynare++/utils/cc/Makefile.am @@ -2,7 +2,5 @@ noinst_LIBRARIES = libutils.a libutils_a_SOURCES = \ exception.hh \ - memory_file.cc \ - memory_file.hh \ pascal_triangle.cc \ pascal_triangle.hh diff --git a/dynare++/utils/cc/memory_file.cc b/dynare++/utils/cc/memory_file.cc deleted file mode 100644 index 31f89ab1da26210b54eb22bbae1782c31e73f526..0000000000000000000000000000000000000000 --- a/dynare++/utils/cc/memory_file.cc +++ /dev/null @@ -1,71 +0,0 @@ -// Copyright (C) 2005, Ondra Kamenik - -// $Id: memory_file.cpp 987 2006-10-17 14:39:19Z kamenik $ - -#include "memory_file.hh" - -#include <cstdio> - -using namespace ogu; - -int -ogu::calc_pos_offset(int length, const char *str, int line, int col) -{ - int i = 0; - int il = 1; - int ic = 1; - while (i < length && il <= line && ic <= col) - { - if (str[i] == '\n') - { - il++; - ic = 1; - } - else - { - ic++; - } - } - return i; -} - -void -ogu::calc_pos_line_and_col(int length, const char *str, int offset, - int &line, int &col) -{ - line = 1; - col = 0; - int i = 0; - while (i < length && i < offset) - { - if (str[i] == '\n') - { - line++; - col = 0; - } - i++; - col++; - } -} - -MemoryFile::MemoryFile(const char *fname) - : len(-1), data(nullptr) -{ - FILE *fd = fopen(fname, "rb"); - if (fd) - { - // get the file size - fseek(fd, 0, SEEK_END); - len = ftell(fd); - // allocate space for the file plus ending '\0' character - data = new char[len+1]; - // read file and set data - fseek(fd, 0, SEEK_SET); - int i = 0; - int c; - while (EOF != (c = fgetc(fd))) - data[i++] = (unsigned char) c; - data[len] = '\0'; - fclose(fd); - } -} diff --git a/dynare++/utils/cc/memory_file.hh b/dynare++/utils/cc/memory_file.hh deleted file mode 100644 index 80227fe392103840d81d9c887674562b7bf72507..0000000000000000000000000000000000000000 --- a/dynare++/utils/cc/memory_file.hh +++ /dev/null @@ -1,71 +0,0 @@ -// Copyright (C) 2005, Ondra Kamenik - -// $Id: memory_file.h 762 2006-05-22 13:00:07Z kamenik $ - -#ifndef OGU_MEMORY_FILE -#define OGU_MEMORY_FILE - -namespace ogu -{ - /** This function calculates an offset of a given position in a - * given string. The position is given by the line number and by - * the offset in the line (both starting from 1). */ - int calc_pos_offset(int length, const char *str, int line, int col); - /** This function calculates a line number and column number of a - * character given by the offset in the string. It is inverse to - * calc_pos_offset. */ - void calc_pos_line_and_col(int length, const char *str, int offset, - int &line, int &col); - - /** This class opens a given file and makes its copy in memory and - * appends it with the '\0' character. Since the type of length is - * int, it can store files with size at most 4GB. If the file - * could be opened for reading, data is NULL and length is -1. If - * the file is empty but exists, len is zero and data points to a - * newly allocated memory containing '\0' character at the end. */ - class MemoryFile - { - protected: - int len; - char *data; - public: - MemoryFile(const char *fname); - virtual ~MemoryFile() - { - if (data) - delete [] data; - } - int - length() const - { - return len; - } - const char * - base() const - { - return data; - } - bool - exists() const - { - return len != -1; - } - /** Return the offset of a character in the given line - * (starting from 1) with the given offset in the line. */ - int - offset(int line, int lineoff) const - { - return calc_pos_offset(len, data, line, lineoff); - } - /** Return the line number and column number of the character - * defined by the offset. */ - void - line_and_col(int offset, int &line, int &col) const - { - calc_pos_line_and_col(len, data, offset, line, col); - } - }; - -}; - -#endif