forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdSearchPath.cxx
More file actions
368 lines (335 loc) · 12.2 KB
/
dSearchPath.cxx
File metadata and controls
368 lines (335 loc) · 12.2 KB
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
// Filename: dSearchPath.cxx
// Created by: drose (01Jul00)
//
////////////////////////////////////////////////////////////////////
//
// PANDA 3D SOFTWARE
// Copyright (c) Carnegie Mellon University. All rights reserved.
//
// All use of this software is subject to the terms of the revised BSD
// license. You should have received a copy of this license along
// with this source code in a file named "LICENSE."
//
////////////////////////////////////////////////////////////////////
#include "dSearchPath.h"
#include "filename.h"
#include <algorithm>
#include <iterator>
////////////////////////////////////////////////////////////////////
// Function: DSearchPath::Results::Constructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
DSearchPath::Results::
Results() {
}
////////////////////////////////////////////////////////////////////
// Function: DSearchPath::Results::Copy Constructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
DSearchPath::Results::
Results(const DSearchPath::Results ©) :
_files(copy._files)
{
}
////////////////////////////////////////////////////////////////////
// Function: DSearchPath::Results::Copy Assignment Operator
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
void DSearchPath::Results::
operator = (const DSearchPath::Results ©) {
_files = copy._files;
}
////////////////////////////////////////////////////////////////////
// Function: DSearchPath::Results::Destructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
DSearchPath::Results::
~Results() {
}
////////////////////////////////////////////////////////////////////
// Function: DSearchPath::Results::clear
// Access: Public
// Description: Removes all the files from the list.
////////////////////////////////////////////////////////////////////
void DSearchPath::Results::
clear() {
_files.clear();
}
////////////////////////////////////////////////////////////////////
// Function: DSearchPath::Results::get_num_files
// Access: Public
// Description: Returns the number of files on the result list.
////////////////////////////////////////////////////////////////////
int DSearchPath::Results::
get_num_files() const {
return _files.size();
}
////////////////////////////////////////////////////////////////////
// Function: DSearchPath::Results::get_file
// Access: Public
// Description: Returns the nth file on the result list.
////////////////////////////////////////////////////////////////////
const Filename &DSearchPath::Results::
get_file(int n) const {
assert(n >= 0 && n < (int)_files.size());
return _files[n];
}
////////////////////////////////////////////////////////////////////
// Function: DSearchPath::Results::add_file
// Access: Public
// Description: Adds a new file to the result list.
////////////////////////////////////////////////////////////////////
void DSearchPath::Results::
add_file(const Filename &file) {
_files.push_back(file);
}
////////////////////////////////////////////////////////////////////
// Function: DSearchPath::Default Constructor
// Access: Public
// Description: Creates an empty search path.
////////////////////////////////////////////////////////////////////
DSearchPath::
DSearchPath() {
}
////////////////////////////////////////////////////////////////////
// Function: DSearchPath::Constructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
DSearchPath::
DSearchPath(const string &path, const string &delimiters) {
append_path(path, delimiters);
}
////////////////////////////////////////////////////////////////////
// Function: DSearchPath::Copy Constructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
DSearchPath::
DSearchPath(const DSearchPath ©) :
_directories(copy._directories)
{
}
////////////////////////////////////////////////////////////////////
// Function: DSearchPath::Copy Assignment Operator
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
void DSearchPath::
operator = (const DSearchPath ©) {
_directories = copy._directories;
}
////////////////////////////////////////////////////////////////////
// Function: DSearchPath::Destructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
DSearchPath::
~DSearchPath() {
}
////////////////////////////////////////////////////////////////////
// Function: DSearchPath::clear
// Access: Public
// Description: Removes all the directories from the search list.
////////////////////////////////////////////////////////////////////
void DSearchPath::
clear() {
_directories.clear();
}
////////////////////////////////////////////////////////////////////
// Function: DSearchPath::append_directory
// Access: Public
// Description: Adds a new directory to the end of the search list.
////////////////////////////////////////////////////////////////////
void DSearchPath::
append_directory(const Filename &directory) {
_directories.push_back(directory);
}
////////////////////////////////////////////////////////////////////
// Function: DSearchPath::prepend_directory
// Access: Public
// Description: Adds a new directory to the front of the search list.
////////////////////////////////////////////////////////////////////
void DSearchPath::
prepend_directory(const Filename &directory) {
_directories.insert(_directories.begin(), directory);
}
////////////////////////////////////////////////////////////////////
// Function: DSearchPath::append_path
// Access: Public
// Description: Adds all of the directories listed in the search path
// to the end of the search list.
////////////////////////////////////////////////////////////////////
void DSearchPath::
append_path(const string &path, const string &delimiters) {
size_t p = 0;
while (p < path.length()) {
size_t q = path.find_first_of(delimiters, p);
if (q == string::npos) {
_directories.push_back(path.substr(p));
return;
}
if (q != p) {
_directories.push_back(path.substr(p, q - p));
}
p = q + 1;
}
}
////////////////////////////////////////////////////////////////////
// Function: DSearchPath::append_path
// Access: Public
// Description: Adds all of the directories listed in the search path
// to the end of the search list.
////////////////////////////////////////////////////////////////////
void DSearchPath::
append_path(const DSearchPath &path) {
copy(path._directories.begin(), path._directories.end(),
back_inserter(_directories));
}
////////////////////////////////////////////////////////////////////
// Function: DSearchPath::prepend_path
// Access: Public
// Description: Adds all of the directories listed in the search path
// to the beginning of the search list.
////////////////////////////////////////////////////////////////////
void DSearchPath::
prepend_path(const DSearchPath &path) {
if (!path._directories.empty()) {
Directories new_directories = path._directories;
copy(_directories.begin(), _directories.end(),
back_inserter(new_directories));
_directories.swap(new_directories);
}
}
////////////////////////////////////////////////////////////////////
// Function: DSearchPath::is_empty
// Access: Public
// Description: Returns true if the search list is empty, false
// otherwise.
////////////////////////////////////////////////////////////////////
bool DSearchPath::
is_empty() const {
return _directories.empty();
}
////////////////////////////////////////////////////////////////////
// Function: DSearchPath::get_num_directories
// Access: Public
// Description: Returns the number of directories on the search list.
////////////////////////////////////////////////////////////////////
int DSearchPath::
get_num_directories() const {
return _directories.size();
}
////////////////////////////////////////////////////////////////////
// Function: DSearchPath::get_directory
// Access: Public
// Description: Returns the nth directory on the search list.
////////////////////////////////////////////////////////////////////
const Filename &DSearchPath::
get_directory(int n) const {
assert(n >= 0 && n < (int)_directories.size());
return _directories[n];
}
////////////////////////////////////////////////////////////////////
// Function: DSearchPath::find_file
// Access: Public
// Description: Searches all the directories in the search list for
// the indicated file, in order. Returns the full
// matching pathname of the first match if found, or the
// empty string if not found.
////////////////////////////////////////////////////////////////////
Filename DSearchPath::
find_file(const Filename &filename) const {
if (filename.is_local()) {
Directories::const_iterator di;
for (di = _directories.begin(); di != _directories.end(); ++di) {
Filename match((*di), filename);
if (match.exists()) {
if ((*di) == "." && filename.is_fully_qualified()) {
// A special case for the "." directory: to avoid prefixing
// an endless stream of ./ in front of files, if the
// filename already has a ./ prefixed
// (i.e. is_fully_qualified() is true), we don't
// prefix another one.
return filename;
} else {
return match;
}
}
}
}
return string();
}
////////////////////////////////////////////////////////////////////
// Function: DSearchPath::find_all_files
// Access: Public
// Description: Searches all the directories in the search list for
// the indicated file, in order. Fills up the results
// list with *all* of the matching filenames found, if
// any. Returns the number of matches found.
//
// It is the responsibility of the the caller to clear
// the results list first; otherwise, the newly-found
// files will be appended to the list.
////////////////////////////////////////////////////////////////////
int DSearchPath::
find_all_files(const Filename &filename,
DSearchPath::Results &results) const {
int num_added = 0;
if (filename.is_local()) {
Directories::const_iterator di;
for (di = _directories.begin(); di != _directories.end(); ++di) {
Filename match((*di), filename);
if (match.exists()) {
if ((*di) == "." && filename.is_fully_qualified()) {
// A special case for the "." directory: to avoid prefixing
// an endless stream of ./ in front of files, if the
// filename already has a ./ prefixed
// (i.e. is_fully_qualified() is true), we don't
// prefix another one.
results.add_file(filename);
} else {
results.add_file(match);
}
num_added++;
}
}
}
return num_added;
}
////////////////////////////////////////////////////////////////////
// Function: DSearchPath::output
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
void DSearchPath::
output(ostream &out, const string &separator) const {
if (!_directories.empty()) {
Directories::const_iterator di = _directories.begin();
out << (*di);
++di;
while (di != _directories.end()) {
out << separator << (*di);
++di;
}
}
}
////////////////////////////////////////////////////////////////////
// Function: DSearchPath::write
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
void DSearchPath::
write(ostream &out, int indent_level) const {
Directories::const_iterator di;
for (di = _directories.begin(); di != _directories.end(); ++di) {
for (int i = 0; i < indent_level; i++) {
out << ' ';
}
out << (*di) << "\n";
}
}