forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathppFilenamePattern.cxx
More file actions
190 lines (170 loc) · 6.53 KB
/
ppFilenamePattern.cxx
File metadata and controls
190 lines (170 loc) · 6.53 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
// Filename: ppFilenamePattern.cxx
// Created by: drose (25Sep00)
//
////////////////////////////////////////////////////////////////////
#include "ppFilenamePattern.h"
#include <assert.h>
////////////////////////////////////////////////////////////////////
// Function: PPFilenamePattern::Constructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
PPFilenamePattern::
PPFilenamePattern(const string &pattern) {
size_t pct = pattern.find(PATTERN_WILDCARD);
_has_wildcard = (pct != string::npos);
if (_has_wildcard) {
_prefix = pattern.substr(0, pct);
_suffix = pattern.substr(pct + 1);
} else {
_prefix = pattern;
}
}
////////////////////////////////////////////////////////////////////
// Function: PPFilenamePattern::Copy Constructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
PPFilenamePattern::
PPFilenamePattern(const PPFilenamePattern ©) :
_has_wildcard(copy._has_wildcard),
_prefix(copy._prefix),
_suffix(copy._suffix)
{
}
////////////////////////////////////////////////////////////////////
// Function: PPFilenamePattern::Copy Assignment Operator
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
void PPFilenamePattern::
operator = (const PPFilenamePattern ©) {
_has_wildcard = copy._has_wildcard;
_prefix = copy._prefix;
_suffix = copy._suffix;
}
////////////////////////////////////////////////////////////////////
// Function: PPFilenamePattern::has_wildcard
// Access: Public
// Description: Returns true if the filename pattern contained a
// wildcard (and hence represents a pattern and not
// just a single particular filename), or false if it
// did not.
////////////////////////////////////////////////////////////////////
bool PPFilenamePattern::
has_wildcard() const {
return _has_wildcard;
}
////////////////////////////////////////////////////////////////////
// Function: PPFilenamePattern::get_pattern
// Access: Public
// Description: Returns the filename pattern.
////////////////////////////////////////////////////////////////////
string PPFilenamePattern::
get_pattern() const {
if (_has_wildcard) {
return _prefix + PATTERN_WILDCARD + _suffix;
} else {
return _prefix;
}
}
////////////////////////////////////////////////////////////////////
// Function: PPFilenamePattern::get_prefix
// Access: Public
// Description: Returns the part of the filename pattern before the
// wildcard. If the filename did not contain a
// wildcard, this returns the entire filename.
////////////////////////////////////////////////////////////////////
const string &PPFilenamePattern::
get_prefix() const {
return _prefix;
}
////////////////////////////////////////////////////////////////////
// Function: PPFilenamePattern::get_suffix
// Access: Public
// Description: Returns the part of the filename pattern after the
// wildcard. If the filename did not contain a
// wildcard, this returns the empty string.
////////////////////////////////////////////////////////////////////
const string &PPFilenamePattern::
get_suffix() const {
return _suffix;
}
////////////////////////////////////////////////////////////////////
// Function: PPFilenamePattern::matches_filename
// Access: Public
// Description: Returns true if the given filename matches the
// pattern, false otherwise.
////////////////////////////////////////////////////////////////////
bool PPFilenamePattern::
matches(const string &filename) const {
if (_has_wildcard) {
return
(filename.length() >= _prefix.length() + _suffix.length()) &&
(filename.substr(0, _prefix.length()) == _prefix) &&
(filename.substr(filename.length() - _suffix.length()) == _suffix);
} else {
return (filename == _prefix);
}
}
////////////////////////////////////////////////////////////////////
// Function: PPFilenamePattern::extract_body
// Access: Public
// Description: If the filename matches the pattern
// (e.g. matches_filename() returns true), return the
// portion of the filename that corresponds to the
// wildcard in the pattern: the part of the filename
// between the prefix and the suffix. If the filename
// does not match the pattern, or the pattern does not
// contain a wildcard, returns empty string.
////////////////////////////////////////////////////////////////////
string PPFilenamePattern::
extract_body(const string &filename) const {
if (_has_wildcard) {
size_t outside_length = _prefix.length() + _suffix.length();
if ((filename.length() >= outside_length) &&
(filename.substr(0, _prefix.length()) == _prefix) &&
(filename.substr(filename.length() - _suffix.length()) == _suffix)) {
return filename.substr(_prefix.length(), filename.length() - outside_length);
}
}
return string();
}
////////////////////////////////////////////////////////////////////
// Function: PPFilenamePattern::transform_filename
// Access: Public
// Description: Transforms a filename by replacing the parts of the
// filename described in the pattern transform_from with
// the corresponding parts of the filename described in
// this pattern. If the filename does not match
// transform_from, returns the untransformed filename.
//
// It is an error to call this unless both this pattern
// and transform_from include a wildcard.
////////////////////////////////////////////////////////////////////
string PPFilenamePattern::
transform(const string &filename,
const PPFilenamePattern &transform_from) const {
assert(transform_from._has_wildcard);
if (transform_from.matches(filename)) {
if (!_has_wildcard) {
return _prefix;
} else {
string body = transform_from.extract_body(filename);
string result = _prefix + body;
// Now the suffix might contain more % characters. Replace all
// of them.
size_t p = 0;
size_t pct = _suffix.find(PATTERN_WILDCARD, p);
while (pct != string::npos) {
result += _suffix.substr(p, pct - p);
result += body;
p = pct + 1;
pct = _suffix.find(PATTERN_WILDCARD, p);
}
result += _suffix.substr(p);
return result;
}
}
return filename;
}