1 /* |
|
2 * regex_parser.c |
|
3 * |
|
4 * Created on: 26.01.2012 |
|
5 * Author: Mike |
|
6 */ |
|
7 |
|
8 #include "regex_parser.h" |
|
9 |
|
10 regex_parser_t* new_regex_parser_t() { |
|
11 regex_parser_t* ret = malloc(sizeof(regex_parser_t)); |
|
12 if (ret != NULL) { |
|
13 ret->pattern_list = new_string_list_t(); |
|
14 ret->matched_lines = 0; |
|
15 ret->pattern_match = 0; |
|
16 ret->compiled_patterns = NULL; |
|
17 ret->compiled_pattern_count = 0; |
|
18 } |
|
19 return ret; |
|
20 } |
|
21 |
|
22 void regex_destcomppats(regex_parser_t* parser) { |
|
23 if (parser->compiled_patterns != NULL) { |
|
24 for (int i = 0 ; i < parser->compiled_pattern_count ; i++) { |
|
25 if (parser->compiled_patterns[i] != NULL) { |
|
26 free(parser->compiled_patterns[i]); |
|
27 } |
|
28 } |
|
29 free(parser->compiled_patterns); |
|
30 parser->compiled_patterns = NULL; |
|
31 parser->compiled_pattern_count = 0; |
|
32 } |
|
33 } |
|
34 |
|
35 void destroy_regex_parser_t(regex_parser_t* parser) { |
|
36 regex_destcomppats(parser); |
|
37 destroy_string_list_t(parser->pattern_list); |
|
38 free(parser); |
|
39 } |
|
40 |
|
41 bool regex_parser_matching(regex_parser_t* parser) { |
|
42 return parser->pattern_match > 0; |
|
43 } |
|
44 |
|
45 int regex_parser_do(regex_parser_t* parser, char* input) { |
|
46 int err = REG_NOMATCH; |
|
47 if (parser->compiled_pattern_count > 0) { |
|
48 regmatch_t match; |
|
49 |
|
50 if (regex_parser_matching(parser)) { |
|
51 parser->matched_lines++; |
|
52 |
|
53 err = regexec(parser->compiled_patterns[parser->pattern_match], |
|
54 input, 1, &match, 0); |
|
55 if (err > 0 && err != REG_NOMATCH) { |
|
56 fprintf(stderr, "Regex-Error: 0x%08x", err); |
|
57 } |
|
58 if (err == 0) { |
|
59 parser->pattern_match = 0; |
|
60 /* do not match line, if it does not end with the pattern */ |
|
61 if (match.rm_eo < strlen(input)) { |
|
62 parser->matched_lines--; |
|
63 } |
|
64 } |
|
65 } else { |
|
66 for (int i = 0 ; i < parser->compiled_pattern_count - 1 ; i += 2) { |
|
67 err = regexec(parser->compiled_patterns[i], input, 1, &match, 0); |
|
68 if (err > 0 && err != REG_NOMATCH) { |
|
69 fprintf(stderr, "Regex-Error: 0x%08x", err); |
|
70 } |
|
71 if (err == 0) { |
|
72 parser->pattern_match = i+1; |
|
73 parser->matched_lines = 0; |
|
74 /* Check, if end pattern is also in this line */ |
|
75 regex_parser_do(parser, input); |
|
76 /* do not match line, if it does not start with the pattern */ |
|
77 if (match.rm_so > 0 && parser->matched_lines > 0) { |
|
78 parser->matched_lines--; |
|
79 } |
|
80 break; |
|
81 } |
|
82 } |
|
83 } |
|
84 } |
|
85 return err; |
|
86 } |
|
87 |
|
88 bool regex_compile_all(regex_parser_t* parser) { |
|
89 bool success = true; |
|
90 size_t pcount = parser->pattern_list->count; |
|
91 if (pcount > 0) { |
|
92 regex_destcomppats(parser); |
|
93 parser->compiled_patterns = calloc(pcount, sizeof(regex_t)); |
|
94 parser->compiled_pattern_count = pcount; |
|
95 |
|
96 regex_t* re; |
|
97 for (int i = 0 ; i < pcount ; i++) { |
|
98 re = malloc(sizeof(regex_t)); |
|
99 if (regcomp(re, parser->pattern_list->items[i], REG_EXTENDED) == 0) { |
|
100 parser->compiled_patterns[i] = re; |
|
101 } else { |
|
102 fprintf(stderr, "Cannot compile pattern: %s\n", |
|
103 (parser->pattern_list->items[i])); |
|
104 parser->compiled_patterns[i] = NULL; |
|
105 success = false; |
|
106 } |
|
107 } |
|
108 } |
|
109 return success; |
|
110 } |
|