This repository has been archived by the owner on Sep 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pattern.cpp
265 lines (223 loc) · 5.78 KB
/
pattern.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
/* oggted: pattern.cpp
* Copyright (c) 2011 Bert Muennich <be.muennich at googlemail.com>
*
* 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 Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <iostream>
#include <sstream>
#include <cstring>
#include "pattern.h"
uint Pattern::count() const {
if (status == 0)
return 0;
else
return ids.size();
}
bool IPattern::setPattern(const char *text, bool isRE) {
uint i;
bool wildcardPresent = false;
char flag = isRE ? 'N' : 'n';
ostringstream tmp;
if (status > 0)
regfree(®ex);
ids.clear();
subs.clear();
matches.clear();
status = 0;
for (i = 0; i < strlen(text); ++i) {
if (text[i] == '%') {
if (i+1 >= strlen(text)) {
cerr << command << ": -" << flag << " option ignored, "
<< "because pattern ends with an invalid character" << endl;
return false;
}
switch (text[i+1]) {
case 'a':
case 'A':
case 't':
case 'g':
tmp << "(.+)";
subs.push_back(text[++i]);
wildcardPresent = true;
break;
case 'y':
case 'T':
case 'd':
tmp << "([0-9]+)";
subs.push_back(text[++i]);
wildcardPresent = true;
break;
case '%':
tmp << '%';
break;
default:
cerr << command << ": -" << flag << " option ignored, "
<< "because pattern contains invalid wildcard: `"
<< text[i] << text[i+1] << "'" << endl;
return false;
}
} else {
if (!isRE) {
if (strchr("\\*+?.^$[]{}()", text[i]))
tmp << '\\';
} else {
if (text[i] == '(' && preBackslashCount(text, i) % 2 == 0)
// although its match is never used,
// we still have to count this subexpression
subs.push_back(0);
}
tmp << text[i];
}
}
if (!wildcardPresent) {
cerr << command << ": -" << flag << " option ignored, "
<< "because pattern does not contain any wildcard" << endl;
return false;
}
pattern = tmp.str();
if (regcomp(®ex, pattern.c_str(), REG_EXTENDED) ||
regex.re_nsub != subs.size()) {
cerr << command << ": error compiling regex for pattern, -" << flag
<< " option ignored" << endl;
return false;
}
status = 1;
return true;
}
uint IPattern::match(const char *filename) {
if (status == 0)
return 0;
string path(filename);
regmatch_t *pmatch = new regmatch_t[subs.size() + 1];
status = 1;
ids.clear();
matches.clear();
if (regexec(®ex, filename, subs.size() + 1, pmatch, 0)) {
cout << filename << ": pattern does not match filename" << endl;
return 0;
}
for (uint i = 1; i <= subs.size(); ++i) {
if (subs[i-1]) {
if (pmatch[i].rm_so == -1 || pmatch[i].rm_eo == -1)
matches.push_back("");
else
matches.push_back(path.substr(pmatch[i].rm_so,
pmatch[i].rm_eo - pmatch[i].rm_so));
ids.push_back(subs[i-1]);
}
}
status = 2;
delete [] pmatch;
return matches.size();
}
MatchInfo IPattern::getMatch(uint num) const {
MatchInfo info;
if (status < 2 || num >= ids.size() || num >= matches.size()) {
info.id = 0;
info.text = "";
} else {
info.id = ids[num];
info.text = matches[num];
}
return info;
}
int IPattern::preBackslashCount(const char *str, uint pos) const {
uint cnt = 0;
if (pos > strlen(str))
return -1;
for (; pos > 0 && str[pos-1] == '\\'; pos--)
++cnt;
return cnt;
}
bool OPattern::setPattern(const char *pattern) {
uint i;
bool wildcardPresent = false;
ids.clear();
pos.clear();
len.clear();
status = 0;
if (strlen(pattern) == 0)
return false;
if (pattern[strlen(pattern) - 1] == '/') {
cerr << command << ": -o option ignored, "
<< "because file pattern ends with a slash" << endl;
return false;
}
for (i = 0; i < strlen(pattern); ++i) {
if (pattern[i] == '%') {
if (i+1 >= strlen(pattern)) {
cerr << command << ": -o option ignored, "
<< "because pattern ends with an invalid character" << endl;
return false;
}
switch (pattern[i+1]) {
case 'a':
case 'A':
case 't':
case 'c':
case 'g':
case 'T':
case 'y':
case 'd':
ids.push_back(pattern[i+1]);
pos.push_back(i++);
len.push_back(2);
wildcardPresent = true;
break;
default:
cerr << command << ": -o option ignored, "
<< "because pattern contains invalid wildcard: `"
<< pattern[i] << pattern[i+1] << "'" << endl;
return false;
}
}
}
if (!wildcardPresent) {
cerr << command << ": -o option ignored, "
<< "because pattern does not contain any wildcard" << endl;
return false;
}
text = pattern;
status = 1;
return true;
}
MatchInfo OPattern::getMatch(uint num) const {
MatchInfo info;
if (status < 1 || num >= ids.size())
info.id = 0;
else
info.id = ids[num];
info.text = "";
return info;
}
void OPattern::setMatch(uint num, const MatchInfo &info) {
int diff;
uint newlen, oldlen;
if (status < 1 || num >= ids.size())
return;
oldlen = len[num];
newlen = info.text.length();
diff = newlen - oldlen;
text.replace(pos[num], oldlen, info.text);
len[num] = newlen;
vector<uint>::iterator each = pos.begin() + num + 1;
for (; each != pos.end(); ++each)
*each += diff;
}
void OPattern::replaceSpecialChars(char replaceChar) {
int pos;
while ((pos = text.find_first_of("*~")) != string::npos)
text[pos] = replaceChar;
}