Mercurial > hg > index.fcgi > rcg > rcg-1
changeset 2:4efb50ce4c9c
rcg.c: expect only one match at a time from regexec()
author | pang@apollo5.intellisis |
---|---|
date | Mon, 05 Apr 2010 21:02:51 -0700 |
parents | 2b95aa9e4e46 |
children | fe4b316b4386 |
files | rcg.c |
diffstat | 1 files changed, 25 insertions(+), 26 deletions(-) [+] |
line diff
1.1 --- a/rcg.c Tue Mar 30 17:01:04 2010 -0700 1.2 +++ b/rcg.c Mon Apr 05 21:02:51 2010 -0700 1.3 @@ -6,7 +6,6 @@ 1.4 #include <string.h> 1.5 1.6 const size_t BUFSIZE = 5000; 1.7 -const size_t REMSIZE = 3; 1.8 1.9 const char *CLR_START = "-CLR_START-"; 1.10 const char *CLR_END = "-CLR_END-"; 1.11 @@ -16,60 +15,60 @@ 1.12 char *buf = calloc(BUFSIZE, sizeof(char)); 1.13 1.14 regex_t *re = calloc(1, sizeof(regex_t)); 1.15 - regcomp(re, "(Hello)", REG_EXTENDED); 1.16 + regcomp(re, "Hello", REG_EXTENDED); 1.17 1.18 - regmatch_t *rem = calloc(REMSIZE, sizeof(regmatch_t)); 1.19 + regmatch_t *rem = calloc(1, sizeof(regmatch_t)); 1.20 1.21 size_t CLR_START_len = strlen(CLR_START); 1.22 size_t CLR_END_len = strlen(CLR_END); 1.23 1.24 - size_t out_len = strlen(buf); 1.25 - if (out_len >= BUFSIZE) 1.26 - out_len = BUFSIZE - 1; 1.27 - 1.28 - char *out = calloc(out_len, sizeof(char)); 1.29 + char *out = NULL; 1.30 unsigned int out_pos = 0; 1.31 + unsigned int buf_pos = 0; 1.32 1.33 while (fgets(buf, BUFSIZE, stdin)) 1.34 { 1.35 - regexec(re, buf, REMSIZE, rem, 0); 1.36 + int i = 0; 1.37 + int so = -1; 1.38 + int eo = -1; 1.39 1.40 - int i; 1.41 + do 1.42 + { 1.43 + regexec(re, &buf[buf_pos], 1, rem, 0); 1.44 1.45 - for (i = 0; i < REMSIZE; i++) 1.46 - fprintf(stderr, "%d %d \n", rem[i].rm_so, rem[i].rm_eo); //d// 20100327 PBA 1.47 + size_t out_len = strlen(&buf[buf_pos]); 1.48 + if (out_len >= (BUFSIZE - buf_pos)) 1.49 + out_len = (BUFSIZE - buf_pos) - 1; 1.50 1.51 - for (i = 1; i < REMSIZE; i++) 1.52 - { 1.53 - int so = rem[i].rm_so; 1.54 - int eo = rem[i].rm_eo; 1.55 - int nso = BUFSIZE; 1.56 + so = rem[0].rm_so; 1.57 + eo = rem[0].rm_eo; 1.58 1.59 - if ((i + 1) < REMSIZE) 1.60 - nso = rem[i + 1].rm_so; 1.61 - if (nso == -1) 1.62 - nso = BUFSIZE; 1.63 + fprintf(stderr, "%d %d \n", so, eo); //d// 20100327 PBA 1.64 1.65 if (so >= 0 && eo >= 0) 1.66 { 1.67 int match_len = eo - so; 1.68 out_len += CLR_START_len + CLR_END_len; 1.69 out = (char *)realloc(out, out_len*sizeof(char)); 1.70 - if (i == 0) 1.71 + if (i++ == 0) 1.72 { 1.73 - strncpy(&out[out_pos], buf, so); 1.74 + strncpy(out, buf, so); 1.75 out_pos += so; 1.76 + buf_pos += so; 1.77 } 1.78 strncpy(&out[out_pos], CLR_START, CLR_START_len); 1.79 out_pos += CLR_START_len; 1.80 - strncpy(&out[out_pos], &buf[so], match_len); 1.81 + strncpy(&out[out_pos], &buf[buf_pos], match_len); 1.82 out_pos += match_len; 1.83 strncpy(&out[out_pos], CLR_END, CLR_END_len); 1.84 out_pos += CLR_END_len; 1.85 - strncpy(&out[out_pos], &buf[eo], nso - eo); 1.86 - out_pos += nso - eo; 1.87 + 1.88 + buf_pos += match_len; 1.89 } 1.90 + else 1.91 + strncpy(&out[out_pos], &buf[eo], out_len - out_pos); 1.92 } 1.93 + while (so >= 0 && eo >= 0); 1.94 1.95 fputs(out, stdout); 1.96 }