regex_parser.c

Thu, 26 Jan 2012 15:55:52 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 26 Jan 2012 15:55:52 +0100
changeset 27
95a958e3de88
child 28
72a98cbcb9f1
permissions
-rw-r--r--

added regexp_parser struct and compile function

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;
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 }
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 return ret;
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 }
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 void destroy_regex_parser_t(regex_parser_t* parser) {
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 destroy_string_list_t(parser->pattern_list);
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 free(parser);
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 }
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 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
27 return parser->pattern_match > 0;
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 }
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 void regex_compile_all(regex_parser_t* parser) {
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 size_t pcount = parser->pattern_list->count;
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 if (pcount > 0) {
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 if (parser->compiled_patterns != NULL) {
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 free(parser->compiled_patterns);
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 }
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 parser->compiled_patterns = calloc(pcount, sizeof(regex_t));
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 regex_t* re = malloc(sizeof(regex_t));
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 for (int i = 0 ; i < pcount ; i++) {
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 if (regcomp(re, parser->pattern_list->items[i],
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 REG_EXTENDED|REG_NOSUB) == 0) {
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 parser->compiled_patterns[i] = re;
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 } else {
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 fprintf(stderr, "Cannot compile: %s\n",
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 (parser->pattern_list->items[i]));
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 parser->compiled_patterns[i] = NULL;
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 }
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 }
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 }
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 }

mercurial