diff options
author | rtk0c <[email protected]> | 2022-05-30 23:42:02 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2022-05-30 23:42:02 -0700 |
commit | 8a0f2cd0b398ee0b7740e44a0e5fb2f75d090ccb (patch) | |
tree | 2838fd73313948f4db156b60c079468314eb1564 /source/10-common | |
parent | 3571627772b245e3e1a45b66e9a8fe4d50d06c8c (diff) |
Changeset: 59 Integrate enum codegen into the actual project
Diffstat (limited to 'source/10-common')
-rw-r--r-- | source/10-common/Utils.cpp | 24 | ||||
-rw-r--r-- | source/10-common/Utils.hpp | 4 |
2 files changed, 26 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); diff --git a/source/10-common/Utils.hpp b/source/10-common/Utils.hpp index 9f28aad..9560b57 100644 --- a/source/10-common/Utils.hpp +++ b/source/10-common/Utils.hpp @@ -5,6 +5,8 @@ #include <cstring> #include <filesystem> #include <glm/glm.hpp> +#include <string> +#include <string_view> namespace Utils { @@ -17,6 +19,8 @@ enum IoMode { FILE* OpenCstdioFile(const std::filesystem::path& path, IoMode mode, bool binary = false); FILE* OpenCstdioFile(const char* path, IoMode mode, bool binary = false); +std::string ReadFileAsString(const std::filesystem::path& path); + constexpr float Abs(float v) noexcept { return v < 0.0f ? -v : v; } |