Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #define MAXSIZE 4096 /** * You can use this recommended helper function * Returns true if partial_line matches pattern, starting from * the

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

#include

#include

#define MAXSIZE 4096

/**

* 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) {

int i = 0;

while(pattern[i] != '\0') {

if(partial_line[i] == '\0' || partial_line[i] == ' '){

return 0;

}

if(pattern[i] == '\\') {

if(partial_line[i] != pattern[i+1]) {

return 0;

}

if(pattern[i+2] == '+') {

char prev_char = pattern[i+1];

int inc = 0;

while(partial_line[i+1] == prev_char) {

partial_line++;

inc = 1;

}

if(pattern[i+3] == prev_char && inc)

partial_line--;

pattern = pattern + 3;

}

else {

pattern++;

i++;

}

} else if(pattern[i+1] == '+') {

char prev_char = pattern[i];

if(prev_char == '.'){

while(partial_line[i] != '\0' && partial_line[i] != pattern[i+2]) {

partial_line++;

}

pattern = pattern+2;

} else {

int incremented = 0;

while(partial_line[i] == prev_char) {

partial_line++;

incremented = 1;

}

if(pattern[i+2] == prev_char && incremented)

partial_line--;

pattern = pattern + 2;

if(!incremented)

return 0;

}

} else if(pattern[i] == '.'){

i++;

} else if(pattern[i] == '?') {

pattern++;

} else if(partial_line[i] != pattern[i]) {

return 0;

} else {

i++;

}

}

return 1;

}

//----------------------------------------------------------------------------------

char *check(char *pattern) {

char *pattern2 = (char *)malloc(MAXSIZE*sizeof(char));

int number= 0;

for(int i = 0; pattern[i] != '\0'; i++) {

if(pattern[i] == '\\' && pattern[i+1] == '?') {

pattern2[number] = pattern[i];

number++;

i++;

pattern2[number] = pattern[i];

number++;

} else if(pattern[i+1] != '?' && pattern[i] != '?') {

pattern2[number] = pattern[i];

number++;

}

}

return pattern2;

}

//----------------------------------------------------------------------------------

/**

* 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 length = 0;

int count = 0;

char *partial_line = (char*)malloc(MAXSIZE*sizeof(char));

while(line[length] != '\0') {

length++;

}

for(int i = 0; i

for(int j = 0; j

*(partial_line+j) = line[i+j];

}

if(matches_leading(partial_line, pattern) || matches_leading(partial_line, check(pattern))){

return 1;

}

free(partial_line);

partial_line = (char*)malloc(MAXSIZE*sizeof(char));

count++;

}

return 0;

}

int main(int argc, char **argv) {

if (argc != 2) {

fprintf(stderr, "Usage: %s ", 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;

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

How would you explain this code to somebody? How does a wildcard such as '?' or '+' work on this code.

1 #include 2 #include #define MAXSIZE 4096 6 You can use this recommended helper function 7 Returns true if partial_line matches pattern, starting from 8 the first char of partial_line. 11 int matches_leading (char *partial_line, char *pattern) f int i- 0; while(patternli]10) if(partial_lineli]10' partial_linelil 'n') return 0 18 if(pattern[i] )t if(partial-line [1] != pattern [1+1)) { 20 21 return if (pattern[i+2] f 23 24 25 char prev_char -patternli+1] int inc = 0; while(partial_lineli+1]prev_char)f partial_line++; inc1i 27 28 29 30 31 32 if(patternli+3]prev char && inc) partial_line-; pattern = pattern + 3; else f pattern+ 35 else if(pattern[i+1] )f 37 38 char prev_charpattern[il; if (prev char.) while(partial-line[1] 0' && partial-linel?] != pattern [1+2]) { partial_line++; 1 #include 2 #include #define MAXSIZE 4096 6 You can use this recommended helper function 7 Returns true if partial_line matches pattern, starting from 8 the first char of partial_line. 11 int matches_leading (char *partial_line, char *pattern) f int i- 0; while(patternli]10) if(partial_lineli]10' partial_linelil 'n') return 0 18 if(pattern[i] )t if(partial-line [1] != pattern [1+1)) { 20 21 return if (pattern[i+2] f 23 24 25 char prev_char -patternli+1] int inc = 0; while(partial_lineli+1]prev_char)f partial_line++; inc1i 27 28 29 30 31 32 if(patternli+3]prev char && inc) partial_line-; pattern = pattern + 3; else f pattern+ 35 else if(pattern[i+1] )f 37 38 char prev_charpattern[il; if (prev char.) while(partial-line[1] 0' && partial-linel?] != pattern [1+2]) { partial_line++

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Ehs 2.0 Revolutionizing The Future Of Safety With Digital Technology

Authors: Tony Mudd

1st Edition

B0CN69B3HW, 979-8867463663

More Books

Students also viewed these Databases questions