aboutsummaryrefslogtreecommitdiff
path: root/source/10-common/Utils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/10-common/Utils.cpp')
-rw-r--r--source/10-common/Utils.cpp24
1 files changed, 22 insertions, 2 deletions
diff --git a/source/10-common/Utils.cpp b/source/10-common/Utils.cpp
index 53b3863..dc76b0a 100644
--- a/source/10-common/Utils.cpp
+++ b/source/10-common/Utils.cpp
@@ -1,9 +1,14 @@
#include "Utils.hpp"
+#include "Macros.hpp"
+#include "ScopeGuard.hpp"
+
#ifdef _WIN32
# include <Windows.h>
#endif
+namespace fs = std::filesystem;
+
#ifdef _WIN32
# define BRUSSEL_MODE_STRING(string) L##string
#else
@@ -34,9 +39,9 @@ static FopenModeString GetModeString(Utils::IoMode mode, bool binary) {
return nullptr;
}
-FILE* Utils::OpenCstdioFile(const std::filesystem::path& path, IoMode mode, bool binary) {
+FILE* Utils::OpenCstdioFile(const fs::path& path, IoMode mode, bool binary) {
#ifdef _WIN32
- // std::filesystem::path::c_str() returns `const wchar_t*` under Windows, because NT uses UTF-16 natively
+ // fs::path::c_str() returns `const wchar_t*` under Windows, because NT uses UTF-16 natively
// NOTE: _wfopen() only affects the type of path parameter, otherwise the file stream created is identical to the one by fopen()
return _wfopen(path.c_str(), ::GetModeString(mode, binary));
#else
@@ -57,6 +62,21 @@ FILE* Utils::OpenCstdioFile(const char* path, IoMode mode, bool binary) {
#endif
}
+std::string Utils::ReadFileAsString(const fs::path& path) {
+ auto file = Utils::OpenCstdioFile(path, Utils::Read);
+ if (!file) throw std::runtime_error("Failed to open source file.");
+ DEFER { fclose(file); };
+
+ fseek(file, 0, SEEK_END);
+ auto fileSize = ftell(file);
+ rewind(file);
+
+ std::string result(fileSize, '\0');
+ fread(result.data(), fileSize, 1, file);
+
+ return result;
+}
+
bool Utils::InRangeInclusive(int n, int lower, int upper) {
if (lower > upper) {
std::swap(lower, upper);