citra/src/core/file_sys/archive_backend.cpp

122 lines
3.2 KiB
C++
Raw Normal View History

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