/
filedirectory.cpp
577 lines (489 loc) · 17.3 KB
/
filedirectory.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/**
* @file filedirectory.cpp
* FileDirectory implementation. @ingroup fs
*
* @authors Copyright © 2003-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2012 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include "de_base.h"
#include "de_console.h"
#include "de_filesys.h"
#include "timer.h"
#include "filedirectory.h"
#include <de/memory.h>
struct FileDirectoryNodeInfo
{
bool processed;
FileDirectoryNodeInfo() : processed(false) {}
};
static int addPathNodesAndMaybeDescendBranch(int flags, const ddstring_t* filePath,
PathDirectoryNodeType nodeType, void* parameters);
static de::PathDirectoryNode* attachMissingNodeInfo(de::PathDirectoryNode* node)
{
if(!node) return NULL;
// Has this already been processed?
FileDirectoryNodeInfo* info = reinterpret_cast<FileDirectoryNodeInfo*>(node->userData());
if(!info)
{
// Clearly not. Attach new node info.
node->setUserData(new FileDirectoryNodeInfo());
}
return node;
}
de::FileDirectory::FileDirectory(const char* _basePath)
: PathDirectory(), basePath(0), baseNode(0)
{
if(_basePath && _basePath[0])
{
basePath = Str_Set(Str_NewStd(), _basePath);
// Ensure path is correctly formed.
F_AppendMissingSlash(basePath);
}
}
de::FileDirectory::~FileDirectory()
{
clearNodeInfo();
if(basePath) Str_Delete(basePath);
}
void de::FileDirectory::clearNodeInfo()
{
const PathNodes* nodes = pathNodes(PT_LEAF);
if(nodes)
DENG2_FOR_EACH(i, *nodes, PathNodes::const_iterator)
{
FileDirectoryNodeInfo* info = reinterpret_cast<FileDirectoryNodeInfo*>((*i)->userData());
if(info)
{
// Detach our user data from this node.
(*i)->setUserData(0);
delete info;
}
}
nodes = pathNodes(PT_BRANCH);
if(nodes)
DENG2_FOR_EACH(i, *nodes, PathNodes::const_iterator)
{
FileDirectoryNodeInfo* info = reinterpret_cast<FileDirectoryNodeInfo*>((*i)->userData());
if(info)
{
// Detach our user data from this node.
(*i)->setUserData(0);
delete info;
}
}
}
void de::FileDirectory::clear()
{
clearNodeInfo();
PathDirectory::clear();
baseNode = NULL;
}
bool de::FileDirectory::find(PathDirectoryNodeType nodeType,
const char* _searchPath, char searchDelimiter, ddstring_t* foundPath,
char foundDelimiter)
{
if(foundPath)
{
Str_Clear(foundPath);
}
if(!_searchPath || !_searchPath[0]) return false;
// Convert the raw path into one we can process.
ddstring_t searchPath;
Str_Init(&searchPath); Str_Set(&searchPath, _searchPath);
F_FixSlashes(&searchPath, &searchPath);
// Try to make it a relative path?
if(basePath && !F_IsAbsolute(&searchPath))
{
F_RemoveBasePath2(&searchPath, &searchPath, basePath);
}
// Perform the search.
int flags = (nodeType == PT_LEAF? PCF_NO_BRANCH : PCF_NO_LEAF) | PCF_MATCH_FULL;
const de::PathDirectoryNode* foundNode = PathDirectory::find(flags, Str_Text(&searchPath), searchDelimiter);
Str_Free(&searchPath);
// Does caller want to know the full path?
if(foundPath && foundNode)
{
foundNode->directory()->composePath(foundNode, foundPath, NULL, foundDelimiter);
}
return !!foundNode;
}
de::PathDirectoryNode* de::FileDirectory::addPathNodes(const ddstring_t* rawPath)
{
if(!rawPath || Str_IsEmpty(rawPath)) return NULL;
const ddstring_t* path;
ddstring_t buf;
if(basePath)
{
// Try to make it a relative path?
if(F_IsAbsolute(rawPath))
{
F_RemoveBasePath2(Str_InitStd(&buf), rawPath, basePath);
path = &buf;
}
else
{
path = rawPath;
}
// If this is equal to the base path, return that node.
if(Str_IsEmpty(path))
{
// Time to construct the relative base node?
// This node is purely symbolic, its only necessary for our internal use.
if(!baseNode)
{
baseNode = insert("./", '/');
attachMissingNodeInfo(baseNode);
}
if(path == &buf) Str_Free(&buf);
return baseNode;
}
}
else
{
// Do not add relative paths.
if(!F_IsAbsolute(rawPath))
{
return NULL;
}
}
de::PathDirectoryNode* node = insert(Str_Text(path), '/');
attachMissingNodeInfo(node);
if(path == &buf) Str_Free(&buf);
return node;
}
typedef struct {
de::FileDirectory* fileDirectory; ///< FileDirectory instance.
int flags; ///< @ref searchPathFlags
/// If not @c NULL the callback's logic dictates whether iteration continues.
int (*callback) (struct pathdirectorynode_s* node, void* parameters);
/// Passed to the callback.
void* parameters;
} addpathworker_paramaters_t;
static int addPathWorker(const ddstring_t* filePath, PathDirectoryNodeType nodeType,
void* parameters)
{
addpathworker_paramaters_t* p = (addpathworker_paramaters_t*)parameters;
return addPathNodesAndMaybeDescendBranch(p->flags, filePath, nodeType, parameters);
}
int de::FileDirectory::addChildNodes(de::PathDirectoryNode* node, int flags,
int (*callback) (struct pathdirectorynode_s* node, void* parameters), void* parameters)
{
int result = 0; // Continue iteration.
if(node && PT_BRANCH == node->type())
{
addpathworker_paramaters_t p;
ddstring_t searchPattern;
// Compose the search pattern.
Str_Init(&searchPattern);
node->directory()->composePath(node, &searchPattern, NULL, '/');
// We're interested in *everything*.
Str_AppendChar(&searchPattern, '*');
// Take a copy of the caller's iteration parameters.
p.fileDirectory = this;
p.callback = callback;
p.flags = flags;
p.parameters = parameters;
// Process this search.
result = F_AllResourcePaths2(Str_Text(&searchPattern), flags, addPathWorker, (void*)&p);
Str_Free(&searchPattern);
}
return result;
}
int de::FileDirectory::addPathNodesAndMaybeDescendBranch(bool descendBranches,
const ddstring_t* filePath, PathDirectoryNodeType nodeType,
int flags, int (*callback) (struct pathdirectorynode_s* node, void* parameters),
void* parameters)
{
DENG2_ASSERT(VALID_PATHDIRECTORYNODE_TYPE(nodeType));
int result = 0; // Continue iteration.
// Add this path to the directory.
de::PathDirectoryNode* node = addPathNodes(filePath);
if(node)
{
FileDirectoryNodeInfo* info = reinterpret_cast<FileDirectoryNodeInfo*>(node->userData());
if(PT_BRANCH == node->type())
{
// Descend into this subdirectory?
if(descendBranches)
{
if(info->processed)
{
// Does caller want to process it again?
if(callback)
{
const PathNodes* nodes = pathNodes(PT_LEAF);
if(nodes)
DENG2_FOR_EACH(i, *nodes, PathNodes::const_iterator)
{
if(node == (*i)->parent())
{
result = callback(reinterpret_cast<struct pathdirectorynode_s*>(*i), parameters);
if(result) break;
}
}
if(!result)
{
nodes = pathNodes(PT_BRANCH);
if(nodes)
DENG2_FOR_EACH(i, *nodes, PathNodes::const_iterator)
{
if(node == (*i)->parent())
{
result = callback(reinterpret_cast<struct pathdirectorynode_s*>(*i), parameters);
if(result) break;
}
}
}
}
}
else
{
result = addChildNodes(node, flags, callback, parameters);
// This node is now considered processed.
info->processed = true;
}
}
}
// Node is a leaf.
else if(callback)
{
result = callback(reinterpret_cast<struct pathdirectorynode_s*>(node), parameters);
// This node is now considered processed (if it wasn't already).
info->processed = true;
}
}
return result;
}
/**
* @param filePath Possibly-relative path to an element in the virtual file system.
* @param nodeType Type of element, either a branch (directory) or a leaf (file).
* @param parameters Caller's iteration parameters.
* @return Non-zero if iteration should stop else @c 0.
*/
static int addPathNodesAndMaybeDescendBranch(int flags, const ddstring_t* filePath,
PathDirectoryNodeType nodeType, void* parameters)
{
addpathworker_paramaters_t* p = (addpathworker_paramaters_t*)parameters;
DENG2_ASSERT(p);
return p->fileDirectory->addPathNodesAndMaybeDescendBranch(!(flags & SPF_NO_DESCEND),
filePath, nodeType,
p->flags, p->callback, p->parameters);
}
DENG_DEBUG_ONLY(
static void printUriList(const Uri* const* pathList, size_t pathCount, int indent)
{
if(!pathList) return;
const Uri* const* pathsIt = pathList;
for(size_t i = 0; i < pathCount && (*pathsIt); ++i, pathsIt++)
{
Uri_Print(*pathsIt, indent);
}
})
void de::FileDirectory::addPaths(int flags,
const Uri* const* searchPaths, uint searchPathsCount,
int (*callback) (struct pathdirectorynode_s* node, void* parameters), void* parameters)
{
if(!searchPaths || searchPathsCount == 0)
{
DEBUG_Message(("Warning: FileDirectory::AddPaths: Attempt to add zero-sized path list, ignoring.\n"));
return;
}
#if _DEBUG
VERBOSE2( Con_Message("Adding paths to FileDirectory...\n");
printUriList(searchPaths, searchPathsCount, 2/*indent*/) )
#endif
for(uint i = 0; i < searchPathsCount; ++i)
{
const ddstring_t* searchPath = Uri_ResolvedConst(searchPaths[i]);
if(!searchPath) continue;
// Add new nodes on this path and/or re-process previously seen nodes.
addPathNodesAndMaybeDescendBranch(true/*do descend*/, searchPath, PT_BRANCH,
flags, callback, parameters);
}
/*#if _DEBUG
debugPrint(fd);
#endif*/
}
void de::FileDirectory::addPathList(int flags, const char* pathList,
int (*callback) (struct pathdirectorynode_s*, void*), void* parameters)
{
Uri** paths = NULL;
size_t pathsCount = 0;
if(pathList && pathList[0])
paths = F_CreateUriList2(RC_UNKNOWN, pathList, &pathsCount);
addPaths(flags, (const Uri**)paths, (uint)pathsCount, callback, parameters);
if(paths) F_DestroyUriList(paths);
}
ddstring_t* de::FileDirectory::collectPaths(size_t* count, int flags, char delimiter)
{
return PathDirectory::collectPaths(count, flags, delimiter);
}
#if _DEBUG
/// @return Lexicographical delta between the two ddstring_ts @a and @a b.
static int C_DECL comparePaths(const void* a, const void* b)
{
return stricmp(Str_Text((ddstring_t*)a), Str_Text((ddstring_t*)b));
}
static void printPathList(const ddstring_t* pathList, size_t numPaths, int indent)
{
if(!pathList) return;
for(size_t n = 0; n < numPaths; ++n)
{
const ddstring_t* path = pathList + n;
Con_Printf("%*s\n", indent, Str_Text(path));
}
}
static void deletePathList(ddstring_t* pathList, size_t numPaths)
{
if(!pathList) return;
for(size_t n = 0; n < numPaths; ++n)
{
ddstring_t* path = pathList + n;
Str_Free(path);
}
M_Free(pathList);
}
void de::FileDirectory::debugPrint(FileDirectory* inst)
{
Con_Printf("FileDirectory [%p]:\n", (void*)inst);
size_t numFiles;
ddstring_t* pathList = inst->collectPaths(&numFiles, 0, DIR_SEP_CHAR);
if(pathList)
{
qsort(pathList, numFiles, sizeof *pathList, comparePaths);
printPathList(pathList, numFiles, 2/*indent*/);
deletePathList(pathList, numFiles);
}
Con_Printf(" %lu %s in directory.\n", (unsigned long)numFiles, (numFiles==1? "path":"paths"));
}
void de::FileDirectory::debugPrintHashDistribution(FileDirectory* inst)
{
PathDirectory::debugPrintHashDistribution(inst);
}
#endif
/**
* C Wrapper API:
*/
#define TOINTERNAL(inst) \
(inst) != 0? reinterpret_cast<de::FileDirectory*>(inst) : NULL
#define TOINTERNAL_CONST(inst) \
(inst) != 0? reinterpret_cast<const de::FileDirectory*>(inst) : NULL
#define SELF(inst) \
DENG2_ASSERT(inst); \
de::FileDirectory* self = TOINTERNAL(inst)
#define SELF_CONST(inst) \
DENG2_ASSERT(inst); \
const de::FileDirectory* self = TOINTERNAL_CONST(inst)
FileDirectory* FileDirectory_New(const char* basePath)
{
return reinterpret_cast<FileDirectory*>(new de::FileDirectory(basePath));
}
void FileDirectory_Delete(FileDirectory* fd)
{
if(fd)
{
SELF(fd);
delete self;
}
}
void FileDirectory_Clear(FileDirectory* fd)
{
SELF(fd);
self->clear();
}
void FileDirectory_AddPaths3(FileDirectory* fd, int flags, const Uri* const* paths,
uint pathsCount, int (*callback) (PathDirectoryNode*, void*),
void* parameters)
{
SELF(fd);
self->addPaths(flags, paths, pathsCount, callback, parameters);
}
void FileDirectory_AddPaths2(FileDirectory* fd, int flags, const Uri* const* paths,
uint pathsCount, int (*callback) (PathDirectoryNode*, void*))
{
SELF(fd);
self->addPaths(flags, paths, pathsCount, callback);
}
void FileDirectory_AddPaths(FileDirectory* fd, int flags, const Uri* const* paths,
uint pathsCount)
{
SELF(fd);
self->addPaths(flags, paths, pathsCount);
}
void FileDirectory_AddPathList3(FileDirectory* fd, int flags, const char* pathList,
int (*callback) (PathDirectoryNode*, void*), void* parameters)
{
SELF(fd);
self->addPathList(flags, pathList, callback, parameters);
}
void FileDirectory_AddPathList2(FileDirectory* fd, int flags, const char* pathList,
int (*callback) (PathDirectoryNode*, void*))
{
SELF(fd);
self->addPathList(flags, pathList, callback);
}
void FileDirectory_AddPathList(FileDirectory* fd, int flags, const char* pathList)
{
SELF(fd);
self->addPathList(flags, pathList);
}
int FileDirectory_Iterate2(FileDirectory* fd, PathDirectoryNodeType nodeType,
PathDirectoryNode* parent, ushort hash, filedirectory_iteratecallback_t callback,
void* parameters)
{
SELF(fd);
int flags = (nodeType == PT_LEAF? PCF_NO_BRANCH : PCF_NO_LEAF);
return PathDirectory_Iterate2(reinterpret_cast<PathDirectory*>(self),
flags, parent, hash, callback, parameters);
}
int FileDirectory_Iterate(FileDirectory* fd, PathDirectoryNodeType nodeType,
PathDirectoryNode* parent, ushort hash, filedirectory_iteratecallback_t callback)
{
return FileDirectory_Iterate2(fd, nodeType, parent, hash, callback, NULL);
}
int FileDirectory_Iterate2_Const(const FileDirectory* fd, PathDirectoryNodeType nodeType,
const PathDirectoryNode* parent, ushort hash,
filedirectory_iterateconstcallback_t callback, void* parameters)
{
SELF_CONST(fd);
int flags = (nodeType == PT_LEAF? PCF_NO_BRANCH : PCF_NO_LEAF);
return PathDirectory_Iterate2_Const(reinterpret_cast<const PathDirectory*>(self),
flags, parent, hash, callback, parameters);
}
int FileDirectory_Iterate_Const(const FileDirectory* fd, PathDirectoryNodeType nodeType,
const PathDirectoryNode* parent, ushort hash, filedirectory_iterateconstcallback_t callback)
{
return FileDirectory_Iterate2_Const(fd, nodeType, parent, hash, callback, NULL);
}
boolean FileDirectory_Find(FileDirectory* fd, PathDirectoryNodeType nodeType,
const char* searchPath, char searchDelimiter, ddstring_t* foundPath,
char foundDelimiter)
{
SELF(fd);
return CPP_BOOL(self->find(nodeType, searchPath, searchDelimiter,
foundPath, foundDelimiter));
}
#if _DEBUG
void FileDirectory_DebugPrint(FileDirectory* fd)
{
de::FileDirectory::debugPrint(TOINTERNAL(fd));
}
void FileDirectory_DebugPrintHashDistribution(FileDirectory* fd)
{
de::FileDirectory::debugPrintHashDistribution(TOINTERNAL(fd));
}
#endif