Skip to content

Commit

Permalink
Replace Yuni::IO with std functions (#2066)
Browse files Browse the repository at this point in the history
The goal of this PR is to remove Yuni::IO functions in important parts
of the code (libs and solver)
`std::filesystem::path` is used instead of Yuni strings when needed,
more work will need to be done to use `path` on every level

No changes were made in:
- GUI
- deprecated tools
- functions that save the study

---------

Signed-off-by: Sylvain Leclerc <[email protected]>
Co-authored-by: Sylvain Leclerc <[email protected]>
Co-authored-by: Florian Omnès <[email protected]>
Co-authored-by: Jason Maréchal <[email protected]>
Co-authored-by: Florian OMNES <[email protected]>
  • Loading branch information
5 people authored May 27, 2024
1 parent 4a24881 commit 1776e8d
Show file tree
Hide file tree
Showing 48 changed files with 373 additions and 379 deletions.
11 changes: 11 additions & 0 deletions src/ext/yuni/src/yuni/core/string/traits/append.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "integer.h"
#include <stdio.h>
#include <cassert>
#include <filesystem>

#ifdef YUNI_OS_MSVC
#define YUNI_PRIVATE_MEMBUF_SPTRINF(BUFFER, SIZE, F, V) ::sprintf_s(BUFFER, SIZE, F, V)
Expand Down Expand Up @@ -294,6 +295,16 @@ class Append<CStringT, std::list<T>> final
}
};

// std::filesystem::path<>
template<class CStringT>
class Append<CStringT, std::filesystem::path> final
{
public:
static void Perform(CStringT& s, const std::filesystem::path& rhs)
{
s << rhs.string();
}
};
} // namespace CString
} // namespace Extension
} // namespace Yuni
Expand Down
50 changes: 38 additions & 12 deletions src/libs/antares/io/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,53 @@
#include <yuni/core/system/suspend.h>
#include <yuni/datetime/timestamp.h>
#include <yuni/io/file.h>

#ifdef YUNI_OS_WINDOWS
#include <io.h>

#include <yuni/core/system/windows.hdr.h>
#else
#include <sys/types.h>
#include <unistd.h>
#endif
#include <errno.h>

#include <fstream>

#include <antares/logs/logs.h>

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

constexpr int retryTimeout = 35; // seconds

namespace Antares::IO
{

static void logErrorAndThrow [[noreturn]] (const std::string& errorMessage)
{
Antares::logs.error() << errorMessage;
throw std::runtime_error(errorMessage);
}

enum
std::string readFile(const fs::path& filePath)
{
retryTimeout = 35, // seconds
};
std::ifstream file(filePath, std::ios_base::binary | std::ios_base::in);
if (!file.is_open())
{
logErrorAndThrow(filePath.string() + ": file does not exist");
}

using Iterator = std::istreambuf_iterator<char>;
std::string content(Iterator{file}, Iterator{});
if (!file)
{
logErrorAndThrow("Read failed '" + filePath.string() + "'");
}
return content;
}

bool IOFileSetContent(const AnyString& filename, const AnyString& content)
bool fileSetContent(const std::string& filename, const std::string& content)
{
if (System::windows)
if (Yuni::System::windows)
{
// On Windows, there is still the hard limit to 256 chars even if the API allows more
if (filename.size() >= 256)
Expand All @@ -67,17 +91,17 @@ bool IOFileSetContent(const AnyString& filename, const AnyString& content)
<< " (probably not enough disk space).";

// Notification via the UI interface
String text;
DateTime::TimestampToString(text, "%H:%M");
Yuni::String text;
Yuni::DateTime::TimestampToString(text, "%H:%M");
logs.info() << "Not enough disk space since " << text << ". Waiting...";
// break;
}
// waiting a little...
Suspend(retryTimeout);
Yuni::Suspend(retryTimeout);
}
++attempt;

IO::File::Stream out(filename, IO::OpenMode::write);
Yuni::IO::File::Stream out(filename, Yuni::IO::OpenMode::write);
if (not out.opened())
{
switch (errno)
Expand Down Expand Up @@ -141,3 +165,5 @@ bool IOFileSetContent(const AnyString& filename, const AnyString& content)

return false;
}

} // namespace Antares::IO
11 changes: 9 additions & 2 deletions src/libs/antares/io/include/antares/io/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,21 @@
#ifndef __LIBS_ANTARES_IO_FILE_H__
#define __LIBS_ANTARES_IO_FILE_H__

#include <yuni/yuni.h>
#include <yuni/core/string.h>

#include <filesystem>

namespace Antares::IO
{
/*!
** \brief Reset the content of a file
**
** This routine will wait if there is not enough disk space
*/
bool IOFileSetContent(const AnyString& filename, const AnyString& content);
bool fileSetContent(const std::string& filename, const std::string& content);

std::string readFile(const std::filesystem::path&);

} // namespace Antares::IO

#endif // __LIBS_ANTARES_IO_FILE_H__
6 changes: 3 additions & 3 deletions src/libs/antares/study/area/area.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Area::Area(const AnyString& name):
{
internalInitialize();
this->name = name;
Antares::TransformNameIntoID(this->name, this->id);
this->id = Antares::transformNameIntoID(this->name);
}

Area::Area(const AnyString& name, const AnyString& id):
Expand All @@ -67,8 +67,8 @@ Area::Area(const AnyString& name, const AnyString& id):
{
internalInitialize();
this->name = name;
AreaName givenID = id;
Antares::TransformNameIntoID(givenID, this->id);
this->id = Antares::transformNameIntoID(id);

}

Area::~Area()
Expand Down
16 changes: 8 additions & 8 deletions src/libs/antares/study/area/links.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
using namespace Yuni;
using namespace Antares;

namespace fs = std::filesystem;

#define SEP (IO::Separator)

namespace
Expand Down Expand Up @@ -473,19 +475,18 @@ bool AreaLinksInternalLoadFromProperty(AreaLink& link, const String& key, const
}
} // anonymous namespace

bool AreaLinksLoadFromFolder(Study& study, AreaList* l, Area* area, const AnyString& folder)
bool AreaLinksLoadFromFolder(Study& study, AreaList* l, Area* area, const fs::path& folder)
{
// Assert
assert(area);

/* Initialize */
String buffer;
buffer << folder << SEP << "properties.ini";
fs::path path = folder / "properties.ini";

IniFile ini;
if (!ini.open(buffer))
if (!ini.open(path.string()))
{
return 0;
return false;
}

bool ret = true;
Expand All @@ -496,8 +497,7 @@ bool AreaLinksLoadFromFolder(Study& study, AreaList* l, Area* area, const AnyStr
for (auto* s = ini.firstSection; s; s = s->next)
{
// Getting the name of the area
buffer.clear();
TransformNameIntoID(s->name, buffer);
std::string buffer = transformNameIntoID(s->name);

// Trying to find it
Area* linkedWith = AreaListLFind(l, buffer.c_str());
Expand All @@ -518,7 +518,7 @@ bool AreaLinksLoadFromFolder(Study& study, AreaList* l, Area* area, const AnyStr
link.comments.clear();
link.displayComments = true;

ret = link.loadTimeSeries(study.header.version, folder) && ret;
ret = link.loadTimeSeries(study.header.version, folder.string()) && ret;

// Checks on loaded link's data
if (study.usedByTheSolver)
Expand Down
Loading

0 comments on commit 1776e8d

Please sign in to comment.