Skip to content

Commit

Permalink
Using filesystem path to open ini files (#2123)
Browse files Browse the repository at this point in the history
Following #2066
  • Loading branch information
payetvin authored May 29, 2024
1 parent ea05ba9 commit c145774
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/libs/antares/correlation/correlation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/libs/antares/inifile/include/antares/inifile/inifile.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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);

Expand Down
22 changes: 15 additions & 7 deletions src/libs/antares/inifile/inifile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "antares/inifile/inifile.h"

#include <filesystem>
#include <fstream>
#include <sstream>
#include <utility>
Expand All @@ -32,6 +33,8 @@

using namespace Yuni;

namespace fs = std::filesystem;

namespace Antares
{

Expand Down Expand Up @@ -82,7 +85,7 @@ IniFile::Section::~Section()
}
}

IniFile::IniFile(const AnyString& filename)
IniFile::IniFile(const fs::path& filename)
{
open(filename);
}
Expand Down Expand Up @@ -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<std::string>();
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;
Expand Down Expand Up @@ -270,7 +278,7 @@ std::string IniFile::toString() const
std::ostringstream ostream;

this->saveToStream(ostream, written);

return ostream.str();
}

Expand Down
2 changes: 1 addition & 1 deletion src/libs/antares/study/area/links.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libs/antares/study/header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/libs/antares/study/include/antares/study/sets.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ bool Sets<T>::loadFromFile(const std::filesystem::path& filename)
}

IniFile ini;
if (ini.open(filename.string()))
if (ini.open(filename))
{
Yuni::String value;

Expand Down
13 changes: 5 additions & 8 deletions src/libs/antares/study/parts/hydro/allocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@

#include "antares/study/parts/hydro/allocation.h"

#include <yuni/core/math.h>
#include <yuni/io/file.h>

#include <antares/utils/utils.h>
#include "antares/study/study.h"

using namespace Yuni;
namespace fs = std::filesystem;

namespace Antares
{
Expand Down Expand Up @@ -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())
{
Expand Down Expand Up @@ -205,15 +202,15 @@ bool HydroAllocation::saveToFile(const AnyString& filename) const
{
if (pValues.empty())
{
IO::File::CreateEmptyFile(filename);
Yuni::IO::File::CreateEmptyFile(filename);
return true;
}
else
{
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;
Expand Down
4 changes: 2 additions & 2 deletions src/libs/antares/study/parts/hydro/prepro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
12 changes: 6 additions & 6 deletions src/solver/constraints-builder/cbuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#include "antares/solver/constraints-builder/grid.h"

using namespace Yuni;
namespace fs = std::filesystem;

namespace Antares
{
Expand Down Expand Up @@ -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());
Expand All @@ -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;
}
Expand All @@ -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)";
Expand Down

0 comments on commit c145774

Please sign in to comment.