Use std::filesystem::path in more places

master
Sébastien Villemot 2023-01-05 16:40:04 +01:00
parent d36eb82b7c
commit 253fbbe7d6
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
16 changed files with 89 additions and 86 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2022 Dynare Team
* Copyright © 2022-2023 Dynare Team
*
* This file is part of Dynare.
*
@ -23,12 +23,12 @@
#include "Bytecode.hh"
BytecodeWriter::BytecodeWriter(const string &filename)
BytecodeWriter::BytecodeWriter(const filesystem::path &filename)
{
open(filename, ios::out | ios::binary);
if (!is_open())
{
cerr << R"(Error : Can't open file ")" << filename << R"(" for writing)" << endl;
cerr << R"(Error : Can't open file ")" << filename.string() << R"(" for writing)" << endl;
exit(EXIT_FAILURE);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2007-2022 Dynare Team
* Copyright © 2007-2023 Dynare Team
*
* This file is part of Dynare.
*
@ -24,6 +24,7 @@
#include <vector>
#include <utility>
#include <ios>
#include <filesystem>
#include "CommonEnums.hh"
@ -1111,7 +1112,7 @@ private:
// Stores the positions of all instructions in the byte stream
vector<pos_type> instructions_positions;
public:
BytecodeWriter(const string &filename);
BytecodeWriter(const filesystem::path &filename);
// Returns the number of the next instruction to be written
int
getInstructionCounter() const

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2003-2022 Dynare Team
* Copyright © 2003-2023 Dynare Team
*
* This file is part of Dynare.
*
@ -5357,11 +5357,11 @@ OccbinConstraintsStatement::writeOutput(ostream &output, const string &basename,
<< "options_.occbin = occbin.set_default_options(options_.occbin, M_);" << endl
<< "oo_.dr=set_state_space(oo_.dr,M_,options_);" << endl;
string filename = "+" + basename + "/occbin_difference.m";
filesystem::path filename {"+" + basename + "/occbin_difference.m"};
ofstream diff_output{filename, ios::out | ios::binary};
if (!diff_output.is_open())
{
cerr << "Error: Can't open file " << filename << " for writing" << endl;
cerr << "Error: Can't open file " << filename.string() << " for writing" << endl;
exit(EXIT_FAILURE);
}
diff_output << "function [binding, relax, err] = occbin_difference(zdatalinear, params, steady_state)" << endl;

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2010-2022 Dynare Team
* Copyright © 2010-2023 Dynare Team
*
* This file is part of Dynare.
*
@ -21,7 +21,6 @@
#include <fstream>
#include <utility>
#include <vector>
#include <filesystem>
#include "ConfigFile.hh"
@ -108,17 +107,20 @@ ConfigFile::ConfigFile(bool parallel_arg, bool parallel_test_arg,
}
void
ConfigFile::getConfigFileInfo(const string &config_file)
ConfigFile::getConfigFileInfo(const filesystem::path &config_file)
{
using namespace boost;
ifstream configFile;
if (config_file.empty())
{
string defaultConfigFile;
filesystem::path defaultConfigFile;
// Test OS and try to open default file
#if defined(_WIN32) || defined(__CYGWIN32__)
if (getenv("APPDATA") == nullptr)
if (auto appdata = getenv("APPDATA");
appdata)
defaultConfigFile = filesystem::path{appdata} / "dynare.ini";
else
{
if (parallel || parallel_test)
cerr << "ERROR: ";
@ -129,13 +131,11 @@ ConfigFile::getConfigFileInfo(const string &config_file)
if (parallel || parallel_test)
exit(EXIT_FAILURE);
}
else
{
defaultConfigFile += getenv("APPDATA");
defaultConfigFile += "\\dynare.ini";
}
#else
if (getenv("HOME") == nullptr)
if (auto home = getenv("HOME");
home)
defaultConfigFile = filesystem::path{home} / ".dynare";
else
{
if (parallel || parallel_test)
cerr << "ERROR: ";
@ -145,17 +145,12 @@ ConfigFile::getConfigFileInfo(const string &config_file)
if (parallel || parallel_test)
exit(EXIT_FAILURE);
}
else
{
defaultConfigFile += getenv("HOME");
defaultConfigFile += "/.dynare";
}
#endif
configFile.open(defaultConfigFile, fstream::in);
if (!configFile.is_open())
if (parallel || parallel_test)
{
cerr << "ERROR: Could not open the default config file (" << defaultConfigFile << ")" << endl;
cerr << "ERROR: Could not open the default config file (" << defaultConfigFile.string() << ")" << endl;
exit(EXIT_FAILURE);
}
else
@ -166,7 +161,7 @@ ConfigFile::getConfigFileInfo(const string &config_file)
configFile.open(config_file, fstream::in);
if (!configFile.is_open())
{
cerr << "ERROR: Couldn't open file " << config_file << endl;;
cerr << "ERROR: Couldn't open file " << config_file.string() << endl;;
exit(EXIT_FAILURE);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2010-2022 Dynare Team
* Copyright © 2010-2023 Dynare Team
*
* This file is part of Dynare.
*
@ -22,6 +22,7 @@
#include <map>
#include <vector>
#include <filesystem>
#include "WarningConsolidation.hh"
@ -119,7 +120,7 @@ private:
const string &operatingSystem);
public:
//! Parse config file
void getConfigFileInfo(const string &parallel_config_file);
void getConfigFileInfo(const filesystem::path &parallel_config_file);
//! Check Pass
void checkPass(WarningConsolidation &warnings) const;
//! Check Pass

View File

@ -621,11 +621,11 @@ DynamicModel::writeDynamicBlockBytecode(const string &basename) const
{
BytecodeWriter code_file {basename + "/model/bytecode/dynamic.cod"};
const string bin_filename {basename + "/model/bytecode/dynamic.bin"};
const filesystem::path bin_filename {basename + "/model/bytecode/dynamic.bin"};
ofstream bin_file {bin_filename, ios::out | ios::binary};
if (!bin_file.is_open())
{
cerr << R"(Error : Can't open file ")" << bin_filename << R"(" for writing)" << endl;
cerr << R"(Error : Can't open file ")" << bin_filename.string() << R"(" for writing)" << endl;
exit(EXIT_FAILURE);
}
@ -792,12 +792,12 @@ DynamicModel::writeDynamicBlockMFile(const string &basename) const
void
DynamicModel::writeDynamicBlockCFile(const string &basename, vector<filesystem::path> per_block_object_files, const string &mexext, const filesystem::path &matlabroot, const filesystem::path &dynareroot) const
{
string filename = basename + "/model/src/dynamic.c";
const filesystem::path filename {basename + "/model/src/dynamic.c"};
ofstream output{filename, ios::out | ios::binary};
if (!output.is_open())
{
cerr << "Error: Can't open file " << filename << " for writing" << endl;
cerr << "Error: Can't open file " << filename.string() << " for writing" << endl;
exit(EXIT_FAILURE);
}
@ -1063,7 +1063,8 @@ DynamicModel::writeDynamicJacobianNonZeroEltsFile(const string &basename) const
sort(nzij_current.begin(), nzij_current.end());
sort(nzij_fwrd.begin(), nzij_fwrd.end());
ofstream output{packageDir(basename) / "dynamic_g1_nz.m", ios::out | ios::binary};
const filesystem::path filename {packageDir(basename) / "dynamic_g1_nz.m"};
ofstream output{filename, ios::out | ios::binary};
output << "function [nzij_pred, nzij_current, nzij_fwrd] = dynamic_g1_nz()" << endl
<< "% Returns the coordinates of non-zero elements in the Jacobian, in column-major order, for each lead/lag (only for endogenous)" << endl;
auto print_nzij = [&output](const vector<pair<int, int>> &nzij, const string &name) {
@ -1497,7 +1498,7 @@ DynamicModel::writeBlockDriverOutput(ostream &output, const string &basename,
if (estimation_present)
{
filesystem::create_directories(basename + "/model/bytecode");
string main_name = basename + "/model/bytecode/kfi";
const filesystem::path main_name {basename + "/model/bytecode/kfi"};
ofstream KF_index_file{main_name, ios::out | ios::binary | ios::ate};
int n_obs = symbol_table.observedVariablesNbr();
int n_state = state_var.size();

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2003-2022 Dynare Team
* Copyright © 2003-2023 Dynare Team
*
* This file is part of Dynare.
*
@ -25,6 +25,7 @@
#include <regex>
#include <thread>
#include <algorithm>
#include <filesystem>
#include <cstdlib>
@ -42,8 +43,8 @@
Function can be found in: MacroExpandModFile.cc
*/
stringstream
macroExpandModFile(const string &filename, const string &basename, const istream &modfile,
bool debug, bool save_macro, string save_macro_file, bool line_macro,
macroExpandModFile(const filesystem::path &filename, const istream &modfile,
bool debug, bool save_macro, filesystem::path save_macro_file, bool line_macro,
const vector<pair<string, string>> &defines,
vector<filesystem::path> paths);
@ -107,11 +108,11 @@ main(int argc, char **argv)
usage();
}
string filename = argv[1];
const filesystem::path filename {argv[1]};
ifstream modfile(filename, ios::binary);
if (modfile.fail())
{
cerr << "ERROR: Could not open file: " << argv[1] << endl;
cerr << "ERROR: Could not open file: " << filename.string() << endl;
exit(EXIT_FAILURE);
}
@ -125,7 +126,7 @@ main(int argc, char **argv)
bool clear_all = true;
bool clear_global = false;
bool save_macro = false;
string save_macro_file;
filesystem::path save_macro_file;
bool debug = false;
bool no_tmp_terms = false;
bool only_macro = false;
@ -136,7 +137,7 @@ main(int argc, char **argv)
bool console = false;
bool nograph = false;
bool nointeractive = false;
string parallel_config_file;
filesystem::path parallel_config_file;
bool parallel = false;
string cluster_name;
bool parallel_follower_open_mode = false; // Must be the same default as in matlab/default_option_values.m
@ -438,10 +439,10 @@ main(int argc, char **argv)
dynareroot = dynareroot.parent_path();
// Construct basename (i.e. remove file extension if there is one)
string basename = argv[1];
if (size_t pos = basename.find_last_of('.');
pos != string::npos)
basename.erase(pos);
/* Calling `string()` method on filename because of bug in GCC/MinGW 10.2
(shipped in Debian Bullseye 11), that fails to accept implicit
conversion to string from filename::path. */
const string basename {filename.stem().string()};
// Forbid some basenames, since they will cause trouble (see preprocessor#62)
set<string> forbidden_basenames = { "T", "y", "x", "params", "steady_state", "it_", "true" };
@ -469,8 +470,7 @@ main(int argc, char **argv)
* Macro-expand MOD file
*/
stringstream macro_output =
macroExpandModFile(filename, basename, modfile, debug, save_macro,
move(save_macro_file), line_macro,
macroExpandModFile(filename, modfile, debug, save_macro, move(save_macro_file), line_macro,
defines, move(paths));
if (only_macro)

View File

@ -26,8 +26,8 @@
#include "macro/Driver.hh"
stringstream
macroExpandModFile(const string &filename, const string &basename, const istream &modfile,
bool debug, bool save_macro, string save_macro_file, bool line_macro,
macroExpandModFile(const filesystem::path &filename, const istream &modfile,
bool debug, bool save_macro, filesystem::path save_macro_file, bool line_macro,
const vector<pair<string, string>> &defines,
vector<filesystem::path> paths)
{
@ -35,15 +35,18 @@ macroExpandModFile(const string &filename, const string &basename, const istream
stringstream macro_output;
macro::Environment env = macro::Environment();
macro::Driver m;
m.parse(filename, modfile, debug, defines, env, paths, macro_output);
/* Calling `string()` method on filename because of bug in GCC/MinGW 10.2
(shipped in Debian Bullseye 11), that fails to accept implicit
conversion to string from filename::path. */
m.parse(filename.string(), modfile, debug, defines, env, paths, macro_output);
if (save_macro)
{
if (save_macro_file.empty())
save_macro_file = basename + "-macroexp.mod";
save_macro_file = filename.stem().string() + "-macroexp.mod";
ofstream macro_output_file{save_macro_file};
if (macro_output_file.fail())
{
cerr << "Cannot open " << save_macro_file << " for macro output" << endl;
cerr << "Cannot open " << save_macro_file.string() << " for macro output" << endl;
exit(EXIT_FAILURE);
}

View File

@ -1257,11 +1257,11 @@ ModFile::writeJsonOutputParsingCheck(const string &basename, JsonFileOutputType
}
filesystem::create_directories(basename + "/model/json");
string fname{basename + "/model/json/modfile.json"};
const filesystem::path fname {basename + "/model/json/modfile.json"};
ofstream jsonOutputFile{fname, ios::out | ios::binary};
if (!jsonOutputFile.is_open())
{
cerr << "ERROR: Can't open file " << fname << " for writing" << endl;
cerr << "ERROR: Can't open file " << fname.string() << " for writing" << endl;
exit(EXIT_FAILURE);
}
@ -1272,11 +1272,11 @@ ModFile::writeJsonOutputParsingCheck(const string &basename, JsonFileOutputType
{
if (basename.size())
{
string fname{basename + "/model/json/modfile-original.json"};
const filesystem::path fname {basename + "/model/json/modfile-original.json"};
jsonOutputFile.open(fname, ios::out | ios::binary);
if (!jsonOutputFile.is_open())
{
cerr << "ERROR: Can't open file " << fname << " for writing" << endl;
cerr << "ERROR: Can't open file " << fname.string() << " for writing" << endl;
exit(EXIT_FAILURE);
}
}
@ -1293,11 +1293,11 @@ ModFile::writeJsonOutputParsingCheck(const string &basename, JsonFileOutputType
{
if (basename.size())
{
string fname{basename + "/model/json/steady_state_model.json"};
const filesystem::path fname {basename + "/model/json/steady_state_model.json"};
jsonOutputFile.open(fname, ios::out | ios::binary);
if (!jsonOutputFile.is_open())
{
cerr << "ERROR: Can't open file " << fname << " for writing" << endl;
cerr << "ERROR: Can't open file " << fname.string() << " for writing" << endl;
exit(EXIT_FAILURE);
}
}
@ -1368,12 +1368,12 @@ ModFile::writeJsonComputingPassOutput(const string &basename, JsonFileOutputType
}
void
ModFile::writeJsonFileHelper(const string &fname, ostringstream &output) const
ModFile::writeJsonFileHelper(const filesystem::path &fname, ostringstream &output) const
{
ofstream jsonOutput{fname, ios::out | ios::binary};
if (!jsonOutput.is_open())
{
cerr << "ERROR: Can't open file " << fname << " for writing" << endl;
cerr << "ERROR: Can't open file " << fname.string() << " for writing" << endl;
exit(EXIT_FAILURE);
}
jsonOutput << output.str();

View File

@ -24,6 +24,7 @@
#include <ctime>
#include <iostream>
#include <sstream>
#include <filesystem>
#include "SymbolTable.hh"
#include "NumericalConstants.hh"
@ -125,7 +126,7 @@ private:
//! Functions used in writing of JSON outut. See writeJsonOutput
void writeJsonOutputParsingCheck(const string &basename, JsonFileOutputType json_output_mode, bool transformpass, bool computingpass) const;
void writeJsonComputingPassOutput(const string &basename, JsonFileOutputType json_output_mode, bool jsonderivsimple) const;
void writeJsonFileHelper(const string &fname, ostringstream &output) const;
void writeJsonFileHelper(const filesystem::path &fname, ostringstream &output) const;
/* Generate a random temporary path, in the current directory. Equivalent to
boost::filesystem::unique_path(). Both are insecure, but currently there
is no better portable solution. Maybe in a later C++ standard? */

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2010-2022 Dynare Team
* Copyright © 2010-2023 Dynare Team
*
* This file is part of Dynare.
*
@ -163,20 +163,20 @@ SteadyStateModel::writeLatexSteadyStateFile(const string &basename) const
{
filesystem::create_directories(basename + "/latex");
string filename = basename + "/latex/steady_state.tex";
string content_filename = basename + "/latex/steady_state_content.tex";
const filesystem::path filename {basename + "/latex/steady_state.tex"},
content_filename {basename + "/latex/steady_state_content.tex"};
ofstream output{filename, ios::out | ios::binary};
if (!output.is_open())
{
cerr << "ERROR: Can't open file " << filename << " for writing" << endl;
cerr << "ERROR: Can't open file " << filename.string() << " for writing" << endl;
exit(EXIT_FAILURE);
}
ofstream content_output{content_filename, ios::out | ios::binary};
if (!content_output.is_open())
{
cerr << "ERROR: Can't open file " << content_filename << " for writing" << endl;
cerr << "ERROR: Can't open file " << content_filename.string() << " for writing" << endl;
exit(EXIT_FAILURE);
}

View File

@ -1267,12 +1267,12 @@ ModelTree::writeJsonModelLocalVariables(ostream &output, bool write_tef_terms, d
}
int
ModelTree::writeBytecodeBinFile(const string &filename, bool is_two_boundaries) const
ModelTree::writeBytecodeBinFile(const filesystem::path &filename, bool is_two_boundaries) const
{
ofstream SaveCode { filename, ios::out | ios::binary };
if (!SaveCode.is_open())
{
cerr << R"(Error : Can't open file ")" << filename << R"(" for writing)" << endl;
cerr << R"(Error : Can't open file ")" << filename.string() << R"(" for writing)" << endl;
exit(EXIT_FAILURE);
}
int u_count {0};
@ -1349,19 +1349,19 @@ ModelTree::writeLatexModelFile(const string &mod_basename, const string &latex_b
{
filesystem::create_directories(mod_basename + "/latex");
string filename = mod_basename + "/latex/" + latex_basename + ".tex";
string content_filename = mod_basename + "/latex/" + latex_basename + "_content" + ".tex";
const filesystem::path filename {mod_basename + "/latex/" + latex_basename + ".tex"},
content_filename {mod_basename + "/latex/" + latex_basename + "_content" + ".tex"};
ofstream output{filename, ios::out | ios::binary};
if (!output.is_open())
{
cerr << "ERROR: Can't open file " << filename << " for writing" << endl;
cerr << "ERROR: Can't open file " << filename.string() << " for writing" << endl;
exit(EXIT_FAILURE);
}
ofstream content_output{content_filename, ios::out | ios::binary};
if (!content_output.is_open())
{
cerr << "ERROR: Can't open file " << content_filename << " for writing" << endl;
cerr << "ERROR: Can't open file " << content_filename.string() << " for writing" << endl;
exit(EXIT_FAILURE);
}

View File

@ -305,7 +305,7 @@ protected:
/* Adds information for (non-block) bytecode simulation in a separate .bin
file.
Returns the number of first derivatives w.r.t. endogenous variables */
int writeBytecodeBinFile(const string &filename, bool is_two_boundaries) const;
int writeBytecodeBinFile(const filesystem::path &filename, bool is_two_boundaries) const;
//! Adds per-block information for bytecode simulation in a separate .bin file
int writeBlockBytecodeBinFile(ofstream &bin_file, int block) const;

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2003-2022 Dynare Team
* Copyright © 2003-2023 Dynare Team
*
* This file is part of Dynare.
*
@ -611,18 +611,18 @@ SaveParamsAndSteadyStateStatement::writeJsonOutput(ostream &output) const
<< "}";
}
LoadParamsAndSteadyStateStatement::LoadParamsAndSteadyStateStatement(const string &filename,
LoadParamsAndSteadyStateStatement::LoadParamsAndSteadyStateStatement(const filesystem::path &filename,
const SymbolTable &symbol_table_arg,
WarningConsolidation &warnings) :
symbol_table{symbol_table_arg}
{
cout << "Reading " << filename << "." << endl;
cout << "Reading " << filename.string() << "." << endl;
ifstream f;
f.open(filename, ios::in);
if (f.fail())
{
cerr << "ERROR: Can't open " << filename << endl;
cerr << "ERROR: Can't open " << filename.string() << endl;
exit(EXIT_FAILURE);
}
@ -640,7 +640,7 @@ LoadParamsAndSteadyStateStatement::LoadParamsAndSteadyStateStatement(const strin
}
catch (SymbolTable::UnknownSymbolNameException &e)
{
warnings << "WARNING: Unknown symbol " << symb_name << " in " << filename << endl;
warnings << "WARNING: Unknown symbol " << symb_name << " in " << filename.string() << endl;
}
}
f.close();

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2003-2022 Dynare Team
* Copyright © 2003-2023 Dynare Team
*
* This file is part of Dynare.
*
@ -23,6 +23,7 @@
#include <string>
#include <vector>
#include <map>
#include <filesystem>
#include "SymbolTable.hh"
#include "ExprNode.hh"
@ -198,7 +199,7 @@ private:
/*! Maps symbol ID to numeric value (stored as string) */
map<int, string> content;
public:
LoadParamsAndSteadyStateStatement(const string &filename,
LoadParamsAndSteadyStateStatement(const filesystem::path &filename,
const SymbolTable &symbol_table_arg,
WarningConsolidation &warnings);
void writeOutput(ostream &output, const string &basename, bool minimal_workspace) const override;

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2003-2022 Dynare Team
* Copyright © 2003-2023 Dynare Team
*
* This file is part of Dynare.
*
@ -240,7 +240,7 @@ StaticModel::writeStaticPerBlockCFiles(const string &basename, const string &mex
ofstream header_output{filename, ios::out | ios::binary};
if (!header_output.is_open())
{
cerr << "ERROR: Can't open file " << filename << " for writing" << endl;
cerr << "ERROR: Can't open file " << filename.string() << " for writing" << endl;
exit(EXIT_FAILURE);
}
header_output << header.str() << ';' << endl;
@ -285,11 +285,11 @@ StaticModel::writeStaticBlockBytecode(const string &basename) const
{
BytecodeWriter code_file {basename + "/model/bytecode/static.cod"};
const string bin_filename {basename + "/model/bytecode/static.bin"};
const filesystem::path bin_filename {basename + "/model/bytecode/static.bin"};
ofstream bin_file {bin_filename, ios::out | ios::binary};
if (!bin_file.is_open())
{
cerr << R"(Error : Can't open file ")" << bin_filename << R"(" for writing)" << endl;
cerr << R"(Error : Can't open file ")" << bin_filename.string() << R"(" for writing)" << endl;
exit(EXIT_FAILURE);
}
@ -738,12 +738,12 @@ StaticModel::writeStaticBlockMFile(const string &basename) const
void
StaticModel::writeStaticBlockCFile(const string &basename, vector<filesystem::path> per_block_object_files, const string &mexext, const filesystem::path &matlabroot, const filesystem::path &dynareroot) const
{
string filename = basename + "/model/src/static.c";
const filesystem::path filename {basename + "/model/src/static.c"};
ofstream output{filename, ios::out | ios::binary};
if (!output.is_open())
{
cerr << "ERROR: Can't open file " << filename << " for writing" << endl;
cerr << "ERROR: Can't open file " << filename.string() << " for writing" << endl;
exit(EXIT_FAILURE);
}