src/regex_parser.c

changeset 34
fa9bda32de17
parent 29
fa625066ae52
child 36
a7ff583e153f
equal deleted inserted replaced
33:1a2d7298bc82 34:fa9bda32de17
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * Copyright 2011 Mike Becker. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * regex_parser.c
27 *
28 * Created on: 26.01.2012
29 * Author: Mike
30 */
31
32 #include "regex_parser.h"
33
34 regex_parser_t* new_regex_parser_t() {
35 regex_parser_t* ret = malloc(sizeof(regex_parser_t));
36 if (ret != NULL) {
37 ret->pattern_list = new_string_list_t();
38 ret->matched_lines = 0;
39 ret->pattern_match = 0;
40 ret->compiled_patterns = NULL;
41 ret->compiled_pattern_count = 0;
42 }
43 return ret;
44 }
45
46 void regex_destcomppats(regex_parser_t* parser) {
47 if (parser->compiled_patterns != NULL) {
48 for (int i = 0 ; i < parser->compiled_pattern_count ; i++) {
49 if (parser->compiled_patterns[i] != NULL) {
50 free(parser->compiled_patterns[i]);
51 }
52 }
53 free(parser->compiled_patterns);
54 parser->compiled_patterns = NULL;
55 parser->compiled_pattern_count = 0;
56 }
57 }
58
59 void destroy_regex_parser_t(regex_parser_t* parser) {
60 regex_destcomppats(parser);
61 destroy_string_list_t(parser->pattern_list);
62 free(parser);
63 }
64
65 bool regex_parser_matching(regex_parser_t* parser) {
66 return parser->pattern_match > 0;
67 }
68
69 int regex_parser_do(regex_parser_t* parser, char* input) {
70 int err = REG_NOMATCH;
71 if (parser->compiled_pattern_count > 0) {
72 regmatch_t match;
73
74 if (regex_parser_matching(parser)) {
75 parser->matched_lines++;
76
77 err = regexec(parser->compiled_patterns[parser->pattern_match],
78 input, 1, &match, 0);
79 if (err > 0 && err != REG_NOMATCH) {
80 fprintf(stderr, "Regex-Error: 0x%08x", err);
81 }
82 if (err == 0) {
83 parser->pattern_match = 0;
84 /* do not match line, if it does not end with the pattern */
85 if (match.rm_eo < strlen(input)) {
86 parser->matched_lines--;
87 }
88 }
89 } else {
90 for (int i = 0 ; i < parser->compiled_pattern_count - 1 ; i += 2) {
91 err = regexec(parser->compiled_patterns[i], input, 1, &match, 0);
92 if (err > 0 && err != REG_NOMATCH) {
93 fprintf(stderr, "Regex-Error: 0x%08x", err);
94 }
95 if (err == 0) {
96 parser->pattern_match = i+1;
97 parser->matched_lines = 0;
98 /* Check, if end pattern is also in this line */
99 regex_parser_do(parser, input);
100 /* do not match line, if it does not start with the pattern */
101 if (match.rm_so > 0 && parser->matched_lines > 0) {
102 parser->matched_lines--;
103 }
104 break;
105 }
106 }
107 }
108 }
109 return err;
110 }
111
112 bool regex_compile_all(regex_parser_t* parser) {
113 bool success = true;
114 size_t pcount = parser->pattern_list->count;
115 if (pcount > 0) {
116 regex_destcomppats(parser);
117 parser->compiled_patterns = calloc(pcount, sizeof(regex_t));
118 parser->compiled_pattern_count = pcount;
119
120 regex_t* re;
121 for (int i = 0 ; i < pcount ; i++) {
122 re = malloc(sizeof(regex_t));
123 if (regcomp(re, parser->pattern_list->items[i], REG_EXTENDED) == 0) {
124 parser->compiled_patterns[i] = re;
125 } else {
126 fprintf(stderr, "Cannot compile pattern: %s\n",
127 (parser->pattern_list->items[i]));
128 parser->compiled_patterns[i] = NULL;
129 success = false;
130 }
131 }
132 }
133 return success;
134 }

mercurial