/
pathtreenode.cpp
312 lines (256 loc) · 8.44 KB
/
pathtreenode.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
/**
* @file pathtreenode.cpp
* @ingroup base
*
* @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/Log>
#include "pathtree.h"
namespace de {
struct PathTree::Node::Instance
{
/// @c true = this is a leaf node.
bool isLeaf;
/// PathTree which owns this node.
PathTree& tree;
/// Unique identifier for the path fragment this node represents,
/// in the owning PathTree.
PathTree::FragmentId fragmentId;
/// Parent node in the user's logical hierarchy.
PathTree::Node* parent;
/// User-specified data pointer associated with this node.
void* userPointer;
/// User-specified value associated with this node.
int userValue;
Instance(PathTree& _tree, bool _isLeaf, PathTree::FragmentId _fragmentId,
PathTree::Node* _parent)
: isLeaf(_isLeaf), tree(_tree), fragmentId(_fragmentId), parent(_parent),
userPointer(0), userValue(0)
{}
};
PathTree::Node::Node(PathTree& tree, PathTree::NodeType type, PathTree::FragmentId fragmentId,
PathTree::Node* parent, void* userPointer, int userValue)
{
d = new Instance(tree, type == PathTree::Leaf, fragmentId, parent);
setUserPointer(userPointer);
setUserValue(userValue);
}
PathTree::Node::~Node()
{
delete d;
}
bool PathTree::Node::isLeaf() const
{
return d->isLeaf;
}
PathTree& PathTree::Node::tree() const
{
return d->tree;
}
PathTree::Node* PathTree::Node::parent() const
{
return d->parent;
}
PathTree::FragmentId PathTree::Node::fragmentId() const
{
return d->fragmentId;
}
String const& PathTree::Node::name() const
{
return tree().fragmentName(d->fragmentId);
}
Uri::hash_type PathTree::Node::hash() const
{
return tree().fragmentHash(d->fragmentId);
}
void* PathTree::Node::userPointer() const
{
return d->userPointer;
}
int PathTree::Node::userValue() const
{
return d->userValue;
}
PathTree::Node& PathTree::Node::setUserPointer(void* ptr)
{
d->userPointer = ptr;
return *this;
}
PathTree::Node& PathTree::Node::setUserValue(int value)
{
d->userValue = value;
return *this;
}
/// @todo This logic should be encapsulated in Uri/Uri::Segment
static int matchName(char const* string, char const* pattern)
{
char const* in = string, *st = pattern;
while(*in)
{
if(*st == '*')
{
st++;
continue;
}
if(*st != '?' && (tolower((unsigned char) *st) != tolower((unsigned char) *in)))
{
// A mismatch. Hmm. Go back to a previous '*'.
while(st >= pattern && *st != '*')
{ st--; }
// No match?
if(st < pattern) return false;
// The asterisk lets us continue.
}
// This character of the pattern is OK.
st++;
in++;
}
// Skip remaining asterisks.
while(*st == '*')
{ st++; }
// Match is good if the end of the pattern was reached.
return *st == 0;
}
int PathTree::Node::comparePath(de::Uri const& searchPattern, int flags) const
{
if(((flags & PCF_NO_LEAF) && isLeaf()) ||
((flags & PCF_NO_BRANCH) && !isLeaf()))
return 1;
try
{
de::Uri::Segment const* snode = &searchPattern.firstSegment();
// In reverse order, compare each path node in the search term.
int pathNodeCount = searchPattern.segmentCount();
PathTree::Node const* node = this;
for(int i = 0; i < pathNodeCount; ++i)
{
bool const snameIsWild = !snode->toString().compare("*");
if(!snameIsWild)
{
// If the hashes don't match it can't possibly be this.
if(snode->hash() != node->hash())
{
return 1;
}
// Compare the names.
/// @todo Optimize: conversion to string is unnecessary.
QByteArray name = node->name().toUtf8();
QByteArray sname = snode->toString().toUtf8();
if(!matchName(name.constData(), sname.constData()))
{
return 1;
}
}
// Have we arrived at the search target?
if(i == pathNodeCount - 1)
{
return !(!(flags & PCF_MATCH_FULL) || !node->parent());
}
// Is the hierarchy too shallow?
if(!node->parent())
{
return 1;
}
// So far so good. Move one level up the hierarchy.
node = node->parent();
snode = &searchPattern.segment(i + 1);
}
}
catch(de::Uri::NotSegmentError const&)
{} // Ignore this error.
return 1;
}
#ifdef LIBDENG_STACK_MONITOR
static void* stackStart;
static size_t maxStackDepth;
#endif
typedef struct pathconstructorparams_s {
size_t length;
String composedPath;
QChar delimiter;
} pathconstructorparams_t;
/**
* Recursive path constructor. First finds the root and the full length of the
* path (when descending), then allocates memory for the string, and finally
* copies each fragment with the delimiters (on the way out).
*/
static void pathConstructor(pathconstructorparams_t& parm, PathTree::Node const& trav)
{
String const& fragment = trav.name();
#ifdef LIBDENG_STACK_MONITOR
maxStackDepth = MAX_OF(maxStackDepth, stackStart - (void*)&fragment);
#endif
parm.length += fragment.length();
if(trav.parent())
{
if(!parm.delimiter.isNull())
{
// There also needs to be a delimiter (a single character).
parm.length += 1;
}
// Descend to parent level.
pathConstructor(parm, *trav.parent());
// Append the separator.
if(!parm.delimiter.isNull())
parm.composedPath.append(parm.delimiter);
}
// We've arrived at the deepest level. The full length is now known.
// Ensure there's enough memory for the string.
else if(parm.composedPath)
{
parm.composedPath.reserve(parm.length);
}
// Assemble the path by appending the fragment.
parm.composedPath.append(fragment);
}
/**
* @todo This is a good candidate for result caching: the constructed path
* could be saved and returned on subsequent calls. Are there any circumstances
* in which the cached result becomes obsolete? -jk
*
* The only times the result becomes obsolete is when the delimiter is changed
* or when the directory itself is rebuilt (in which case the nodes themselves
* will be free'd). Note that any caching mechanism should not counteract one
* of the primary goals of this class, i.e., optimal memory usage for the whole
* directory. Caching constructed paths for every leaf node in the directory
* would completely negate the benefits of the design of this class.
*
* Perhaps a fixed size MRU cache? -ds
*/
Uri PathTree::Node::composeUri(QChar delimiter) const
{
pathconstructorparams_t parm = { 0, String(), delimiter };
#ifdef LIBDENG_STACK_MONITOR
stackStart = &parm;
#endif
// Include a terminating path delimiter for branches.
if(!delimiter.isNull() && !isLeaf())
parm.length += 1; // A single character.
// Recursively construct the path from fragments and delimiters.
pathConstructor(parm, *this);
// Terminating delimiter for branches.
if(!delimiter.isNull() && !isLeaf())
parm.composedPath += delimiter;
DENG2_ASSERT(parm.composedPath.length() == (int)parm.length);
#ifdef LIBDENG_STACK_MONITOR
LOG_AS("pathConstructor");
LOG_INFO("Max stack depth: %1 bytes") << maxStackDepth;
#endif
return Uri(parm.composedPath, RC_NULL);
}
} // namespace de