summaryrefslogtreecommitdiffstats
path: root/src/core/file_sys/vfs_concat.cpp
blob: 88a9a5259337940505eb8ad4dd6750dbc4b88dbd (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
// Copyright 2018 yuzu emulator team
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include <algorithm>
#include <utility>

#include "core/file_sys/vfs_concat.h"

namespace FileSys {

VirtualFile ConcatenateFiles(std::vector<VirtualFile> files, std::string name) {
    if (files.empty())
        return nullptr;
    if (files.size() == 1)
        return files[0];

    return std::shared_ptr<VfsFile>(new ConcatenatedVfsFile(std::move(files), std::move(name)));
}

ConcatenatedVfsFile::ConcatenatedVfsFile(std::vector<VirtualFile> files_, std::string name)
    : name(std::move(name)) {
    size_t next_offset = 0;
    for (const auto& file : files_) {
        files[next_offset] = file;
        next_offset += file->GetSize();
    }
}

std::string ConcatenatedVfsFile::GetName() const {
    if (files.empty())
        return "";
    if (!name.empty())
        return name;
    return files.begin()->second->GetName();
}

size_t ConcatenatedVfsFile::GetSize() const {
    if (files.empty())
        return 0;
    return files.rbegin()->first + files.rbegin()->second->GetSize();
}

bool ConcatenatedVfsFile::Resize(size_t new_size) {
    return false;
}

std::shared_ptr<VfsDirectory> ConcatenatedVfsFile::GetContainingDirectory() const {
    if (files.empty())
        return nullptr;
    return files.begin()->second->GetContainingDirectory();
}

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

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

size_t ConcatenatedVfsFile::Read(u8* data, size_t length, size_t offset) const {
    auto entry = files.end();
    for (auto iter = files.begin(); iter != files.end(); ++iter) {
        if (iter->first > offset) {
            entry = --iter;
            break;
        }
    }

    if (entry == files.end() && offset < files.rbegin()->first + files.rbegin()->second->GetSize())
        --entry;

    if (entry == files.end())
        return 0;

    const auto remaining = entry->second->GetSize() + offset - entry->first;
    if (length > remaining) {
        return entry->second->Read(data, remaining, offset - entry->first) +
               Read(data + remaining, length - remaining, offset + remaining);
    }

    return entry->second->Read(data, length, offset - entry->first);
}

size_t ConcatenatedVfsFile::Write(const u8* data, size_t length, size_t offset) {
    return 0;
}

bool ConcatenatedVfsFile::Rename(std::string_view name) {
    return false;
}
} // namespace FileSys