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