aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/file_sys/archive_backend.cpp
blob: 45a559ce8757852ca45917cd27f63a1c1c2aa658 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2015 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include <sstream>

#include "common/logging/log.h"
#include "common/string_util.h"

#include "core/file_sys/archive_backend.h"
#include "core/memory.h"


namespace FileSys {

Path::Path(LowPathType type, u32 size, u32 pointer) : type(type) {
    switch (type) {
    case Binary:
    {
        u8* data = Memory::GetPointer(pointer);
        binary = std::vector<u8>(data, data + size);
        break;
    }

    case Char:
    {
        const char* data = reinterpret_cast<const char*>(Memory::GetPointer(pointer));
        string = std::string(data, size - 1); // Data is always null-terminated.
        break;
    }

    case Wchar:
    {
        const char16_t* data = reinterpret_cast<const char16_t*>(Memory::GetPointer(pointer));
        u16str = std::u16string(data, size/2 - 1); // Data is always null-terminated.
        break;
    }

    default:
        break;
    }
}

const std::string Path::DebugStr() const {
    switch (GetType()) {
    case Invalid:
    default:
        return "[Invalid]";
    case Empty:
        return "[Empty]";
    case Binary:
    {
        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() + ']';
    }
}

const std::string Path::AsString() const {
    switch (GetType()) {
    case Char:
        return string;
    case Wchar:
        return Common::UTF16ToUTF8(u16str);
    case Empty:
        return{};
    case Invalid:
    case Binary:
    default:
        // TODO(yuriks): Add assert
        LOG_ERROR(Service_FS, "LowPathType cannot be converted to string!");
        return{};
    }
}

const std::u16string Path::AsU16Str() const {
    switch (GetType()) {
    case Char:
        return Common::UTF8ToUTF16(string);
    case Wchar:
        return u16str;
    case Empty:
        return{};
    case Invalid:
    case Binary:
        // TODO(yuriks): Add assert
        LOG_ERROR(Service_FS, "LowPathType cannot be converted to u16string!");
        return{};
    }
}

const std::vector<u8> Path::AsBinary() const {
    switch (GetType()) {
    case Binary:
        return binary;
    case Char:
        return std::vector<u8>(string.begin(), string.end());
    case Wchar:
    {
        // 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);
            to_return[i*2] = (tmp_char & 0xFF00) >> 8;
            to_return[i*2 + 1] = (tmp_char & 0x00FF);
        }
        return to_return;
    }
    case Empty:
        return{};
    case Invalid:
    default:
        // TODO(yuriks): Add assert
        LOG_ERROR(Service_FS, "LowPathType cannot be converted to binary!");
        return{};
    }
}

}