From c1457747e48eddf8f51a2e1be9dace369e1fcfb7 Mon Sep 17 00:00:00 2001 From: payetvin <113102157+payetvin@users.noreply.github.com> Date: Wed, 29 May 2024 11:02:52 +0200 Subject: [PATCH] Using filesystem path to open ini files (#2123) Following #2066 --- src/libs/antares/correlation/correlation.cpp | 4 ++-- .../include/antares/correlation/correlation.h | 2 +- .../inifile/include/antares/inifile/inifile.h | 6 +++-- src/libs/antares/inifile/inifile.cpp | 22 +++++++++++++------ src/libs/antares/study/area/links.cpp | 2 +- src/libs/antares/study/header.cpp | 2 +- .../study/include/antares/study/sets.hxx | 2 +- .../antares/study/parts/hydro/allocation.cpp | 13 +++++------ src/libs/antares/study/parts/hydro/prepro.cpp | 4 ++-- .../parts/short-term-storage/container.cpp | 2 +- src/solver/constraints-builder/cbuilder.cpp | 12 +++++----- 11 files changed, 39 insertions(+), 32 deletions(-) diff --git a/src/libs/antares/correlation/correlation.cpp b/src/libs/antares/correlation/correlation.cpp index 9642f9d1db..84c39b974f 100644 --- a/src/libs/antares/correlation/correlation.cpp +++ b/src/libs/antares/correlation/correlation.cpp @@ -249,12 +249,12 @@ int InterAreaCorrelationLoadFromIniFile(Matrix<>* m, AreaList* l, IniFile* ini, return 0; } -int InterAreaCorrelationLoadFromFile(Matrix<>* m, AreaList* l, const char filename[]) +int InterAreaCorrelationLoadFromFile(Matrix<>* m, AreaList* l, const std::string& filename) { /* Asserts */ assert(m); assert(l); - assert(filename); + assert(!filename.empty()); InterAreaCorrelationResetMatrix(m, l); IniFile ini; diff --git a/src/libs/antares/correlation/include/antares/correlation/correlation.h b/src/libs/antares/correlation/include/antares/correlation/correlation.h index 6dc3693f93..9dc48c5a76 100644 --- a/src/libs/antares/correlation/include/antares/correlation/correlation.h +++ b/src/libs/antares/correlation/include/antares/correlation/correlation.h @@ -186,7 +186,7 @@ void InterAreaCorrelationResetMatrix(Matrix<>* m, AreaList* l); ** \param filename The filename to use to load values ** \return A non-zero value if the matrix has been loaded, 0 otherwise */ -int InterAreaCorrelationLoadFromFile(Matrix<>* m, AreaList* l, const char filename[]); +int InterAreaCorrelationLoadFromFile(Matrix<>* m, AreaList* l, const std::string& filename); /*! ** \brief Save an inter-area correlation matrix to a file diff --git a/src/libs/antares/inifile/include/antares/inifile/inifile.h b/src/libs/antares/inifile/include/antares/inifile/inifile.h index cc10749580..22dc679b5e 100644 --- a/src/libs/antares/inifile/include/antares/inifile/inifile.h +++ b/src/libs/antares/inifile/include/antares/inifile/inifile.h @@ -169,7 +169,7 @@ class IniFile final /*! ** \brief Load an INI file */ - explicit IniFile(const AnyString& filename); + explicit IniFile(const std::filesystem::path& filename); /*! ** \brief Destructor */ @@ -194,7 +194,9 @@ class IniFile final ** \param filename Filename to load ** \return True if the operation succeeded, false otherwise */ - bool open(const AnyString& filename, bool warnings = true); + bool open(const std::string& filename, bool warnings = true); + + bool open(const std::filesystem::path& filename, bool warnings = true); bool readStream(std::istream& in_stream); diff --git a/src/libs/antares/inifile/inifile.cpp b/src/libs/antares/inifile/inifile.cpp index a718bfded7..e6524920ee 100644 --- a/src/libs/antares/inifile/inifile.cpp +++ b/src/libs/antares/inifile/inifile.cpp @@ -21,6 +21,7 @@ #include "antares/inifile/inifile.h" +#include #include #include #include @@ -32,6 +33,8 @@ using namespace Yuni; +namespace fs = std::filesystem; + namespace Antares { @@ -82,7 +85,7 @@ IniFile::Section::~Section() } } -IniFile::IniFile(const AnyString& filename) +IniFile::IniFile(const fs::path& filename) { open(filename); } @@ -231,17 +234,22 @@ bool IniFile::readStream(std::istream& in_stream) return true; } -bool IniFile::open(const AnyString& filename, bool warnings) +bool IniFile::open(const std::string& filename, bool warnings) +{ + fs::path filepath = filename; + return open(filepath, warnings); +} + +bool IniFile::open(const fs::path& filename, bool warnings) { clear(); - filename_ = filename; // storing filename for further use - std::string filePath = filename.to(); + filename_ = filename.string(); // storing filename for further use - if (std::ifstream file(filePath); file.is_open()) + if (std::ifstream file(filename); file.is_open()) { if (! readStream(file)) { - logs.error() << "Invalid INI file : " << filePath; + logs.error() << "Invalid INI file : " << filename; return false; } return true; @@ -270,7 +278,7 @@ std::string IniFile::toString() const std::ostringstream ostream; this->saveToStream(ostream, written); - + return ostream.str(); } diff --git a/src/libs/antares/study/area/links.cpp b/src/libs/antares/study/area/links.cpp index ef4c81adc3..4d4b6af140 100644 --- a/src/libs/antares/study/area/links.cpp +++ b/src/libs/antares/study/area/links.cpp @@ -484,7 +484,7 @@ bool AreaLinksLoadFromFolder(Study& study, AreaList* l, Area* area, const fs::pa fs::path path = folder / "properties.ini"; IniFile ini; - if (!ini.open(path.string())) + if (!ini.open(path)) { return false; } diff --git a/src/libs/antares/study/header.cpp b/src/libs/antares/study/header.cpp index 13283077f6..4b751d75c7 100644 --- a/src/libs/antares/study/header.cpp +++ b/src/libs/antares/study/header.cpp @@ -239,7 +239,7 @@ StudyVersion StudyHeader::tryToFindTheVersion(const std::string& folder) bool StudyHeader::readVersionFromFile(const fs::path& filename, std::string& version) { IniFile ini; - if (ini.open(filename.string())) + if (ini.open(filename)) { return internalFindVersionFromFile(ini, version); } diff --git a/src/libs/antares/study/include/antares/study/sets.hxx b/src/libs/antares/study/include/antares/study/sets.hxx index 3ab31e9f8a..d2976252cc 100644 --- a/src/libs/antares/study/include/antares/study/sets.hxx +++ b/src/libs/antares/study/include/antares/study/sets.hxx @@ -226,7 +226,7 @@ bool Sets::loadFromFile(const std::filesystem::path& filename) } IniFile ini; - if (ini.open(filename.string())) + if (ini.open(filename)) { Yuni::String value; diff --git a/src/libs/antares/study/parts/hydro/allocation.cpp b/src/libs/antares/study/parts/hydro/allocation.cpp index 997fad7c72..93ee0ef5d0 100644 --- a/src/libs/antares/study/parts/hydro/allocation.cpp +++ b/src/libs/antares/study/parts/hydro/allocation.cpp @@ -21,13 +21,10 @@ #include "antares/study/parts/hydro/allocation.h" -#include -#include - #include #include "antares/study/study.h" -using namespace Yuni; +namespace fs = std::filesystem; namespace Antares { @@ -168,12 +165,12 @@ void HydroAllocation::clear() } bool HydroAllocation::loadFromFile(const AreaName& referencearea, - const std::filesystem::path& filename) + const fs::path& filename) { clear(); IniFile ini; - if (std::filesystem::exists(filename) && ini.open(filename.string())) + if (fs::exists(filename) && ini.open(filename)) { if (!ini.empty()) { @@ -205,7 +202,7 @@ bool HydroAllocation::saveToFile(const AnyString& filename) const { if (pValues.empty()) { - IO::File::CreateEmptyFile(filename); + Yuni::IO::File::CreateEmptyFile(filename); return true; } else @@ -213,7 +210,7 @@ bool HydroAllocation::saveToFile(const AnyString& filename) const IniFile ini; auto* s = ini.addSection("[allocation]"); auto end = pValues.end(); - CString<64, false> str; + Yuni::CString<64, false> str; for (auto i = pValues.begin(); i != end; ++i) { double v = i->second; diff --git a/src/libs/antares/study/parts/hydro/prepro.cpp b/src/libs/antares/study/parts/hydro/prepro.cpp index dda880ad81..d8ddb72352 100644 --- a/src/libs/antares/study/parts/hydro/prepro.cpp +++ b/src/libs/antares/study/parts/hydro/prepro.cpp @@ -46,7 +46,7 @@ static bool PreproHydroSaveSettings(PreproHydro* h, const char* filename) return ini.save(filename); } -static bool PreproHydroLoadSettings(PreproHydro* h, const char* filename) +static bool PreproHydroLoadSettings(PreproHydro* h, const std::string& filename) { IniFile ini; IniFile::Section* s; @@ -160,7 +160,7 @@ bool PreproHydro::loadFromFolder(Study& s, const AreaName& areaID, const char* f String& buffer = s.bufferLoadingTS; buffer.clear() << folder << SEP << areaID << SEP << "prepro.ini"; - bool ret = (PreproHydroLoadSettings(this, buffer.c_str()) ? true : false); + bool ret = PreproHydroLoadSettings(this, buffer); if (intermonthlyCorrelation < 0. || intermonthlyCorrelation > 1.) { logs.error() << "Hydro: Prepro: `" << areaID diff --git a/src/libs/antares/study/parts/short-term-storage/container.cpp b/src/libs/antares/study/parts/short-term-storage/container.cpp index 37b6ee1953..1c9ca78bf7 100644 --- a/src/libs/antares/study/parts/short-term-storage/container.cpp +++ b/src/libs/antares/study/parts/short-term-storage/container.cpp @@ -46,7 +46,7 @@ bool STStorageInput::createSTStorageClustersFromIniFile(const fs::path& path) { const fs::path pathIni = path / "list.ini"; IniFile ini; - if (!ini.open(pathIni.string())) + if (!ini.open(pathIni)) { return false; } diff --git a/src/solver/constraints-builder/cbuilder.cpp b/src/solver/constraints-builder/cbuilder.cpp index b1cb2d0a90..a49806ff16 100644 --- a/src/solver/constraints-builder/cbuilder.cpp +++ b/src/solver/constraints-builder/cbuilder.cpp @@ -26,7 +26,7 @@ #include "antares/solver/constraints-builder/grid.h" -using namespace Yuni; +namespace fs = std::filesystem; namespace Antares { @@ -364,7 +364,7 @@ bool CBuilder::saveCBuilderToFile(const String& filename) const if (filename == "") { - std::filesystem::path path = std::filesystem::path(pStudy.folder.c_str()) / "settings" + fs::path path = fs::path(pStudy.folder.c_str()) / "settings" / "constraintbuilder.ini"; return ini.save(path.string()); @@ -375,11 +375,11 @@ bool CBuilder::saveCBuilderToFile(const String& filename) const bool CBuilder::completeCBuilderFromFile(const std::string& filename) { - std::filesystem::path path; + fs::path path; if (filename == "") { - path = std::filesystem::path(pStudy.folder.c_str()) / "settings" / "constraintbuilder.ini"; - if (!std::filesystem::exists(path)) + path = fs::path(pStudy.folder.c_str()) / "settings" / "constraintbuilder.ini"; + if (!fs::exists(path)) { return false; } @@ -391,7 +391,7 @@ bool CBuilder::completeCBuilderFromFile(const std::string& filename) logs.info() << "Read data"; IniFile ini; - if (ini.open(path.string())) + if (ini.open(path)) { // logs.info() << "Reading " << filename; logs.info() << "Read data (INI file)";