Mercurial > hg > index.fcgi > rcg > rcg-1
changeset 1:2b95aa9e4e46
initial add rcg.c
author | pang@sojourner.intellisis |
---|---|
date | Tue, 30 Mar 2010 17:01:04 -0700 |
parents | df03fa355b60 |
children | 4efb50ce4c9c |
files | rcg.c |
diffstat | 1 files changed, 78 insertions(+), 0 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/rcg.c Tue Mar 30 17:01:04 2010 -0700 1.3 @@ -0,0 +1,78 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdlib.h> 1.6 +#include <regex.h> 1.7 + 1.8 +#define __USE_GNU 1.9 +#include <string.h> 1.10 + 1.11 +const size_t BUFSIZE = 5000; 1.12 +const size_t REMSIZE = 3; 1.13 + 1.14 +const char *CLR_START = "-CLR_START-"; 1.15 +const char *CLR_END = "-CLR_END-"; 1.16 + 1.17 +int main(int argc, char **argv) 1.18 +{ 1.19 + char *buf = calloc(BUFSIZE, sizeof(char)); 1.20 + 1.21 + regex_t *re = calloc(1, sizeof(regex_t)); 1.22 + regcomp(re, "(Hello)", REG_EXTENDED); 1.23 + 1.24 + regmatch_t *rem = calloc(REMSIZE, sizeof(regmatch_t)); 1.25 + 1.26 + size_t CLR_START_len = strlen(CLR_START); 1.27 + size_t CLR_END_len = strlen(CLR_END); 1.28 + 1.29 + size_t out_len = strlen(buf); 1.30 + if (out_len >= BUFSIZE) 1.31 + out_len = BUFSIZE - 1; 1.32 + 1.33 + char *out = calloc(out_len, sizeof(char)); 1.34 + unsigned int out_pos = 0; 1.35 + 1.36 + while (fgets(buf, BUFSIZE, stdin)) 1.37 + { 1.38 + regexec(re, buf, REMSIZE, rem, 0); 1.39 + 1.40 + int i; 1.41 + 1.42 + for (i = 0; i < REMSIZE; i++) 1.43 + fprintf(stderr, "%d %d \n", rem[i].rm_so, rem[i].rm_eo); //d// 20100327 PBA 1.44 + 1.45 + for (i = 1; i < REMSIZE; i++) 1.46 + { 1.47 + int so = rem[i].rm_so; 1.48 + int eo = rem[i].rm_eo; 1.49 + int nso = BUFSIZE; 1.50 + 1.51 + if ((i + 1) < REMSIZE) 1.52 + nso = rem[i + 1].rm_so; 1.53 + if (nso == -1) 1.54 + nso = BUFSIZE; 1.55 + 1.56 + if (so >= 0 && eo >= 0) 1.57 + { 1.58 + int match_len = eo - so; 1.59 + out_len += CLR_START_len + CLR_END_len; 1.60 + out = (char *)realloc(out, out_len*sizeof(char)); 1.61 + if (i == 0) 1.62 + { 1.63 + strncpy(&out[out_pos], buf, so); 1.64 + out_pos += so; 1.65 + } 1.66 + strncpy(&out[out_pos], CLR_START, CLR_START_len); 1.67 + out_pos += CLR_START_len; 1.68 + strncpy(&out[out_pos], &buf[so], match_len); 1.69 + out_pos += match_len; 1.70 + strncpy(&out[out_pos], CLR_END, CLR_END_len); 1.71 + out_pos += CLR_END_len; 1.72 + strncpy(&out[out_pos], &buf[eo], nso - eo); 1.73 + out_pos += nso - eo; 1.74 + } 1.75 + } 1.76 + 1.77 + fputs(out, stdout); 1.78 + } 1.79 + 1.80 + return 0; 1.81 +}