// find first occurence of pattern in line OR NULLPTR if none int Regexpr::find_first(char *start) { int err; start_ptr = end_ptr = line; if ((err = search()) != 0) last_occurence(start); return err; }
// find next occurence but begin search at end of the previous occurence int Regexpr::find_next_skipcurrent(char *start) { int err; if (*end_ptr == eol) return 0; end_ptr = start_ptr = end_ptr+1; // end of previous occurence if ((err = search()) != 0) last_occurence(start); return err; }
// find next occurence OR NULLPTR int Regexpr::find_next(char *start) { int err; if (*start_ptr == eol) return 0; end_ptr = ++start_ptr; // position start/end_ptr to next character if ((err = search()) != 0) last_occurence(start); return err; }
int main(int argc, char *argv[]) { // int bytes_read; size_t nbytes = 100; char *my_string, c; puts ("Please enter a line of text."); my_string = (char *) malloc (nbytes + 1); getline (&my_string, &nbytes, stdin); puts ("Please enter a character"); scanf("%c", &c); printf("%d\n", last_occurence(c, my_string)); free(my_string); return 0; }
// main function for searching the pattern // handles the '|' meta symbol // returns 0 or 1 and sets pointers int Regexpr::search(char **start,char **end) { char *cur_startptr; patptr = pattern; // init. patptr ignore_case = 0; lineptr = start_ptr; if (_search(&cur_startptr) <= 0) return 0; start_ptr = cur_startptr; end_ptr = lineptr-1; // points on last character matching pattern if (start && end) last_occurence(start,end); // set up start and end return 1; }