Thu, 23 Aug 2018 17:58:38 +0200
adds test file skeleton to build system
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * Copyright 2017 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 */
32 #include "regex_parser.h"
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 }
46 void regex_parser_reset(regex_parser_t* parser) {
47 parser->pattern_match = parser->matched_lines = 0;
48 }
50 void regex_destcomppats(regex_parser_t* parser) {
51 if (parser->compiled_patterns != NULL) {
52 for (int i = 0 ; i < parser->compiled_pattern_count ; i++) {
53 if (parser->compiled_patterns[i] != NULL) {
54 free(parser->compiled_patterns[i]);
55 }
56 }
57 free(parser->compiled_patterns);
58 parser->compiled_patterns = NULL;
59 parser->compiled_pattern_count = 0;
60 }
61 }
63 void destroy_regex_parser_t(regex_parser_t* parser) {
64 regex_destcomppats(parser);
65 destroy_string_list_t(parser->pattern_list);
66 free(parser);
67 }
69 bool regex_parser_matching(regex_parser_t* parser) {
70 return parser->pattern_match > 0;
71 }
73 int regex_parser_do(regex_parser_t* parser, char* input) {
74 int err = REG_NOMATCH;
75 if (parser->compiled_pattern_count > 0) {
76 regmatch_t match;
78 if (regex_parser_matching(parser)) {
79 parser->matched_lines++;
81 err = regexec(parser->compiled_patterns[parser->pattern_match],
82 input, 1, &match, 0);
83 if (err > 0 && err != REG_NOMATCH) {
84 fprintf(stderr, "Regex-Error: 0x%08x", err);
85 }
86 if (err == 0) {
87 parser->pattern_match = 0;
88 /* do not match line, if it does not end with the pattern */
89 if (match.rm_eo < strlen(input)) {
90 parser->matched_lines--;
91 }
92 }
93 } else {
94 for (int i = 0 ; i < parser->compiled_pattern_count - 1 ; i += 2) {
95 err = regexec(parser->compiled_patterns[i], input, 1, &match, 0);
96 if (err > 0 && err != REG_NOMATCH) {
97 fprintf(stderr, "Regex-Error: 0x%08x", err);
98 }
99 if (err == 0) {
100 parser->pattern_match = i+1;
101 parser->matched_lines = 0;
102 /* Check, if end pattern is also in this line */
103 regex_parser_do(parser, input);
104 /* do not match line, if it does not start with the pattern */
105 if (match.rm_so > 0 && parser->matched_lines > 0) {
106 parser->matched_lines--;
107 }
108 break;
109 }
110 }
111 }
112 }
113 return err;
114 }
116 bool regex_compile_all(regex_parser_t* parser) {
117 bool success = true;
118 size_t pcount = parser->pattern_list->count;
119 if (pcount > 0) {
120 regex_destcomppats(parser);
121 parser->compiled_patterns = calloc(pcount, sizeof(regex_t));
122 parser->compiled_pattern_count = pcount;
124 regex_t* re;
125 for (int i = 0 ; i < pcount ; i++) {
126 re = malloc(sizeof(regex_t));
127 if (regcomp(re, parser->pattern_list->items[i], REG_EXTENDED) == 0) {
128 parser->compiled_patterns[i] = re;
129 } else {
130 fprintf(stderr, "Cannot compile pattern: %s\n",
131 (parser->pattern_list->items[i]));
132 parser->compiled_patterns[i] = NULL;
133 success = false;
134 }
135 }
136 }
137 return success;
138 }