2015-05-06 05:29:11 +00:00
|
|
|
// Copyright 2015 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2015-06-21 14:44:11 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <iomanip>
|
2015-05-06 05:29:11 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "common/string_util.h"
|
2016-09-21 06:52:38 +00:00
|
|
|
#include "core/file_sys/archive_backend.h"
|
2015-05-13 01:38:29 +00:00
|
|
|
#include "core/memory.h"
|
2015-05-06 05:29:11 +00:00
|
|
|
|
|
|
|
namespace FileSys {
|
|
|
|
|
2018-02-15 09:19:01 +00:00
|
|
|
Path::Path(LowPathType type, const std::vector<u8>& data) : type(type) {
|
2015-05-06 05:29:11 +00:00
|
|
|
switch (type) {
|
2017-12-12 22:41:36 +00:00
|
|
|
case LowPathType::Binary: {
|
2018-02-15 09:19:01 +00:00
|
|
|
binary = data;
|
2015-05-06 05:29:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-12-12 22:41:36 +00:00
|
|
|
case LowPathType::Char: {
|
2018-02-15 09:19:01 +00:00
|
|
|
string.resize(data.size() - 1); // Data is always null-terminated.
|
|
|
|
std::memcpy(string.data(), data.data(), string.size());
|
2015-05-06 05:29:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-12-12 22:41:36 +00:00
|
|
|
case LowPathType::Wchar: {
|
2018-02-15 09:19:01 +00:00
|
|
|
u16str.resize(data.size() / 2 - 1); // Data is always null-terminated.
|
|
|
|
std::memcpy(u16str.data(), data.data(), u16str.size() * sizeof(char16_t));
|
2015-05-06 05:29:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-25 05:10:05 +00:00
|
|
|
std::string Path::DebugStr() const {
|
2015-05-06 05:29:11 +00:00
|
|
|
switch (GetType()) {
|
2017-12-12 22:41:36 +00:00
|
|
|
case LowPathType::Invalid:
|
2015-05-06 05:29:11 +00:00
|
|
|
default:
|
|
|
|
return "[Invalid]";
|
2017-12-12 22:41:36 +00:00
|
|
|
case LowPathType::Empty:
|
2015-05-06 05:29:11 +00:00
|
|
|
return "[Empty]";
|
2017-12-12 22:41:36 +00:00
|
|
|
case LowPathType::Binary: {
|
2015-05-06 05:29:11 +00:00
|
|
|
std::stringstream res;
|
|
|
|
res << "[Binary: ";
|
|
|
|
for (unsigned byte : binary)
|
|
|
|
res << std::hex << std::setw(2) << std::setfill('0') << byte;
|
|
|
|
res << ']';
|
|
|
|
return res.str();
|
|
|
|
}
|
2017-12-12 22:41:36 +00:00
|
|
|
case LowPathType::Char:
|
2015-05-06 05:29:11 +00:00
|
|
|
return "[Char: " + AsString() + ']';
|
2017-12-12 22:41:36 +00:00
|
|
|
case LowPathType::Wchar:
|
2015-05-06 05:29:11 +00:00
|
|
|
return "[Wchar: " + AsString() + ']';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-25 05:10:05 +00:00
|
|
|
std::string Path::AsString() const {
|
2015-05-06 05:29:11 +00:00
|
|
|
switch (GetType()) {
|
2017-12-12 22:41:36 +00:00
|
|
|
case LowPathType::Char:
|
2015-05-06 05:29:11 +00:00
|
|
|
return string;
|
2017-12-12 22:41:36 +00:00
|
|
|
case LowPathType::Wchar:
|
2015-05-06 05:29:11 +00:00
|
|
|
return Common::UTF16ToUTF8(u16str);
|
2017-12-12 22:41:36 +00:00
|
|
|
case LowPathType::Empty:
|
2016-09-18 00:38:01 +00:00
|
|
|
return {};
|
2017-12-12 22:41:36 +00:00
|
|
|
case LowPathType::Invalid:
|
|
|
|
case LowPathType::Binary:
|
2015-05-06 05:29:11 +00:00
|
|
|
default:
|
|
|
|
// TODO(yuriks): Add assert
|
|
|
|
LOG_ERROR(Service_FS, "LowPathType cannot be converted to string!");
|
2016-09-18 00:38:01 +00:00
|
|
|
return {};
|
2015-05-06 05:29:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-25 05:10:05 +00:00
|
|
|
std::u16string Path::AsU16Str() const {
|
2015-05-06 05:29:11 +00:00
|
|
|
switch (GetType()) {
|
2017-12-12 22:41:36 +00:00
|
|
|
case LowPathType::Char:
|
2015-05-06 05:29:11 +00:00
|
|
|
return Common::UTF8ToUTF16(string);
|
2017-12-12 22:41:36 +00:00
|
|
|
case LowPathType::Wchar:
|
2015-05-06 05:29:11 +00:00
|
|
|
return u16str;
|
2017-12-12 22:41:36 +00:00
|
|
|
case LowPathType::Empty:
|
2016-09-18 00:38:01 +00:00
|
|
|
return {};
|
2017-12-12 22:41:36 +00:00
|
|
|
case LowPathType::Invalid:
|
|
|
|
case LowPathType::Binary:
|
2015-05-06 05:29:11 +00:00
|
|
|
// TODO(yuriks): Add assert
|
|
|
|
LOG_ERROR(Service_FS, "LowPathType cannot be converted to u16string!");
|
2016-09-18 00:38:01 +00:00
|
|
|
return {};
|
2015-05-06 05:29:11 +00:00
|
|
|
}
|
2017-08-19 17:04:40 +00:00
|
|
|
|
|
|
|
UNREACHABLE();
|
2015-05-06 05:29:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-25 05:10:05 +00:00
|
|
|
std::vector<u8> Path::AsBinary() const {
|
2015-05-06 05:29:11 +00:00
|
|
|
switch (GetType()) {
|
2017-12-12 22:41:36 +00:00
|
|
|
case LowPathType::Binary:
|
2015-05-06 05:29:11 +00:00
|
|
|
return binary;
|
2017-12-12 22:41:36 +00:00
|
|
|
case LowPathType::Char:
|
2015-05-06 05:29:11 +00:00
|
|
|
return std::vector<u8>(string.begin(), string.end());
|
2017-12-12 22:41:36 +00:00
|
|
|
case LowPathType::Wchar: {
|
2015-05-06 05:29:11 +00:00
|
|
|
// use two u8 for each character of u16str
|
|
|
|
std::vector<u8> to_return(u16str.size() * 2);
|
|
|
|
for (size_t i = 0; i < u16str.size(); ++i) {
|
|
|
|
u16 tmp_char = u16str.at(i);
|
2016-09-18 00:38:01 +00:00
|
|
|
to_return[i * 2] = (tmp_char & 0xFF00) >> 8;
|
|
|
|
to_return[i * 2 + 1] = (tmp_char & 0x00FF);
|
2015-05-06 05:29:11 +00:00
|
|
|
}
|
|
|
|
return to_return;
|
|
|
|
}
|
2017-12-12 22:41:36 +00:00
|
|
|
case LowPathType::Empty:
|
2016-09-18 00:38:01 +00:00
|
|
|
return {};
|
2017-12-12 22:41:36 +00:00
|
|
|
case LowPathType::Invalid:
|
2015-05-06 05:29:11 +00:00
|
|
|
default:
|
|
|
|
// TODO(yuriks): Add assert
|
|
|
|
LOG_ERROR(Service_FS, "LowPathType cannot be converted to binary!");
|
2016-09-18 00:38:01 +00:00
|
|
|
return {};
|
2015-05-06 05:29:11 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-09 17:54:43 +00:00
|
|
|
} // namespace FileSys
|