Thu, 02 Feb 2012 14:17:35 +0100
added regex parser
27
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
1 | /* |
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
2 | * regex_parser.c |
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
3 | * |
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
4 | * Created on: 26.01.2012 |
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
5 | * Author: fox3049 |
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
6 | */ |
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
7 | |
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
8 | #include "regex_parser.h" |
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
9 | |
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
10 | regex_parser_t* new_regex_parser_t() { |
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
11 | regex_parser_t* ret = malloc(sizeof(regex_parser_t)); |
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
12 | if (ret != NULL) { |
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
13 | ret->pattern_list = new_string_list_t(); |
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
14 | ret->matched_lines = 0; |
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
15 | ret->pattern_match = 0; |
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
16 | ret->compiled_patterns = NULL; |
28 | 17 | ret->compiled_pattern_count = 0; |
27
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
18 | } |
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
19 | return ret; |
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
20 | } |
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
21 | |
28 | 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 | ||
27
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
35 | void destroy_regex_parser_t(regex_parser_t* parser) { |
28 | 36 | regex_destcomppats(parser); |
27
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
37 | destroy_string_list_t(parser->pattern_list); |
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
38 | free(parser); |
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
39 | } |
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
40 | |
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
41 | bool regex_parser_matching(regex_parser_t* parser) { |
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
42 | return parser->pattern_match > 0; |
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
43 | } |
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
44 | |
28 | 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++; | |
27
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
52 | |
28 | 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 | } | |
27
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
82 | } |
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
83 | } |
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
84 | } |
28 | 85 | return err; |
27
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
86 | } |
28 | 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 | } |