file_sys/mode: Make use of DECLARE_ENUM_FLAG_OPERATORS with Mode

Same behavior, minus a hand-rolled operator.
This commit is contained in:
Lioncash 2020-08-03 07:11:07 -04:00
parent 05781ce8c4
commit 2b8ae009a0
2 changed files with 21 additions and 18 deletions

View File

@ -4,6 +4,7 @@
#pragma once #pragma once
#include "common/common_funcs.h"
#include "common/common_types.h" #include "common/common_types.h"
namespace FileSys { namespace FileSys {
@ -11,13 +12,11 @@ namespace FileSys {
enum class Mode : u32 { enum class Mode : u32 {
Read = 1, Read = 1,
Write = 2, Write = 2,
ReadWrite = 3, ReadWrite = Read | Write,
Append = 4, Append = 4,
WriteAppend = 6, WriteAppend = Write | Append,
}; };
inline u32 operator&(Mode lhs, Mode rhs) { DECLARE_ENUM_FLAG_OPERATORS(Mode)
return static_cast<u32>(lhs) & static_cast<u32>(rhs);
}
} // namespace FileSys } // namespace FileSys

View File

@ -18,21 +18,23 @@ static std::string ModeFlagsToString(Mode mode) {
std::string mode_str; std::string mode_str;
// Calculate the correct open mode for the file. // Calculate the correct open mode for the file.
if (mode & Mode::Read && mode & Mode::Write) { if (True(mode & Mode::Read) && True(mode & Mode::Write)) {
if (mode & Mode::Append) if (True(mode & Mode::Append)) {
mode_str = "a+"; mode_str = "a+";
else
mode_str = "r+";
} else { } else {
if (mode & Mode::Read) mode_str = "r+";
}
} else {
if (True(mode & Mode::Read)) {
mode_str = "r"; mode_str = "r";
else if (mode & Mode::Append) } else if (True(mode & Mode::Append)) {
mode_str = "a"; mode_str = "a";
else if (mode & Mode::Write) } else if (True(mode & Mode::Write)) {
mode_str = "w"; mode_str = "w";
else } else {
UNREACHABLE_MSG("Invalid file open mode: {:02X}", static_cast<u8>(mode)); UNREACHABLE_MSG("Invalid file open mode: {:02X}", static_cast<u8>(mode));
} }
}
mode_str += "b"; mode_str += "b";
@ -73,8 +75,9 @@ VirtualFile RealVfsFilesystem::OpenFile(std::string_view path_, Mode perms) {
} }
} }
if (!FileUtil::Exists(path) && (perms & Mode::WriteAppend) != 0) if (!FileUtil::Exists(path) && True(perms & Mode::WriteAppend)) {
FileUtil::CreateEmptyFile(path); FileUtil::CreateEmptyFile(path);
}
auto backing = std::make_shared<FileUtil::IOFile>(path, ModeFlagsToString(perms).c_str()); auto backing = std::make_shared<FileUtil::IOFile>(path, ModeFlagsToString(perms).c_str());
cache[path] = backing; cache[path] = backing;
@ -247,11 +250,11 @@ std::shared_ptr<VfsDirectory> RealVfsFile::GetContainingDirectory() const {
} }
bool RealVfsFile::IsWritable() const { bool RealVfsFile::IsWritable() const {
return (perms & Mode::WriteAppend) != 0; return True(perms & Mode::WriteAppend);
} }
bool RealVfsFile::IsReadable() const { bool RealVfsFile::IsReadable() const {
return (perms & Mode::ReadWrite) != 0; return True(perms & Mode::ReadWrite);
} }
std::size_t RealVfsFile::Read(u8* data, std::size_t length, std::size_t offset) const { std::size_t RealVfsFile::Read(u8* data, std::size_t length, std::size_t offset) const {
@ -319,9 +322,10 @@ RealVfsDirectory::RealVfsDirectory(RealVfsFilesystem& base_, const std::string&
path_components(FileUtil::SplitPathComponents(path)), path_components(FileUtil::SplitPathComponents(path)),
parent_components(FileUtil::SliceVector(path_components, 0, path_components.size() - 1)), parent_components(FileUtil::SliceVector(path_components, 0, path_components.size() - 1)),
perms(perms_) { perms(perms_) {
if (!FileUtil::Exists(path) && perms & Mode::WriteAppend) if (!FileUtil::Exists(path) && True(perms & Mode::WriteAppend)) {
FileUtil::CreateDir(path); FileUtil::CreateDir(path);
} }
}
RealVfsDirectory::~RealVfsDirectory() = default; RealVfsDirectory::~RealVfsDirectory() = default;
@ -371,11 +375,11 @@ std::vector<std::shared_ptr<VfsDirectory>> RealVfsDirectory::GetSubdirectories()
} }
bool RealVfsDirectory::IsWritable() const { bool RealVfsDirectory::IsWritable() const {
return (perms & Mode::WriteAppend) != 0; return True(perms & Mode::WriteAppend);
} }
bool RealVfsDirectory::IsReadable() const { bool RealVfsDirectory::IsReadable() const {
return (perms & Mode::ReadWrite) != 0; return True(perms & Mode::ReadWrite);
} }
std::string RealVfsDirectory::GetName() const { std::string RealVfsDirectory::GetName() const {