-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrgrep.c
More file actions
581 lines (530 loc) · 10.7 KB
/
rgrep.c
File metadata and controls
581 lines (530 loc) · 10.7 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
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*
Adam Pluguez
CSE 31
11/3/17
Project 1
rgrep
*/
#include <stdio.h>
#define MAXSIZE 4096
//Functions
int strleng(char* string);
int strlengIgnoreSlash(char* string);
int firstOccur(char* line, char c);
int checkForSpecialChar(char* pattern);
int samePattern(char* pattern);
int patternDotAfterPlus(char* pattern, char* line);
int matchPatternDots(char* pattern,char* line);
int patternSinglePlus(char* pattern,char* line);
int multiplePlus(char* pattern,char* line);
int basicRgrep(char* pattern,char* line, int n, int m);
int quesMark(char * pattern, char* line);
int escapeChar(char* pattern,char* line);
int matchOne(char* pattern,char* line);
int plusDotsCount(char* pattern);
int slashesCount(char* pattern);
int quesCount(char* pattern);
/**
* 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
*/
int rgrep_matches(char *line, char *pattern){
int n = strleng(pattern);
int m = strleng(line);
if(n==0 || m == 0){
return 0;
}
if(checkForSpecialChar(pattern) != 5000){
switch(pattern[checkForSpecialChar(pattern)]) {
case '.' :
//
if(samePattern(pattern)){
if(m == n)
return 1;
else
return 0;
}
else if(firstOccur(pattern, '+') != 5000){
if (patternDotAfterPlus(pattern, line)){
return 1;
}
return 0;
}
else if(firstOccur(pattern, '?') != 5000){
if (m==n-quesCount(pattern)){
return 1;
}
return 0;
}
else if (matchPatternDots(pattern, line)){
return 1;
}
else
return 0;
case '\\' :
if(n==2){
if(pattern[1] == '?'){
pattern = "?";
if(basicRgrep(pattern, line, 1, m))
return 1;
}
else if(pattern[1] == '.'){
pattern = ".";
if(basicRgrep(pattern, line, 1, m))
return 1;
}
return 0;
}
else if(n==3){
if(pattern[1] == '\\'){
if(pattern[2] == '+'){
pattern = "\\";
for(int i = 0; i < m; i++){
if(line[i] == pattern[0])
return 1;
}
}
}
}
else{
if(escapeChar(pattern, line))
return 1;
else
return 0;
}
case '+' :
if(n==1)
return 1;
if(multiplePlus(pattern, line))
return 1;
if(patternSinglePlus(pattern, line))
return 1;
return 0;
case '?' :
if(n==1){
pattern = "?";
if(basicRgrep(pattern, line, 1, m))
return 1;
}
else if(quesMark(pattern, line))
return 1;
else
return 0;
default :
return 0;
}
}
else{
if(basicRgrep(pattern , line, n, m))
return 1;
}
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;
}
/*Checks for the first special character not proceeded by a \(escape char)
returns index of special char or returns 5000*/
int checkForSpecialChar(char* pattern){
int n = strleng(pattern);
//checks for .
for(int i = 0; i < n; i++){
if(pattern[i] == '.'){
if(i != 0 && pattern[i-1] != '\\'){
return i;
}
else if (i == 0)
return i;
}
}
//checks for +
for(int i = 0; i < n; i++){
if(pattern[i] == '+'){
if(i != 0 && pattern[i-1] != '\\'){
return i;
}
else if (i == 0)
return i;
}
}
//checks for ?
for(int i = 0; i < n; i++){
if(pattern[i] == '?'){
if(i != 0 && pattern[i-1] != 92){
return i;
}
else if (i == 0)
return i;
}
}
//checks for '\\'
for(int i = 0; i < n; i++){
if(pattern[i] == '\\'){
if(i != 0 && pattern[i-1] != 92){
return i;
}
else if (i == 0)
return i;
}
}
return 5000;
}
//Checks for matchs to dot-pattern
int matchPatternDots(char* pattern,char* line){
int n = strleng(pattern);
int m = strleng(line);
int index = 0;
int matches = 0;
int slashes = slashesCount(pattern);
for(int i = 0; i< m; i++){
if(pattern[index] == '\\')
index++;
if(pattern[index] == line[i] || pattern[index] == '.'){
matches++;
if(pattern[index] != '\0')
index++;
}
if(line[i]=='\\'){
i++;
if(line[i]=='n'){
matches =0;
index=0;
}
}
if(matches == n|| matches == n - slashes)
return 1;
}
return 0;
}
//Checks for matches in pattern after a +
int patternDotAfterPlus(char* pattern, char* line){
int n = strlengIgnoreSlash(pattern);
int m = strleng(line);
int matches= 0;
int i = 0;
int plusDots = plusDotsCount(pattern);
for(int j = 0; j < m; j++){
if(pattern[i] == '\\'){
if(i+2 < m && pattern[i+1] == 'n'){
i = i+1;
}
i++;
}
if(i+1 > m && pattern[i] == line[j]){
matches++;
if (i != n){
i++;
}
}
else if(pattern[i] == '+' && pattern[i+1] != '\\'){
if (i != n){
i++;
}
}
else if(pattern[i] == '+'|| pattern[i] == '.'){
if (i != n){
i++;
}
}
else if(pattern[i] == line[j]){
matches++;
if(line[j] == '\n'){
line[j] = '\0';
}
if (i != n){
i++;
}
}
}
if( matches == n-1 ||matches == n || matches >= n - plusDots -1 ){
return 1;
}
return 0;
}
int patternSinglePlus(char* pattern,char* line){
int n = strleng(pattern);
int m = strleng(line);
int p = firstOccur(pattern, '+');
int matches = 0;
int found = 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{
for(int i = 0; i< m; i++){
if(line[i] == pattern[0]){
matches = 1;
}
}
for(int i = 1; i< n; i++){
if(line[i] == pattern[i]){
matches++;
}
}
if(p < n){
char p2 = pattern[p+1];
for(int i = p+1; i< n && i < m; i++){
if(line[firstOccur(line, p2)] == pattern[i]){
matches++;
}
}
}
if(matches == n-1 || matches == n)
return 1;
}
return 0;
}
/*Checks for and deals with multiple plus signs*/
int multiplePlus(char* pattern,char* line){
int n = strleng(pattern);
int m = strleng(line);
int matches = 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;
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]){
matches++;
break;
}
}
}
if(matches >= plus)
return 1;
}
return 0;
}
/*Deals with question mark as first occurence in pattern*/
int quesMark(char * pattern, char* line){
int n = strleng(pattern);
int m = strleng(line);
if(m == 0)
return 0;
int q = firstOccur(pattern, '?');
char newPat[n];
char newPat2[n-1];
int index = 0;
int index2 = 0;
for(int i = 0; i < n; i++){
if(i== n-1){
newPat2[index2] = '\0';
}
if(pattern[i] != '?' && pattern[i+1] != '?')
newPat2[index2++] = pattern[i];
if(i == n){
newPat[index] = '\0';
}
else if(i+1< n && pattern[i+1] == '?'&& pattern[i] != '\\'){
newPat[index++] = '.';
}
else if(pattern[i] == '?'){
if(pattern[i+1] != '\0')
newPat[index++] = pattern[i+1];
else
newPat[i] = '\0';
}
else if(i == n-1)
newPat[index] = '\0';
else
newPat[index++] = pattern[i];
}
if(q != 5000){
if(n<=3){
if(matchOne(newPat2,line))
return 1;
}
if(matchPatternDots(newPat, line))
return 1;
}
return 0;
}
int escapeChar(char* pattern,char* line){
int n = strleng(pattern);
int m = strleng(line);
int index = 0;
int matches = 0;
for(int i = 0; i < m; i++){
if(line[i] == pattern[index]){
matches++;
if(index < n)
index++;
}
}
if(matches == n)
return 1;
return 0;
}
/*Non special char rgrep; n strleng of pattern, m strleng of line*/
int basicRgrep(char* pattern , char* line, int n, int m){
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;
}
//Finds matches to patterns of one letter
int matchOne(char* pattern,char* line){
int m = strleng(line);
for(int i = 0; i < m; i++){
if(line[i] == pattern[0])
return 1;
}
return 0;
}
//Checks if pattern is all the same
int samePattern(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 ignoring \n and \r*/
int strleng(char* string){
int i = 0;
while(string[i] != '\0' && string[i] != '\n'&& string[i] != '\r'){
i++;
}
return i;
}
/* Strlen function ignoring \\*/
int strlengIgnoreSlash(char* string){
int i = 0;
int count = 0;
while(string[i] != '\0'){
i++;
count++;
if(string[i] == '\\'){
count--;
}
}
return count;
}
/*Returns index of first char in string that matches char, returns 5000 if no matches*/
int firstOccur(char * line, char c){
int n = strlengIgnoreSlash(line);
for(int i = 0; i < n; i++){
if(i-1 < n && line[i] == c && line[i-1] != '\\')
return i;
}
return 5000;
}
/*Counts the number of dots and plus's in a pattern*/
int plusDotsCount(char* pattern){
int i = 0;
int count = 0;
while(pattern[i] != '\0'){
if((pattern[i] == '.' && pattern[i-1] != '\\' )||( pattern[i] == '+' && pattern[i-1] != '\\')){
count++;
}
i++;
}
return count;
}
/*Counts the number of slashes in a pattern */
int slashesCount(char* pattern){
int i = 0;
int count = 0;
while(pattern[i] != '\0'){
if(pattern[i] == '\\'){
count++;
}
i++;
}
return count;
}
/*Counts question marks in pattern*/
int quesCount(char* pattern){
int i = 0;
int count = 0;
while(pattern[i] != '\0'){
if(pattern[i] == '?' && pattern[i-1] != '\\'){
count++;
}
i++;
}
return count;
}