From 7cd1b32e957067cad355f09c747e5477f8cb3b40 Mon Sep 17 00:00:00 2001 From: Lynix Date: Mon, 25 Dec 2023 19:51:19 +0100 Subject: [PATCH] Core/File: Add DecodeOpenMode --- include/Nazara/Core/File.hpp | 3 ++- include/Nazara/Core/File.inl | 32 ++++++++++++++++++++++++++++++++ plugins/Assimp/CustomStream.cpp | 30 ++++-------------------------- 3 files changed, 38 insertions(+), 27 deletions(-) diff --git a/include/Nazara/Core/File.hpp b/include/Nazara/Core/File.hpp index 217f32398..5e6bf61bd 100644 --- a/include/Nazara/Core/File.hpp +++ b/include/Nazara/Core/File.hpp @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include #include @@ -56,6 +56,7 @@ namespace Nz static inline ByteArray ComputeHash(HashType hash, const std::filesystem::path& filePath); static inline ByteArray ComputeHash(AbstractHash& hash, const std::filesystem::path& filePath); + static /*constexpr*/ Result DecodeOpenMode(std::string_view openModeStr); static std::optional> ReadWhole(const std::filesystem::path& path); static bool WriteWhole(const std::filesystem::path& path, const void* data, std::size_t size); diff --git a/include/Nazara/Core/File.inl b/include/Nazara/Core/File.inl index be6288e2e..e7f3b9719 100644 --- a/include/Nazara/Core/File.inl +++ b/include/Nazara/Core/File.inl @@ -3,6 +3,7 @@ // For conditions of distribution and use, see copyright notice in Config.hpp #include +#include #include namespace Nz @@ -33,6 +34,37 @@ namespace Nz return Nz::ComputeHash(hash, File(filePath)); } + inline /*constexpr*/ Result File::DecodeOpenMode(std::string_view openModeStr) + { + OpenModeFlags openModes = 0; + + if (openModeStr.find('r') != std::string_view::npos) + { + openModes |= OpenMode::Read; + if (openModeStr.find('+') != std::string_view::npos) + openModes |= OpenMode_ReadWrite | OpenMode::MustExist; + } + else if (openModeStr.find('w') != std::string_view::npos) + { + openModes |= OpenMode::Write | OpenMode::Truncate; + if (openModeStr.find('+') != std::string_view::npos) + openModes |= OpenMode::Read; + } + else if (openModeStr.find('a') != std::string_view::npos) + { + openModes |= OpenMode::Write | OpenMode::Append; + if (openModeStr.find('+') != std::string_view::npos) + openModes |= OpenMode::Read; + } + else + return Err(Format("unhandled or invalid openmode {0}", openModeStr)); + + if (openModeStr.find('b') == std::string_view::npos) + openModes |= OpenMode::Text; + + return openModes; + } + inline bool File::CheckFileOpening() { if (m_openMode.Test(OpenMode::Defer)) diff --git a/plugins/Assimp/CustomStream.cpp b/plugins/Assimp/CustomStream.cpp index 8569176c0..b45a2b22d 100644 --- a/plugins/Assimp/CustomStream.cpp +++ b/plugins/Assimp/CustomStream.cpp @@ -84,38 +84,16 @@ aiFile* StreamOpener(aiFileIO* fileIO, const char* filePath, const char* openMod { ErrorFlags errFlags({}, ~ErrorMode::ThrowException); - ///TODO: Move to File::DecodeOpenMode - OpenModeFlags openModeEnum = 0; + Result openModes = File::DecodeOpenMode(openMode); - if (std::strchr(openMode, 'r')) + if (openModes.IsErr()) { - openModeEnum |= OpenMode::Read; - if (std::strchr(openMode, '+')) - openModeEnum |= OpenMode_ReadWrite | OpenMode::MustExist; - } - else if (std::strchr(openMode, 'w')) - { - openModeEnum |= OpenMode::Write | OpenMode::Truncate; - if (std::strchr(openMode, '+')) - openModeEnum |= OpenMode::Read; - } - else if (std::strchr(openMode, 'a')) - { - openModeEnum |= OpenMode::Write | OpenMode::Append; - if (std::strchr(openMode, '+')) - openModeEnum |= OpenMode::Read; - } - else - { - NazaraErrorFmt("unhandled/invalid openmode: {0} for file {1}", openMode, filePath); + NazaraErrorFmt("{0} for file {1}", openModes.GetError(), filePath); return nullptr; } - if (!std::strchr(openMode, 'b')) - openModeEnum |= OpenMode::Text; - std::unique_ptr file = std::make_unique(); - if (!file->Open(filePath, openModeEnum)) + if (!file->Open(filePath, openModes.GetValue())) return nullptr; stream = reinterpret_cast(file.release());