Core/File: Add DecodeOpenMode

This commit is contained in:
Lynix
2023-12-25 19:51:19 +01:00
parent 4065fbfb1a
commit 7cd1b32e95
3 changed files with 38 additions and 27 deletions

View File

@@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Core/Format.hpp>
#include <Nazara/Core/Debug.hpp>
namespace Nz
@@ -33,6 +34,37 @@ namespace Nz
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()
{
if (m_openMode.Test(OpenMode::Defer))