-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathocSearchPath.cxx
More file actions
331 lines (300 loc) · 11.9 KB
/
Copy pathocSearchPath.cxx
File metadata and controls
331 lines (300 loc) · 11.9 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
// Filename: ocSearchPath.cxx
// Created by: frang (03June04)
//
////////////////////////////////////////////////////////////////////
#include "ocSearchPath.h"
#include "filename.h"
#include <algorithm>
#include <iterator>
////////////////////////////////////////////////////////////////////
// Function: OCSearchPath::Results::Constructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
OCSearchPath::Results::Results() {
}
////////////////////////////////////////////////////////////////////
// Function: OCSearchPath::Results::Copy Constructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
OCSearchPath::Results::Results(const OCSearchPath::Results ©) :
_files(copy._files)
{
}
////////////////////////////////////////////////////////////////////
// Function: OCSearchPath::Results::Copy Assignment Operator
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
void OCSearchPath::Results::operator = (const OCSearchPath::Results ©) {
_files = copy._files;
}
////////////////////////////////////////////////////////////////////
// Function: OCSearchPath::Results::Destructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
OCSearchPath::Results::~Results() {
}
////////////////////////////////////////////////////////////////////
// Function: OCSearchPath::Results::clear
// Access: Public
// Description: Removes all the files from the list.
////////////////////////////////////////////////////////////////////
void OCSearchPath::Results::clear() {
_files.clear();
}
////////////////////////////////////////////////////////////////////
// Function: OCSearchPath::Results::get_num_files
// Access: Public
// Description: Returns the number of files on the result list.
////////////////////////////////////////////////////////////////////
int OCSearchPath::Results::get_num_files() const {
return static_cast<int>(_files.size());
}
////////////////////////////////////////////////////////////////////
// Function: OCSearchPath::Results::get_file
// Access: Public
// Description: Returns the nth file on the result list.
////////////////////////////////////////////////////////////////////
const Filename &OCSearchPath::Results::get_file(int n) const {
assert(n >= 0 && n < (int)_files.size());
return _files[static_cast<unsigned>(n)];
}
////////////////////////////////////////////////////////////////////
// Function: OCSearchPath::Results::add_file
// Access: Public
// Description: Adds a new file to the result list.
////////////////////////////////////////////////////////////////////
void OCSearchPath::Results::add_file(const Filename &file) {
_files.push_back(file);
}
////////////////////////////////////////////////////////////////////
// Function: OCSearchPath::Default Constructor
// Access: Public
// Description: Creates an empty search path.
////////////////////////////////////////////////////////////////////
OCSearchPath::OCSearchPath() {
}
////////////////////////////////////////////////////////////////////
// Function: OCSearchPath::Constructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
OCSearchPath::OCSearchPath(const string &path, const string &delimiters) {
append_path(path, delimiters);
}
////////////////////////////////////////////////////////////////////
// Function: OCSearchPath::Copy Constructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
OCSearchPath::OCSearchPath(const OCSearchPath ©) :
_directories(copy._directories)
{
}
////////////////////////////////////////////////////////////////////
// Function: OCSearchPath::Copy Assignment Operator
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
void OCSearchPath::operator = (const OCSearchPath ©) {
_directories = copy._directories;
}
////////////////////////////////////////////////////////////////////
// Function: OCSearchPath::Destructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
OCSearchPath::~OCSearchPath() {
}
////////////////////////////////////////////////////////////////////
// Function: OCSearchPath::clear
// Access: Public
// Description: Removes all the directories from the search list.
////////////////////////////////////////////////////////////////////
void OCSearchPath::clear() {
_directories.clear();
}
////////////////////////////////////////////////////////////////////
// Function: OCSearchPath::append_directory
// Access: Public
// Description: Adds a new directory to the end of the search list.
////////////////////////////////////////////////////////////////////
void OCSearchPath::append_directory(const Filename &directory) {
_directories.push_back(directory);
}
////////////////////////////////////////////////////////////////////
// Function: OCSearchPath::prepend_directory
// Access: Public
// Description: Adds a new directory to the front of the search list.
////////////////////////////////////////////////////////////////////
void OCSearchPath::prepend_directory(const Filename &directory) {
_directories.insert(_directories.begin(), directory);
}
////////////////////////////////////////////////////////////////////
// Function: OCSearchPath::append_path
// Access: Public
// Description: Adds all of the directories listed in the search path
// to the end of the search list.
////////////////////////////////////////////////////////////////////
void OCSearchPath::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: OCSearchPath::append_path
// Access: Public
// Description: Adds all of the directories listed in the search path
// to the end of the search list.
////////////////////////////////////////////////////////////////////
void OCSearchPath::append_path(const OCSearchPath &path) {
copy(path._directories.begin(), path._directories.end(),
back_inserter(_directories));
}
////////////////////////////////////////////////////////////////////
// Function: OCSearchPath::prepend_path
// Access: Public
// Description: Adds all of the directories listed in the search path
// to the beginning of the search list.
////////////////////////////////////////////////////////////////////
void OCSearchPath::prepend_path(const OCSearchPath &path) {
if (!path._directories.empty()) {
Directories new_directories = path._directories;
copy(_directories.begin(), _directories.end(),
back_inserter(new_directories));
_directories.swap(new_directories);
}
}
////////////////////////////////////////////////////////////////////
// Function: OCSearchPath::is_empty
// Access: Public
// Description: Returns true if the search list is empty, false
// otherwise.
////////////////////////////////////////////////////////////////////
bool OCSearchPath::is_empty() const {
return _directories.empty();
}
////////////////////////////////////////////////////////////////////
// Function: OCSearchPath::get_num_directories
// Access: Public
// Description: Returns the number of directories on the search list.
////////////////////////////////////////////////////////////////////
int OCSearchPath::get_num_directories() const {
return static_cast<int>(_directories.size());
}
////////////////////////////////////////////////////////////////////
// Function: OCSearchPath::get_directory
// Access: Public
// Description: Returns the nth directory on the search list.
////////////////////////////////////////////////////////////////////
const Filename &OCSearchPath::get_directory(int n) const {
assert(n >= 0 && n < (int)_directories.size());
return _directories[static_cast<unsigned>(n)];
}
////////////////////////////////////////////////////////////////////
// Function: OCSearchPath::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 OCSearchPath::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: OCSearchPath::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 OCSearchPath::find_all_files(const Filename &filename,
OCSearchPath::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: OCSearchPath::output
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
void OCSearchPath::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: OCSearchPath::write
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
void OCSearchPath::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";
}
}