summaryrefslogtreecommitdiffstats
path: root/src/core/file_sys/vfs/vfs_vector.cpp
blob: 0d54461c8fc6b455fdd48e39bbe6bca4ae5b83f0 (plain) (blame)
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
128
129
130
131
132
133
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <algorithm>
#include <utility>
#include "core/file_sys/vfs/vfs_vector.h"

namespace FileSys {
VectorVfsFile::VectorVfsFile(std::vector<u8> initial_data, std::string name_, VirtualDir parent_)
    : data(std::move(initial_data)), parent(std::move(parent_)), name(std::move(name_)) {}

VectorVfsFile::~VectorVfsFile() = default;

std::string VectorVfsFile::GetName() const {
    return name;
}

size_t VectorVfsFile::GetSize() const {
    return data.size();
}

bool VectorVfsFile::Resize(size_t new_size) {
    data.resize(new_size);
    return true;
}

VirtualDir VectorVfsFile::GetContainingDirectory() const {
    return parent;
}

bool VectorVfsFile::IsWritable() const {
    return true;
}

bool VectorVfsFile::IsReadable() const {
    return true;
}

std::size_t VectorVfsFile::Read(u8* data_, std::size_t length, std::size_t offset) const {
    const auto read = std::min(length, data.size() - offset);
    std::memcpy(data_, data.data() + offset, read);
    return read;
}

std::size_t VectorVfsFile::Write(const u8* data_, std::size_t length, std::size_t offset) {
    if (offset + length > data.size())
        data.resize(offset + length);
    const auto write = std::min(length, data.size() - offset);
    std::memcpy(data.data() + offset, data_, write);
    return write;
}

bool VectorVfsFile::Rename(std::string_view name_) {
    name = name_;
    return true;
}

void VectorVfsFile::Assign(std::vector<u8> new_data) {
    data = std::move(new_data);
}

VectorVfsDirectory::VectorVfsDirectory(std::vector<VirtualFile> files_,
                                       std::vector<VirtualDir> dirs_, std::string name_,
                                       VirtualDir parent_)
    : files(std::move(files_)), dirs(std::move(dirs_)), parent(std::move(parent_)),
      name(std::move(name_)) {}

VectorVfsDirectory::~VectorVfsDirectory() = default;

std::vector<VirtualFile> VectorVfsDirectory::GetFiles() const {
    return files;
}

std::vector<VirtualDir> VectorVfsDirectory::GetSubdirectories() const {
    return dirs;
}

bool VectorVfsDirectory::IsWritable() const {
    return false;
}

bool VectorVfsDirectory::IsReadable() const {
    return true;
}

std::string VectorVfsDirectory::GetName() const {
    return name;
}

VirtualDir VectorVfsDirectory::GetParentDirectory() const {
    return parent;
}

template <typename T>
static bool FindAndRemoveVectorElement(std::vector<T>& vec, std::string_view name) {
    const auto iter =
        std::find_if(vec.begin(), vec.end(), [name](const T& e) { return e->GetName() == name; });
    if (iter == vec.end())
        return false;

    vec.erase(iter);
    return true;
}

bool VectorVfsDirectory::DeleteSubdirectory(std::string_view subdir_name) {
    return FindAndRemoveVectorElement(dirs, subdir_name);
}

bool VectorVfsDirectory::DeleteFile(std::string_view file_name) {
    return FindAndRemoveVectorElement(files, file_name);
}

bool VectorVfsDirectory::Rename(std::string_view name_) {
    name = name_;
    return true;
}

VirtualDir VectorVfsDirectory::CreateSubdirectory(std::string_view subdir_name) {
    return nullptr;
}

VirtualFile VectorVfsDirectory::CreateFile(std::string_view file_name) {
    return nullptr;
}

void VectorVfsDirectory::AddFile(VirtualFile file) {
    files.push_back(std::move(file));
}

void VectorVfsDirectory::AddDirectory(VirtualDir dir) {
    dirs.push_back(std::move(dir));
}
} // namespace FileSys