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"
|
2015-05-13 03:38:29 +02:00
|
|
|
#include "core/memory.h"
|
2015-05-06 07:29:11 +02:00
|
|
|
|
|
|
|
namespace FileSys {
|
|
|
|
|
|
|
|
Path::Path(LowPathType type, u32 size, u32 pointer) : type(type) {
|
|
|
|
switch (type) {
|
2016-09-18 02:38:01 +02:00
|
|
|
case Binary: {
|
2016-04-16 11:01:43 +02:00
|
|
|
binary.resize(size);
|
|
|
|
Memory::ReadBlock(pointer, binary.data(), binary.size());
|
2015-05-06 07:29:11 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
case Char: {
|
2016-04-16 11:01:43 +02:00
|
|
|
string.resize(size - 1); // Data is always null-terminated.
|
|
|
|
Memory::ReadBlock(pointer, &string[0], string.size());
|
2015-05-06 07:29:11 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
case Wchar: {
|
2016-04-16 11:01:43 +02:00
|
|
|
u16str.resize(size / 2 - 1); // Data is always null-terminated.
|
|
|
|
Memory::ReadBlock(pointer, &u16str[0], u16str.size() * sizeof(char16_t));
|
2015-05-06 07:29:11 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-25 06:10:05 +01:00
|
|
|
std::string Path::DebugStr() const {
|
2015-05-06 07:29:11 +02:00
|
|
|
switch (GetType()) {
|
|
|
|
case Invalid:
|
|
|
|
default:
|
|
|
|
return "[Invalid]";
|
|
|
|
case Empty:
|
|
|
|
return "[Empty]";
|
2016-09-18 02:38:01 +02:00
|
|
|
case 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 Char:
|
|
|
|
return "[Char: " + AsString() + ']';
|
|
|
|
case Wchar:
|
|
|
|
return "[Wchar: " + AsString() + ']';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-25 06:10:05 +01:00
|
|
|
std::string Path::AsString() const {
|
2015-05-06 07:29:11 +02:00
|
|
|
switch (GetType()) {
|
|
|
|
case Char:
|
|
|
|
return string;
|
|
|
|
case Wchar:
|
|
|
|
return Common::UTF16ToUTF8(u16str);
|
|
|
|
case Empty:
|
2016-09-18 02:38:01 +02:00
|
|
|
return {};
|
2015-05-06 07:29:11 +02:00
|
|
|
case Invalid:
|
|
|
|
case Binary:
|
|
|
|
default:
|
|
|
|
// TODO(yuriks): Add assert
|
|
|
|
LOG_ERROR(Service_FS, "LowPathType cannot be converted to string!");
|
2016-09-18 02:38:01 +02:00
|
|
|
return {};
|
2015-05-06 07:29:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-25 06:10:05 +01:00
|
|
|
std::u16string Path::AsU16Str() const {
|
2015-05-06 07:29:11 +02:00
|
|
|
switch (GetType()) {
|
|
|
|
case Char:
|
|
|
|
return Common::UTF8ToUTF16(string);
|
|
|
|
case Wchar:
|
|
|
|
return u16str;
|
|
|
|
case Empty:
|
2016-09-18 02:38:01 +02:00
|
|
|
return {};
|
2015-05-06 07:29:11 +02:00
|
|
|
case Invalid:
|
|
|
|
case Binary:
|
|
|
|
// TODO(yuriks): Add assert
|
|
|
|
LOG_ERROR(Service_FS, "LowPathType cannot be converted to u16string!");
|
2016-09-18 02:38:01 +02:00
|
|
|
return {};
|
2015-05-06 07:29:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-25 06:10:05 +01:00
|
|
|
std::vector<u8> Path::AsBinary() const {
|
2015-05-06 07:29:11 +02:00
|
|
|
switch (GetType()) {
|
|
|
|
case Binary:
|
|
|
|
return binary;
|
|
|
|
case Char:
|
|
|
|
return std::vector<u8>(string.begin(), string.end());
|
2016-09-18 02:38:01 +02:00
|
|
|
case 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 (size_t i = 0; i < u16str.size(); ++i) {
|
|
|
|
u16 tmp_char = u16str.at(i);
|
2016-09-18 02:38:01 +02:00
|
|
|
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 Empty:
|
2016-09-18 02:38:01 +02:00
|
|
|
return {};
|
2015-05-06 07:29:11 +02:00
|
|
|
case Invalid:
|
|
|
|
default:
|
|
|
|
// TODO(yuriks): Add assert
|
|
|
|
LOG_ERROR(Service_FS, "LowPathType cannot be converted to binary!");
|
2016-09-18 02:38:01 +02:00
|
|
|
return {};
|
2015-05-06 07:29:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|