From 8a0f2cd0b398ee0b7740e44a0e5fb2f75d090ccb Mon Sep 17 00:00:00 2001 From: rtk0c Date: Mon, 30 May 2022 23:42:02 -0700 Subject: Changeset: 59 Integrate enum codegen into the actual project --- source/10-common/Utils.cpp | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'source/10-common/Utils.cpp') 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 #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); -- cgit v1.2.3-70-g09d2