FS: Make LowPathType a strongly typed enum.

This commit is contained in:
Subv 2017-12-12 17:41:36 -05:00
parent 6afd091c37
commit 90fdc8dcbf
3 changed files with 28 additions and 28 deletions

View File

@ -14,19 +14,19 @@ namespace FileSys {
Path::Path(LowPathType type, u32 size, u32 pointer) : type(type) { Path::Path(LowPathType type, u32 size, u32 pointer) : type(type) {
switch (type) { switch (type) {
case Binary: { case LowPathType::Binary: {
binary.resize(size); binary.resize(size);
Memory::ReadBlock(pointer, binary.data(), binary.size()); Memory::ReadBlock(pointer, binary.data(), binary.size());
break; break;
} }
case Char: { case LowPathType::Char: {
string.resize(size - 1); // Data is always null-terminated. string.resize(size - 1); // Data is always null-terminated.
Memory::ReadBlock(pointer, &string[0], string.size()); Memory::ReadBlock(pointer, &string[0], string.size());
break; break;
} }
case Wchar: { case LowPathType::Wchar: {
u16str.resize(size / 2 - 1); // Data is always null-terminated. u16str.resize(size / 2 - 1); // Data is always null-terminated.
Memory::ReadBlock(pointer, &u16str[0], u16str.size() * sizeof(char16_t)); Memory::ReadBlock(pointer, &u16str[0], u16str.size() * sizeof(char16_t));
break; break;
@ -39,12 +39,12 @@ Path::Path(LowPathType type, u32 size, u32 pointer) : type(type) {
std::string Path::DebugStr() const { std::string Path::DebugStr() const {
switch (GetType()) { switch (GetType()) {
case Invalid: case LowPathType::Invalid:
default: default:
return "[Invalid]"; return "[Invalid]";
case Empty: case LowPathType::Empty:
return "[Empty]"; return "[Empty]";
case Binary: { case LowPathType::Binary: {
std::stringstream res; std::stringstream res;
res << "[Binary: "; res << "[Binary: ";
for (unsigned byte : binary) for (unsigned byte : binary)
@ -52,23 +52,23 @@ std::string Path::DebugStr() const {
res << ']'; res << ']';
return res.str(); return res.str();
} }
case Char: case LowPathType::Char:
return "[Char: " + AsString() + ']'; return "[Char: " + AsString() + ']';
case Wchar: case LowPathType::Wchar:
return "[Wchar: " + AsString() + ']'; return "[Wchar: " + AsString() + ']';
} }
} }
std::string Path::AsString() const { std::string Path::AsString() const {
switch (GetType()) { switch (GetType()) {
case Char: case LowPathType::Char:
return string; return string;
case Wchar: case LowPathType::Wchar:
return Common::UTF16ToUTF8(u16str); return Common::UTF16ToUTF8(u16str);
case Empty: case LowPathType::Empty:
return {}; return {};
case Invalid: case LowPathType::Invalid:
case Binary: case LowPathType::Binary:
default: default:
// TODO(yuriks): Add assert // TODO(yuriks): Add assert
LOG_ERROR(Service_FS, "LowPathType cannot be converted to string!"); LOG_ERROR(Service_FS, "LowPathType cannot be converted to string!");
@ -78,14 +78,14 @@ std::string Path::AsString() const {
std::u16string Path::AsU16Str() const { std::u16string Path::AsU16Str() const {
switch (GetType()) { switch (GetType()) {
case Char: case LowPathType::Char:
return Common::UTF8ToUTF16(string); return Common::UTF8ToUTF16(string);
case Wchar: case LowPathType::Wchar:
return u16str; return u16str;
case Empty: case LowPathType::Empty:
return {}; return {};
case Invalid: case LowPathType::Invalid:
case Binary: case LowPathType::Binary:
// TODO(yuriks): Add assert // TODO(yuriks): Add assert
LOG_ERROR(Service_FS, "LowPathType cannot be converted to u16string!"); LOG_ERROR(Service_FS, "LowPathType cannot be converted to u16string!");
return {}; return {};
@ -96,11 +96,11 @@ std::u16string Path::AsU16Str() const {
std::vector<u8> Path::AsBinary() const { std::vector<u8> Path::AsBinary() const {
switch (GetType()) { switch (GetType()) {
case Binary: case LowPathType::Binary:
return binary; return binary;
case Char: case LowPathType::Char:
return std::vector<u8>(string.begin(), string.end()); return std::vector<u8>(string.begin(), string.end());
case Wchar: { case LowPathType::Wchar: {
// use two u8 for each character of u16str // use two u8 for each character of u16str
std::vector<u8> to_return(u16str.size() * 2); std::vector<u8> to_return(u16str.size() * 2);
for (size_t i = 0; i < u16str.size(); ++i) { for (size_t i = 0; i < u16str.size(); ++i) {
@ -110,9 +110,9 @@ std::vector<u8> Path::AsBinary() const {
} }
return to_return; return to_return;
} }
case Empty: case LowPathType::Empty:
return {}; return {};
case Invalid: case LowPathType::Invalid:
default: default:
// TODO(yuriks): Add assert // TODO(yuriks): Add assert
LOG_ERROR(Service_FS, "LowPathType cannot be converted to binary!"); LOG_ERROR(Service_FS, "LowPathType cannot be converted to binary!");

View File

@ -19,7 +19,7 @@ class FileBackend;
class DirectoryBackend; class DirectoryBackend;
// Path string type // Path string type
enum LowPathType : u32 { enum class LowPathType : u32 {
Invalid = 0, Invalid = 0,
Empty = 1, Empty = 1,
Binary = 2, Binary = 2,
@ -36,9 +36,9 @@ union Mode {
class Path { class Path {
public: public:
Path() : type(Invalid) {} Path() : type(LowPathType::Invalid) {}
Path(const char* path) : type(Char), string(path) {} Path(const char* path) : type(LowPathType::Char), string(path) {}
Path(std::vector<u8> binary_data) : type(Binary), binary(std::move(binary_data)) {} Path(std::vector<u8> binary_data) : type(LowPathType::Binary), binary(std::move(binary_data)) {}
Path(LowPathType type, u32 size, u32 pointer); Path(LowPathType type, u32 size, u32 pointer);
LowPathType GetType() const { LowPathType GetType() const {

View File

@ -22,7 +22,7 @@ namespace {
template <typename T> template <typename T>
ResultVal<std::tuple<MediaType, u64>> ParsePath(const Path& path, T program_id_reader) { ResultVal<std::tuple<MediaType, u64>> ParsePath(const Path& path, T program_id_reader) {
if (path.GetType() != Binary) { if (path.GetType() != LowPathType::Binary) {
LOG_ERROR(Service_FS, "Wrong path type %d", static_cast<int>(path.GetType())); LOG_ERROR(Service_FS, "Wrong path type %d", static_cast<int>(path.GetType()));
return ERROR_INVALID_PATH; return ERROR_INVALID_PATH;
} }