comparison rcg.c @ 3:fe4b316b4386

rcg.c: working multiple regexec matches; couple of error handling functions
author paulo@twcdns.fastsearch.net
date Mon, 05 Apr 2010 22:37:37 -0700
parents 4efb50ce4c9c
children 99770a6b967d
comparison
equal deleted inserted replaced
1:284dbc588c70 2:a8e756ccd89d
8 const size_t BUFSIZE = 5000; 8 const size_t BUFSIZE = 5000;
9 9
10 const char *CLR_START = "-CLR_START-"; 10 const char *CLR_START = "-CLR_START-";
11 const char *CLR_END = "-CLR_END-"; 11 const char *CLR_END = "-CLR_END-";
12 12
13 int re_error(int errcode, const regex_t *re)
14 {
15 char *err_string = calloc(BUFSIZE, sizeof(char));
16 regerror(errcode, re, err_string, BUFSIZE*sizeof(char));
17 fprintf(stderr, "%s \n", err_string);
18 free(err_string);
19 return errcode;
20 }
21
22 int realloc_error()
23 {
24 fprintf(stderr, "realloc() failure \n");
25 return 1;
26 }
27
13 int main(int argc, char **argv) 28 int main(int argc, char **argv)
14 { 29 {
15 char *buf = calloc(BUFSIZE, sizeof(char)); 30 char *buf = calloc(BUFSIZE, sizeof(char));
16 31
32 int re_err;
17 regex_t *re = calloc(1, sizeof(regex_t)); 33 regex_t *re = calloc(1, sizeof(regex_t));
18 regcomp(re, "Hello", REG_EXTENDED); 34 re_err = regcomp(re, "Hello", REG_EXTENDED);
35 if (re_err != 0)
36 exit(re_error(re_err, re));
19 37
20 regmatch_t *rem = calloc(1, sizeof(regmatch_t)); 38 regmatch_t *rem = calloc(1, sizeof(regmatch_t));
21 39
22 size_t CLR_START_len = strlen(CLR_START); 40 size_t CLR_START_len = strlen(CLR_START);
23 size_t CLR_END_len = strlen(CLR_END); 41 size_t CLR_END_len = strlen(CLR_END);
24 42
25 char *out = NULL;
26 unsigned int out_pos = 0;
27 unsigned int buf_pos = 0;
28 43
29 while (fgets(buf, BUFSIZE, stdin)) 44 while (fgets(buf, BUFSIZE, stdin))
30 { 45 {
31 int i = 0;
32 int so = -1; 46 int so = -1;
33 int eo = -1; 47 int eo = -1;
34 48
35 do 49 size_t out_len = strlen(buf);
50 char *out = calloc(out_len, sizeof(char));
51 unsigned int out_pos = 0;
52 unsigned int buf_pos = 0;
53
54 while (re_err != REG_NOMATCH)
36 { 55 {
37 regexec(re, &buf[buf_pos], 1, rem, 0); 56 re_err = regexec(re, &buf[buf_pos], 1, rem, 0);
38 57
39 size_t out_len = strlen(&buf[buf_pos]); 58 //fprintf(stderr, "%s", &buf[buf_pos]); //d// 20100405 PBA
59
40 if (out_len >= (BUFSIZE - buf_pos)) 60 if (out_len >= (BUFSIZE - buf_pos))
41 out_len = (BUFSIZE - buf_pos) - 1; 61 out_len = (BUFSIZE - buf_pos) - 1;
42 62
43 so = rem[0].rm_so; 63 so = rem[0].rm_so;
44 eo = rem[0].rm_eo; 64 eo = rem[0].rm_eo;
45 65
46 fprintf(stderr, "%d %d \n", so, eo); //d// 20100327 PBA 66 //fprintf(stderr, "%d %d \n", so, eo); //d// 20100327 PBA
47 67
48 if (so >= 0 && eo >= 0) 68 if (re_err != REG_NOMATCH && so >= 0 && eo >= 0)
49 { 69 {
50 int match_len = eo - so; 70 int match_len = eo - so;
51 out_len += CLR_START_len + CLR_END_len; 71 out_len += CLR_START_len + CLR_END_len;
52 out = (char *)realloc(out, out_len*sizeof(char)); 72 out = (char *)realloc(out, out_len*sizeof(char));
53 if (i++ == 0) 73 if (!out)
54 { 74 exit(realloc_error());
55 strncpy(out, buf, so); 75 strncpy(&out[out_pos], &buf[buf_pos], so);
56 out_pos += so; 76 out_pos += so;
57 buf_pos += so; 77 buf_pos += so;
58 }
59 strncpy(&out[out_pos], CLR_START, CLR_START_len); 78 strncpy(&out[out_pos], CLR_START, CLR_START_len);
60 out_pos += CLR_START_len; 79 out_pos += CLR_START_len;
61 strncpy(&out[out_pos], &buf[buf_pos], match_len); 80 strncpy(&out[out_pos], &buf[buf_pos], match_len);
62 out_pos += match_len; 81 out_pos += match_len;
63 strncpy(&out[out_pos], CLR_END, CLR_END_len); 82 strncpy(&out[out_pos], CLR_END, CLR_END_len);
64 out_pos += CLR_END_len; 83 out_pos += CLR_END_len;
65 84
66 buf_pos += match_len; 85 buf_pos += match_len;
67 } 86 }
68 else 87 else
69 strncpy(&out[out_pos], &buf[eo], out_len - out_pos); 88 strncpy(&out[out_pos], &buf[buf_pos], out_len - out_pos);
70 } 89 }
71 while (so >= 0 && eo >= 0);
72 90
73 fputs(out, stdout); 91 fputs(out, stdout);
92 free(out);
74 } 93 }
75 94
76 return 0; 95 return 0;
77 } 96 }