regex_parser.c

Thu, 02 Feb 2012 16:55:51 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 02 Feb 2012 16:55:51 +0100
changeset 29
fa625066ae52
parent 28
72a98cbcb9f1
permissions
-rw-r--r--

fixed author note

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
29
fa625066ae52 fixed author note
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
5 * Author: Mike
27
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
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
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
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
22 void regex_destcomppats(regex_parser_t* parser) {
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
23 if (parser->compiled_patterns != NULL) {
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
24 for (int i = 0 ; i < parser->compiled_pattern_count ; i++) {
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
25 if (parser->compiled_patterns[i] != NULL) {
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
26 free(parser->compiled_patterns[i]);
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
27 }
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
28 }
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
29 free(parser->compiled_patterns);
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
30 parser->compiled_patterns = NULL;
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
31 parser->compiled_pattern_count = 0;
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
32 }
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
33 }
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
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
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
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
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
45 int regex_parser_do(regex_parser_t* parser, char* input) {
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
46 int err = REG_NOMATCH;
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
47 if (parser->compiled_pattern_count > 0) {
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
48 regmatch_t match;
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
49
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
50 if (regex_parser_matching(parser)) {
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
51 parser->matched_lines++;
27
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52
28
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
53 err = regexec(parser->compiled_patterns[parser->pattern_match],
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
54 input, 1, &match, 0);
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
55 if (err > 0 && err != REG_NOMATCH) {
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
56 fprintf(stderr, "Regex-Error: 0x%08x", err);
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
57 }
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
58 if (err == 0) {
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
59 parser->pattern_match = 0;
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
60 /* do not match line, if it does not end with the pattern */
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
61 if (match.rm_eo < strlen(input)) {
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
62 parser->matched_lines--;
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
63 }
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
64 }
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
65 } else {
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
66 for (int i = 0 ; i < parser->compiled_pattern_count - 1 ; i += 2) {
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
67 err = regexec(parser->compiled_patterns[i], input, 1, &match, 0);
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
68 if (err > 0 && err != REG_NOMATCH) {
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
69 fprintf(stderr, "Regex-Error: 0x%08x", err);
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
70 }
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
71 if (err == 0) {
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
72 parser->pattern_match = i+1;
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
73 parser->matched_lines = 0;
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
74 /* Check, if end pattern is also in this line */
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
75 regex_parser_do(parser, input);
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
76 /* do not match line, if it does not start with the pattern */
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
77 if (match.rm_so > 0 && parser->matched_lines > 0) {
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
78 parser->matched_lines--;
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
79 }
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
80 break;
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
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
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
85 return err;
27
95a958e3de88 added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 }
28
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
87
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
88 bool regex_compile_all(regex_parser_t* parser) {
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
89 bool success = true;
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
90 size_t pcount = parser->pattern_list->count;
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
91 if (pcount > 0) {
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
92 regex_destcomppats(parser);
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
93 parser->compiled_patterns = calloc(pcount, sizeof(regex_t));
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
94 parser->compiled_pattern_count = pcount;
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
95
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
96 regex_t* re;
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
97 for (int i = 0 ; i < pcount ; i++) {
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
98 re = malloc(sizeof(regex_t));
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
99 if (regcomp(re, parser->pattern_list->items[i], REG_EXTENDED) == 0) {
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
100 parser->compiled_patterns[i] = re;
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
101 } else {
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
102 fprintf(stderr, "Cannot compile pattern: %s\n",
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
103 (parser->pattern_list->items[i]));
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
104 parser->compiled_patterns[i] = NULL;
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
105 success = false;
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
106 }
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
107 }
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
108 }
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
109 return success;
72a98cbcb9f1 added regex parser
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
110 }

mercurial