Core/File: Add DecodeOpenMode
This commit is contained in:
parent
4065fbfb1a
commit
7cd1b32e95
|
|
@ -11,7 +11,7 @@
|
||||||
#include <Nazara/Core/ByteArray.hpp>
|
#include <Nazara/Core/ByteArray.hpp>
|
||||||
#include <Nazara/Core/Stream.hpp>
|
#include <Nazara/Core/Stream.hpp>
|
||||||
#include <NazaraUtils/Endianness.hpp>
|
#include <NazaraUtils/Endianness.hpp>
|
||||||
#include <NazaraUtils/MovablePtr.hpp>
|
#include <NazaraUtils/Result.hpp>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
@ -56,6 +56,7 @@ namespace Nz
|
||||||
|
|
||||||
static inline ByteArray ComputeHash(HashType hash, const std::filesystem::path& filePath);
|
static inline ByteArray ComputeHash(HashType hash, const std::filesystem::path& filePath);
|
||||||
static inline ByteArray ComputeHash(AbstractHash& hash, const std::filesystem::path& filePath);
|
static inline ByteArray ComputeHash(AbstractHash& hash, const std::filesystem::path& filePath);
|
||||||
|
static /*constexpr*/ Result<OpenModeFlags, std::string> DecodeOpenMode(std::string_view openModeStr);
|
||||||
static std::optional<std::vector<UInt8>> ReadWhole(const std::filesystem::path& path);
|
static std::optional<std::vector<UInt8>> ReadWhole(const std::filesystem::path& path);
|
||||||
static bool WriteWhole(const std::filesystem::path& path, const void* data, std::size_t size);
|
static bool WriteWhole(const std::filesystem::path& path, const void* data, std::size_t size);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
#include <Nazara/Core/Algorithm.hpp>
|
#include <Nazara/Core/Algorithm.hpp>
|
||||||
|
#include <Nazara/Core/Format.hpp>
|
||||||
#include <Nazara/Core/Debug.hpp>
|
#include <Nazara/Core/Debug.hpp>
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz
|
||||||
|
|
@ -33,6 +34,37 @@ namespace Nz
|
||||||
return Nz::ComputeHash(hash, File(filePath));
|
return Nz::ComputeHash(hash, File(filePath));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline /*constexpr*/ Result<OpenModeFlags, std::string> 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()
|
inline bool File::CheckFileOpening()
|
||||||
{
|
{
|
||||||
if (m_openMode.Test(OpenMode::Defer))
|
if (m_openMode.Test(OpenMode::Defer))
|
||||||
|
|
|
||||||
|
|
@ -84,38 +84,16 @@ aiFile* StreamOpener(aiFileIO* fileIO, const char* filePath, const char* openMod
|
||||||
{
|
{
|
||||||
ErrorFlags errFlags({}, ~ErrorMode::ThrowException);
|
ErrorFlags errFlags({}, ~ErrorMode::ThrowException);
|
||||||
|
|
||||||
///TODO: Move to File::DecodeOpenMode
|
Result<OpenModeFlags, std::string> openModes = File::DecodeOpenMode(openMode);
|
||||||
OpenModeFlags openModeEnum = 0;
|
|
||||||
|
|
||||||
if (std::strchr(openMode, 'r'))
|
if (openModes.IsErr())
|
||||||
{
|
{
|
||||||
openModeEnum |= OpenMode::Read;
|
NazaraErrorFmt("{0} for file {1}", openModes.GetError(), filePath);
|
||||||
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);
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!std::strchr(openMode, 'b'))
|
|
||||||
openModeEnum |= OpenMode::Text;
|
|
||||||
|
|
||||||
std::unique_ptr<File> file = std::make_unique<File>();
|
std::unique_ptr<File> file = std::make_unique<File>();
|
||||||
if (!file->Open(filePath, openModeEnum))
|
if (!file->Open(filePath, openModes.GetValue()))
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
stream = reinterpret_cast<char*>(file.release());
|
stream = reinterpret_cast<char*>(file.release());
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue