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