From e5fc4ed9fb277d92ee24cd8e1bf5e305eb8e18f4 Mon Sep 17 00:00:00 2001 From: KatolaZ Date: Thu, 3 May 2018 22:53:01 +0100 Subject: implemented REGEXP match on addresses --- buff.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) (limited to 'buff.c') diff --git a/buff.c b/buff.c index 89ca82f..aa66a67 100644 --- a/buff.c +++ b/buff.c @@ -1,4 +1,5 @@ #include "buff.h" +#include line_t* __search_pos(int addr){ @@ -108,6 +109,8 @@ void print_lines(char lineno){ int move_to_line(int addr, char print){ + fprintf(stderr, " >>>> move_to_line: addr: %d pos: %d\n", addr, pos); + if (addr > num) return -1; else if (addr >= 0){ @@ -307,3 +310,59 @@ int write_lines(){ printf("%d\n", tot); return 0; } + +/* match either forward (bw=0) or backward (bw=1)*/ +int match(char **c, char bw){ + + char sep = **c, *tmp; + line_t *ptr; + regex_t re; + int l; + + if (bw) + tmp = strtok(*c, "?\n"); + else + tmp = strtok(*c, "/\n"); + fprintf(stderr, " >>>> string to match: \"%s\" bw: %d\n", tmp, bw); + if (!tmp) + return -1; + + if (regcomp(&re, tmp, REG_EXTENDED)){ + return -1; + } + *c += strlen(tmp); + l = pos; + ptr = cur; + do { + fprintf(stderr, "."); + if (bw){ + ptr = ptr->prev; + l = (l-1 + num) % num; + if (!ptr){ + /* FIXME: if we keep b_end updated we can avoid this search */ + ptr = __search_pos(num); + l = num; + } + } + else{ + ptr = ptr->next; + l = (l+1) % num; + if (! ptr){ + ptr = b_start; + l = 1; + } + } + if (!l) l = num; + if (!regexec(&re, (char *) ptr->c , (size_t) 0, NULL, 0)){ + regfree(&re); + return l; + } + fprintf(stderr, " %d\n", l); + } while(l != pos); + fprintf(stderr, " >>>> match: returning\n"); + regfree(&re); + return -1; +} + + + -- cgit v1.2.3