view rcg.c @ 1:2b95aa9e4e46

initial add rcg.c
author pang@sojourner.intellisis
date Tue, 30 Mar 2010 17:01:04 -0700
parents
children 4efb50ce4c9c
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <regex.h>
5 #define __USE_GNU
6 #include <string.h>
8 const size_t BUFSIZE = 5000;
9 const size_t REMSIZE = 3;
11 const char *CLR_START = "-CLR_START-";
12 const char *CLR_END = "-CLR_END-";
14 int main(int argc, char **argv)
15 {
16 char *buf = calloc(BUFSIZE, sizeof(char));
18 regex_t *re = calloc(1, sizeof(regex_t));
19 regcomp(re, "(Hello)", REG_EXTENDED);
21 regmatch_t *rem = calloc(REMSIZE, sizeof(regmatch_t));
23 size_t CLR_START_len = strlen(CLR_START);
24 size_t CLR_END_len = strlen(CLR_END);
26 size_t out_len = strlen(buf);
27 if (out_len >= BUFSIZE)
28 out_len = BUFSIZE - 1;
30 char *out = calloc(out_len, sizeof(char));
31 unsigned int out_pos = 0;
33 while (fgets(buf, BUFSIZE, stdin))
34 {
35 regexec(re, buf, REMSIZE, rem, 0);
37 int i;
39 for (i = 0; i < REMSIZE; i++)
40 fprintf(stderr, "%d %d \n", rem[i].rm_so, rem[i].rm_eo); //d// 20100327 PBA
42 for (i = 1; i < REMSIZE; i++)
43 {
44 int so = rem[i].rm_so;
45 int eo = rem[i].rm_eo;
46 int nso = BUFSIZE;
48 if ((i + 1) < REMSIZE)
49 nso = rem[i + 1].rm_so;
50 if (nso == -1)
51 nso = BUFSIZE;
53 if (so >= 0 && eo >= 0)
54 {
55 int match_len = eo - so;
56 out_len += CLR_START_len + CLR_END_len;
57 out = (char *)realloc(out, out_len*sizeof(char));
58 if (i == 0)
59 {
60 strncpy(&out[out_pos], buf, so);
61 out_pos += so;
62 }
63 strncpy(&out[out_pos], CLR_START, CLR_START_len);
64 out_pos += CLR_START_len;
65 strncpy(&out[out_pos], &buf[so], match_len);
66 out_pos += match_len;
67 strncpy(&out[out_pos], CLR_END, CLR_END_len);
68 out_pos += CLR_END_len;
69 strncpy(&out[out_pos], &buf[eo], nso - eo);
70 out_pos += nso - eo;
71 }
72 }
74 fputs(out, stdout);
75 }
77 return 0;
78 }