summaryrefslogtreecommitdiffhomepage
path: root/StarsEx/DataSource.cpp
blob: b587971d07df832572dcdc21f747629f317733c4 (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
/*  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 <cstring>
#include <filesystem>

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


DataSource::DataSource() :
    m_prefix {""}
{
}


DataSource::~DataSource()
{
}


void
DataSource::SetPrefix(const char* prefix)
{
    if (prefix)
        m_prefix = prefix;
    else
        m_prefix = "";
}


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


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


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


bool
ArchiveDataSource::Find(const char* name) const
{
    const int index = m_archive->FindEntry(name);
    return index > -1;
}


int
ArchiveDataSource::ListFiles(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 prefix = m_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(m_prefix) && name.contains(filter)) {
            const auto without_prefix = name.substring(prefix, name.length());
            if (!items.contains(&without_prefix))
                items.append(new Text(without_prefix));
        }
    }
    return items.size();
}


int
ArchiveDataSource::Load(const char* name, std::uint8_t*& buf, bool null_terminate) const
{
    const int index = m_archive->FindEntry(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) :
    m_path {path}
{
}


FileSystemDataSource::~FileSystemDataSource()
{
}


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


int
FileSystemDataSource::ListFiles(Text filter, List<Text>& items, bool recurse) const
{
    std::filesystem::path full_path {m_path};
    full_path.append(m_prefix.data());
    filter = filter.replace("*", "");
    const auto check = [&items, &filter](const std::filesystem::directory_entry& entry){
        const auto filename = entry.path().filename().string();
        const auto index = filename.find(filter.data());
        if (index != decltype(filename)::npos)
            items.append(new Text(entry.path().string().c_str()));
    };
    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);
    }
    return items.size();
}


int
FileSystemDataSource::Load(const char* name, std::uint8_t*& buf, bool null_terminate) const
{
    std::filesystem::path full_path {m_path};
    full_path.append(m_prefix.data());
    full_path.append(name);
    FILE* f = fopen(full_path.string().c_str(), "rb");
    if (!f)
        return 0;  // Again, -1 would be better to differentiate an error from an empty file
    fseek(f, 0, SEEK_END);
    int length = ftell(f);
    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 = fread(buf, length, 1, f);
    fclose(f);
    return bytes;
}