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