summaryrefslogtreecommitdiffhomepage
path: root/StarsEx/DataSource.cpp
blob: 3ace2ca979922d5289944fce50d9628fee72d9d6 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*  Starshatter: The Open Source Project
    Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors
    Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
    Copyright (c) 1997-2006, Destroyer Studios LLC.
*/

#include "DataSource.h"

#include <cstdint>
#include <cstdio>
#include <cstring>
#include <filesystem>

#include "Archive.h"
#include "List.h"
#include "Text.h"
#include "Utils.h"


int DataSource::s_next_id {0};


DataSource::DataSource() :
    m_id {s_next_id++}
{
}


DataSource::~DataSource()
{
}


int
DataSource::Id() const
{
    return m_id;
}


ArchiveDataSource::ArchiveDataSource(const char* name) :
    ArchiveDataSource(new DataArchive(name))
{
}


ArchiveDataSource::ArchiveDataSource(DataArchive* archive) :
    DataSource(),
    m_archive {archive}
{
}


ArchiveDataSource::~ArchiveDataSource()
{
    delete m_archive;
}


bool
ArchiveDataSource::Find(const Text& prefix, const char* name) const
{
    const int index = m_archive->FindEntry(prefix.concat(name));
    return index > -1;
}


int
ArchiveDataSource::ListFiles(const Text& prefix, Text filter, List<Text>& items, bool recurse) const
{
    (void) recurse;  // Lookup in DataArchives was always recursive so far
    filter = filter.replace("*", "");  // Wildcards worked only on boundaries
    const int first = prefix.length();
    const int count = m_archive->NumFiles();
    for (int i = 0; i < count; ++i) {
        const auto* entry = m_archive->GetFile(i);
        Text name = entry->name;
        name.setSensitive(false);
        if (name.contains(prefix) && name.contains(filter)) {
            const auto without_prefix = name.substring(first, name.length());
            if (!items.contains(&without_prefix))
                items.append(new Text(without_prefix));
        }
    }
    return items.size();
}


int
ArchiveDataSource::Load(const Text& prefix, const char* name, std::uint8_t*& buf, bool null_terminate) const
{
    const int index = m_archive->FindEntry(prefix.concat(name));
    if (index > -1)
        return m_archive->ExpandEntry(index, buf, null_terminate);
    return 0;  // -1 would be preferable, but 0 is from legacy
}


FileSystemDataSource::FileSystemDataSource(const char* path) :
    DataSource(),
    m_path {path}
{
}


FileSystemDataSource::~FileSystemDataSource()
{
}


bool
FileSystemDataSource::Find(const Text& prefix, const char* name) const
{
    std::filesystem::path full_path {m_path};
    full_path.append(prefix.data());
    full_path.append(name);
    return std::filesystem::is_regular_file(full_path);
}


int
FileSystemDataSource::ListFiles(const Text& prefix, Text filter, List<Text>& items, bool recurse) const
{
    std::filesystem::path full_path {m_path};
    full_path.append(prefix.data());
    filter = filter.replace("*", "");
    const auto first = full_path.string().length();
    const auto check = [&](const std::filesystem::directory_entry& entry){
        const auto filename = entry.path().filename().string();  // more of a reason to switch to string soon
        const auto index = filename.find(filter.data());
        if (index != decltype(filename)::npos) {
            Text path = entry.path().string().c_str();
            items.append(new Text(path.substring(first, path.length())));
        }
    };
    try {
        if (recurse) {
            for (const auto& entry : std::filesystem::recursive_directory_iterator(full_path))
                check(entry);
        }
        else {
            for (const auto& entry : std::filesystem::directory_iterator(full_path))
                check(entry);
        }
    }
    catch (const std::filesystem::filesystem_error& err) {
        Print("FS::ListFiles: %s\n", err.what());
    }
    return items.size();
}


int
FileSystemDataSource::Load(const Text& prefix, const char* name, std::uint8_t*& buf, bool null_terminate) const
{
    std::filesystem::path full_path {m_path};
    full_path.append(prefix.data());
    full_path.append(name);
    auto f = std::fopen(full_path.string().c_str(), "rb");
    if (!f)
        return 0;  // Again, -1 would be better to differentiate an error from an empty file
    std::fseek(f, 0, SEEK_END);
    int length = std::ftell(f);
    std::fseek(f, 0, SEEK_SET);
    if (null_terminate) {
        buf = new std::uint8_t[length + 1];
        if (buf)
            buf[length] = 0;
    }
    else {
        buf = new std::uint8_t[length];
    }
    int bytes = 0;
    if (buf)
        bytes = std::fread(buf, length, 1, f);
    std::fclose(f);
    return bytes;
}