| 45 } |
45 } |
| 46 |
46 |
| 47 void regex_destcomppats(regex_parser_t* parser) { |
47 void regex_destcomppats(regex_parser_t* parser) { |
| 48 if (parser->compiled_patterns != NULL) { |
48 if (parser->compiled_patterns != NULL) { |
| 49 for (unsigned i = 0 ; i < parser->compiled_pattern_count ; i++) { |
49 for (unsigned i = 0 ; i < parser->compiled_pattern_count ; i++) { |
| 50 if (parser->compiled_patterns[i] != NULL) { |
50 regfree(parser->compiled_patterns + i); |
| 51 free(parser->compiled_patterns[i]); |
|
| 52 } |
|
| 53 } |
51 } |
| 54 free(parser->compiled_patterns); |
52 free(parser->compiled_patterns); |
| 55 parser->compiled_patterns = NULL; |
53 parser->compiled_patterns = NULL; |
| 56 parser->compiled_pattern_count = 0; |
54 parser->compiled_pattern_count = 0; |
| 57 } |
55 } |
| 87 regex_parser_count_chars(input, 0, strlen(input)); |
85 regex_parser_count_chars(input, 0, strlen(input)); |
| 88 } else { |
86 } else { |
| 89 parser->matched_counted++; |
87 parser->matched_counted++; |
| 90 } |
88 } |
| 91 |
89 |
| 92 err = regexec(parser->compiled_patterns[parser->pattern_match], |
90 err = regexec(parser->compiled_patterns + parser->pattern_match, |
| 93 input, 1, &match, 0); |
91 input, 1, &match, 0); |
| 94 if (err > 0 && err != REG_NOMATCH) { |
92 if (err > 0 && err != REG_NOMATCH) { |
| 95 fprintf(stderr, "Regex-Error: 0x%08x", err); |
93 fprintf(stderr, "Regex-Error: 0x%08x", err); |
| 96 } |
94 } |
| 97 if (err == 0) { |
95 if (err == 0) { |
| 108 } |
106 } |
| 109 } |
107 } |
| 110 } |
108 } |
| 111 } else { |
109 } else { |
| 112 for (unsigned i = 0 ; i < parser->compiled_pattern_count - 1 ; i += 2) { |
110 for (unsigned i = 0 ; i < parser->compiled_pattern_count - 1 ; i += 2) { |
| 113 err = regexec(parser->compiled_patterns[i], input, 1, &match, 0); |
111 err = regexec(parser->compiled_patterns + i, input, 1, &match, 0); |
| 114 if (err > 0 && err != REG_NOMATCH) { |
112 if (err > 0 && err != REG_NOMATCH) { |
| 115 fprintf(stderr, "Regex-Error: 0x%08x", err); |
113 fprintf(stderr, "Regex-Error: 0x%08x", err); |
| 116 } |
114 } |
| 117 if (err == 0) { |
115 if (err == 0) { |
| 118 /* a start pattern matches, start counting */ |
116 /* a start pattern matches, start counting */ |
| 140 } |
138 } |
| 141 return err; |
139 return err; |
| 142 } |
140 } |
| 143 |
141 |
| 144 bool regex_compile_all(regex_parser_t* parser) { |
142 bool regex_compile_all(regex_parser_t* parser) { |
| 145 bool success = true; |
|
| 146 size_t pcount = parser->pattern_list->count; |
143 size_t pcount = parser->pattern_list->count; |
| 147 if (pcount > 0) { |
144 if (pcount > 0) { |
| 148 regex_destcomppats(parser); |
145 regex_destcomppats(parser); |
| 149 parser->compiled_patterns = calloc(pcount, sizeof(regex_t)); |
146 parser->compiled_patterns = calloc(pcount, sizeof(regex_t)); |
| 150 parser->compiled_pattern_count = pcount; |
|
| 151 |
147 |
| 152 regex_t* re; |
148 for (size_t i = 0 ; i < pcount ; i++) { |
| 153 for (unsigned i = 0 ; i < pcount ; i++) { |
149 if (regcomp(parser->compiled_patterns + i, |
| 154 re = malloc(sizeof(regex_t)); |
150 parser->pattern_list->items[i], REG_EXTENDED) != 0) { |
| 155 if (regcomp(re, parser->pattern_list->items[i], REG_EXTENDED) == 0) { |
|
| 156 parser->compiled_patterns[i] = re; |
|
| 157 } else { |
|
| 158 fprintf(stderr, "Cannot compile pattern: %s\n", |
151 fprintf(stderr, "Cannot compile pattern: %s\n", |
| 159 (parser->pattern_list->items[i])); |
152 (parser->pattern_list->items[i])); |
| 160 parser->compiled_patterns[i] = NULL; |
153 parser->compiled_pattern_count = i; |
| 161 success = false; |
154 return false; |
| 162 } |
155 } |
| 163 } |
156 } |
| 164 } |
157 } |
| 165 return success; |
158 parser->compiled_pattern_count = pcount; |
| |
159 return true; |
| 166 } |
160 } |