forked from pioneerspacesim/pioneer
/
FileSystem.cpp
280 lines (247 loc) · 6.7 KB
/
FileSystem.cpp
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include "libs.h"
#include "FileSystem.h"
#include "StringRange.h"
#include <cassert>
#include <algorithm>
#include <iterator>
#include <stdexcept>
namespace FileSystem {
static FileSourceFS dataFilesApp(GetDataDir());
static FileSourceFS dataFilesUser(GetUserDir("data"));
FileSourceUnion gameDataFiles;
FileSourceFS rawFileSystem("/");
// note: some functions (GetUserDir(), GetDataDir()) are in FileSystem{Posix,Win32}.cpp
std::string JoinPath(const std::string &a, const std::string &b)
{
if (!b.empty()) {
if (b[0] == '/' || a.empty())
return b;
else
return a + "/" + b;
} else
return a;
}
static void normalise_path(std::string &result, const StringRange &path)
{
StringRange part(path.begin, path.begin);
const char *c = path.begin;
if ((c != path.end) && (*c == '/')) {
result += '/';
++c;
++part.begin;
}
const size_t initial_result_length = result.size();
while (true) {
if ((*c == '/') || (c == path.end)) {
part.end = c;
if (part.Empty() || (part == ".")) {
// skip this part
} else if (part == "..") {
// pop the last component
if (result.size() <= initial_result_length)
throw std::invalid_argument(path.ToString());
size_t pos = result.rfind('/', result.size()-2);
++pos;
assert(pos >= initial_result_length);
result.erase(pos);
} else {
// push the new component
if (part.end != path.end) {
assert(*part.end == '/');
++part.end;
}
result.append(part.begin, part.Size());
}
part.begin = c+1;
}
if (c == path.end) { break; }
++c;
}
}
std::string NormalisePath(const std::string &path)
{
std::string result;
result.reserve(path.size());
normalise_path(result, StringRange(path.c_str(), path.size()));
return result;
}
std::string JoinPathBelow(const std::string &base, const std::string &path)
{
if (base.empty())
return path;
if (!path.empty()) {
if ((path[0] == '/') && (base != "/"))
throw std::invalid_argument(path);
else {
std::string result(base);
result.reserve(result.size() + 1 + path.size());
if (result[result.size()-1] != '/')
result += '/';
StringRange rhs(path.c_str(), path.size());
if (path[0] == '/') {
assert(base == "/");
++rhs.begin;
}
normalise_path(result, rhs);
return result;
}
} else
return base;
}
void Init()
{
gameDataFiles.AppendSource(&dataFilesUser);
gameDataFiles.AppendSource(&dataFilesApp);
}
void Uninit()
{
}
FileInfo::FileInfo(FileSource *source, const std::string &path, FileType type):
m_source(source),
m_path(path),
m_dirLen(0),
m_type(type)
{
std::size_t slashpos = m_path.rfind('/');
if (slashpos != std::string::npos) {
m_dirLen = slashpos + 1;
} else {
m_dirLen = 0;
}
}
FileInfo FileSource::MakeFileInfo(const std::string &path, FileInfo::FileType fileType)
{
return FileInfo(this, path, fileType);
}
FileSourceUnion::FileSourceUnion(): FileSource(":union:") {}
FileSourceUnion::~FileSourceUnion() {}
void FileSourceUnion::PrependSource(FileSource *fs)
{
assert(fs);
RemoveSource(fs);
m_sources.insert(m_sources.begin(), fs);
}
void FileSourceUnion::AppendSource(FileSource *fs)
{
assert(fs);
RemoveSource(fs);
m_sources.push_back(fs);
}
void FileSourceUnion::RemoveSource(FileSource *fs)
{
std::vector<FileSource*>::iterator nend = std::remove(m_sources.begin(), m_sources.end(), fs);
m_sources.erase(nend, m_sources.end());
}
FileInfo FileSourceUnion::Lookup(const std::string &path)
{
for (std::vector<FileSource*>::const_iterator
it = m_sources.begin(); it != m_sources.end(); ++it)
{
FileInfo info = (*it)->Lookup(path);
if (info.Exists()) { return info; }
}
return MakeFileInfo(path, FileInfo::FT_NON_EXISTENT);
}
RefCountedPtr<FileData> FileSourceUnion::ReadFile(const std::string &path)
{
for (std::vector<FileSource*>::const_iterator
it = m_sources.begin(); it != m_sources.end(); ++it)
{
RefCountedPtr<FileData> data = (*it)->ReadFile(path);
if (data) { return data; }
}
return RefCountedPtr<FileData>();
}
static void file_union_merge(
std::vector<FileInfo>::const_iterator a, std::vector<FileInfo>::const_iterator aend,
std::vector<FileInfo>::const_iterator b, std::vector<FileInfo>::const_iterator bend,
std::vector<FileInfo> &output)
{
while ((a != aend) && (b != bend)) {
int order = a->GetPath().compare(b->GetPath());
int which = order;
if (which == 0) {
if (b->IsDir() && !a->IsDir()) { which = 1; }
else { which = -1; }
}
if (which < 0) {
output.push_back(*a++);
if (order == 0) ++b;
} else {
output.push_back(*b++);
if (order == 0) ++a;
}
}
if (a != aend) { std::copy(a, aend, std::back_inserter(output)); }
if (b != bend) { std::copy(b, bend, std::back_inserter(output)); }
}
bool FileSourceUnion::ReadDirectory(const std::string &path, std::vector<FileInfo> &output)
{
if (m_sources.empty()) {
return false;
}
if (m_sources.size() == 1) {
return m_sources.front()->ReadDirectory(path, output);
}
bool founddir = false;
size_t headsize = output.size();
std::vector<FileInfo> merged;
for (std::vector<FileSource*>::const_iterator
it = m_sources.begin(); it != m_sources.end(); ++it)
{
size_t prevsize = output.size();
assert(prevsize >= headsize);
std::vector<FileInfo> temp1;
if ((*it)->ReadDirectory(path, temp1))
founddir = true;
std::vector<FileInfo> temp2;
temp2.swap(merged);
file_union_merge(
temp1.begin(), temp1.end(),
temp2.begin(), temp2.end(),
merged);
}
output.reserve(output.size() + merged.size());
std::copy(merged.begin(), merged.end(), std::back_inserter(output));
return founddir;
}
FileEnumerator::FileEnumerator(FileSource &fs, int flags):
m_source(&fs), m_flags(flags)
{
Init("/");
}
FileEnumerator::FileEnumerator(FileSource &fs, const std::string &path, int flags):
m_source(&fs), m_flags(flags)
{
Init(path);
}
FileEnumerator::~FileEnumerator() {}
void FileEnumerator::Init(const std::string &path)
{
FileInfo fi = m_source->Lookup(path);
if (fi.IsDir()) {
m_queue.push_back(fi);
Next(m_flags | Recurse);
}
}
void FileEnumerator::Next(int flags)
{
if (flags & RecurseFlag) {
FileInfo head = m_queue.front();
m_queue.pop_front();
if (head.IsDir()) {
std::vector<FileInfo> entries;
m_source->ReadDirectory(head.GetPath(), entries);
for (std::vector<FileInfo>::const_iterator
it = entries.begin(); it != entries.end(); ++it) {
if ((flags & IncludeDirectories) && it->IsDir())
m_queue.push_back(*it);
if (!(flags & ExcludeFiles) && it->IsFile())
m_queue.push_back(*it);
}
}
} else {
m_queue.pop_front();
}
}
} // namespace FileSystem