regex_parser.c

changeset 27
95a958e3de88
child 28
72a98cbcb9f1
equal deleted inserted replaced
26:853a1181884b 27:95a958e3de88
1 /*
2 * regex_parser.c
3 *
4 * Created on: 26.01.2012
5 * Author: fox3049
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 }
18 return ret;
19 }
20
21 void destroy_regex_parser_t(regex_parser_t* parser) {
22 destroy_string_list_t(parser->pattern_list);
23 free(parser);
24 }
25
26 bool regex_parser_matching(regex_parser_t* parser) {
27 return parser->pattern_match > 0;
28 }
29
30 void regex_compile_all(regex_parser_t* parser) {
31 size_t pcount = parser->pattern_list->count;
32 if (pcount > 0) {
33 if (parser->compiled_patterns != NULL) {
34 free(parser->compiled_patterns);
35 }
36 parser->compiled_patterns = calloc(pcount, sizeof(regex_t));
37
38 regex_t* re = malloc(sizeof(regex_t));
39 for (int i = 0 ; i < pcount ; i++) {
40 if (regcomp(re, parser->pattern_list->items[i],
41 REG_EXTENDED|REG_NOSUB) == 0) {
42 parser->compiled_patterns[i] = re;
43 } else {
44 fprintf(stderr, "Cannot compile: %s\n",
45 (parser->pattern_list->items[i]));
46 parser->compiled_patterns[i] = NULL;
47 }
48 }
49 }
50 }

mercurial