-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrgrepa.c
422 lines (370 loc) · 8.77 KB
/
rgrepa.c
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include <stdio.h>
#define MAXSIZE 4096
//Funtions
int strleng(char* string);
int firstOccur(char* line, char c);
int secondOccur(char* line, char c);
int checkSpecial(char* pattern);
int sameChar(char* pattern);
int afterPlus(char* pattern, char* line);
int matchPatternDots(char* pattern,char* line);
int patternPlus(char* pattern,char* line);
int morePlus(char* pattern,char* line);
int questionMark(char* pattern,char* line);
/**
* You can use this recommended helper function
* Returns true if partial_line matches pattern, starting from
* the first char of partial_line.
*/
int matches_leading(char *partial_line, char *pattern) {
// Implement if desire
return 0;
}
/**
* You may assume that all strings are properly null terminated
* and will not overrun the buffer set by MAXSIZE
*
* Implementation of the rgrep matcher function
*/
/* . = any char
+ = preceeding will appear any # of times
? = preceeding char may or may not appear
\ = nullifys any special meaning of the previous character
Usage:
a+ Matches a, aa, aaaaa or sreally any number of a’s more than one
.+ Matches any non-empty String
\\+ Matches a string of \’s
a?b+ Matches ab, b, abbb or any amount of b following op:onal a.
\? A question mark must appear in the line
they?re Matches a line that contains either the string "theyre" or the string "there"
h.d..?n Matches lines that contain substrings like "hidden", "hidin", "hbdwen", "hadbn", etc.
cu\.?t Matches lines that either contain the substring "cut" or "cu.t"
*/
int rgrep_matches(char *line, char *pattern){
int n = strleng(pattern);
int m = strleng(line);
if(checkSpecial(pattern) != 5000){
switch(pattern[checkSpecial(pattern)]) {
case '.' :
//printf("DOT\n");
if(sameChar(pattern)){
//printf("DOT1\n");
if(m == n)
return 1;
else
return 0;
}
else if (matchPatternDots(pattern, line)){
//printf("DOTpattern\n");
return 1;
}
else if(firstOccur(pattern, '+') != 5000){
//printf("DOT+\n");
if (afterPlus(pattern, line))
return 1;
}
else
return 0;
case '\\' :
//printf("SLASHSLASH\n");
return 0;
case '+' :
//printf("PLUS\n");
if(n==1)
return 1;
if(morePlus(pattern, line))
return 1;
if(patternPlus(pattern, line))
return 1;
return 0;
case '?' :
//printf("QUESTION\n");
if(questionMark(pattern, line))
return 1;
return 0;
default :
//printf("DEFAULT\n");
return 0;
}
}
else{
//printf("ELSE\n");
int matches = 0;
int index = firstOccur(line, pattern[0]);
//If pattern is empty string, prints all lines
if(n == 0){
return 1;
//If pattern is length one char and no special chars.
}
else if(n==1){
if(index != 5000){
/*check for next letter in pattern in line*/
return 1;
}
}
//If pattern length > 1 and no special chars.
else if (n > 1){
if(index != 5000){
matches++;
/*check for next letter in pattern in line*/
for(int i = 1, l = strleng(line); i < l; i++){
if(pattern[i] == line[index + i]){
matches++;
if(matches == n){
return 1;
}
}
else
return 0;
}
}
else
return 0;
}
return 0;
}
return 0;
}
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <PATTERN>\n", argv[0]);
return 2;
}
/* we're not going to worry about long lines */
char buf[MAXSIZE];
while (!feof(stdin) && !ferror(stdin)) {
if (!fgets(buf, sizeof(buf), stdin)) {
break;
}
if (rgrep_matches(buf, argv[1])) {
fputs(buf, stdout);
fflush(stdout);
}
}
if (ferror(stdin)) {
perror(argv[0]);
return 1;
}
return 0;
}
int patternPlus(char* pattern,char* line){
int n = strleng(pattern);
int m = strleng(line);
//int firstFound[m];
int p = firstOccur(pattern, '+');
int count = 0;
int found = 0;
//int index = 0;
//Pattern length less then = 2
if(n <= 2){
for(int i = 0; i< m; i++){
if(line[i] == pattern[0]){
found++;
return 1;
}
}
}
//Pattern Greater Than 2
else{
//char p1 = pattern[p-1];
for(int i = 0; i< m; i++){
if(line[i] == pattern[0]){
//firstFound[index++] = i;
count = 1;
}
}
//index = 0;
for(int i = 1; i< n; i++){
if(line[i] == pattern[i]){
count++;
}
}
if(p < n){
char p2 = pattern[p+1];
for(int i = p+1; i< n && i < m; i++){
//printf("p2: %d\n", line[firstOccur(line, p2)] == pattern[i]);
if(line[firstOccur(line, p2)] == pattern[i]){
//printf("p: %d,n: %d, line: %s\n", p,n, line);
count++;
//printf("count: %d,n: %d, line: %s\n", count,n, line);
}
}
}
//printf("count: %d,n: %d, line: %s\n", count,n, line);
if(count == n-1 || count == n)
return 1;
}
return 0;
}
//Checks for matchs to dot-pattern
int matchPatternDots(char* pattern,char* line){
int n = strleng(pattern);
int m = strleng(line);
int count = 0;
int dots = 0;
int temp = 0;
if(pattern[0] == '.')
while(pattern[temp] == '.'){
temp++;
dots++;
}
else{
for(int i = 0; i< n; i++){
if(pattern[i] == '.'){
dots++;
}
}
}
for(int i = 0; i< n; i++){
for(int j = 0; j< m; j++){
if((pattern[i] != '\0') && (pattern[i] == line[j]) && (pattern[i] != '.')){
count++;
}
}
}
//printf("count: %d,n: %d\n", count,n-dots);
if(count >= n- dots && strleng(line) >= strleng(pattern))
return 1;
return 0;
}
//Checks for matches after +
int afterPlus(char* pattern, char* line){
int n = strleng(pattern);
int m = strleng(line);
int count= 0;
int i = 0;
for(int j = 0; j < m; j++){
if(pattern[i] == line[j] || pattern[i] == '+'|| pattern[i] == '.'){
count++;
if (i != n){
i++;
}
}
}
//printf("count: %d,n: %d, line: %s\n", count,n, line);
if(count == n- 1|| count == n){
return 1;
}
return 0;
}
//Checks if pattern is all the same
int sameChar(char *pattern){
int n = strleng(pattern);
int m = 0;
for(int i = 0; i< n; i++){
if(pattern[i] == pattern[0]){
m++;
}
if(m == n)
return 1;
}
return 0;
}
/* Strlen function*/
int strleng(char * string){
int i = 0;
while(string[i] != '\0' && string[i] != '\n'){
i++;
}
return i;
}
/*Returns index of first char in string that matches char, returns 5000 if no matches*/
int firstOccur(char * line, char c){
int n = strleng(line);
for(int i = 0; i < n; i++){
if(line[i] == c)
return i;
}
return 5000;
}
/*Returns index of second char in string that matches char , returns 5000 if no matches*/
int secondOccur(char* line, char c){
int n = strleng(line);
int count = 0;
for(int i = 0; i < n; i++){
if(line[i] == c)
count++;
if(count == 2)
return i;
}
return 5000;
}
/*Checks for the first special character not proceeded by a \(escape char)
returns index of special char or returns 5000
. = any char
+ = preceeding will appear any # of times
? = preceeding char may or may not appear
\ = nullifys any special meaning of the previous character*/
int checkSpecial(char* pattern){
int n = strleng(pattern);
//checks for .
for(int i = 0; i < n; i++){
if(pattern[i] == '.'){
if(i != 0 && pattern[i-1] != 92){
return i;
}
else
return i;
}
}
//checks for +
for(int i = 0; i < n; i++){
if(pattern[i] == '+'){
if(i != 0 && pattern[i-1] != 92){
return i;
}
else
return i;
}
}
//checks for ?
for(int i = 0; i < n; i++){
if(pattern[i] == '?'){
if(i != 0 && pattern[i-1] != 92){
return i;
}
else
return i;
}
}
return 5000;
}
//Checks for and processes multiple plus signs
int morePlus(char* pattern,char* line){
int n = strleng(pattern);
int m = strleng(line);
int count = 0;
int plus = 0;
char beforePlus[n];
int temp = 0;
for(int i = 0; i < n; i++ ){
if(pattern[i] == '+'){
plus++;
temp++;
}
else
beforePlus[temp] = pattern[i];
}
temp = 0;
//printf("%c,%c, %c\n", beforePlus[0], beforePlus[1], beforePlus[2]);
if(plus < 2)
return 0;
else{
for(int i = 0; i < n; i++ ){
for(int j = 0; j < m; j++ ){
if (line[j] == beforePlus[i]){
count++;
break;
}
}
}
//printf("count %d, plus: %d,, line: %s\n", count, plus, line);
if(count >= plus)
return 1;
}
return 0;
}
//Checks for question marks ignores escaped question marks
int questionMark(char* pattern,char* line){
return 0;
}