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