src/regex_parser.c

Mon, 27 Jul 2020 17:19:56 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 27 Jul 2020 17:19:56 +0200
changeset 61
9c8d768f0244
parent 57
68018eac46c3
child 66
be2084398c37
permissions
-rw-r--r--

adds option to compute individual sums

universe@27 1 /*
universe@34 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@57 3 * Copyright 2018 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@57 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
universe@27 25 */
universe@27 26
universe@27 27 #include "regex_parser.h"
universe@27 28
universe@27 29 regex_parser_t* new_regex_parser_t() {
universe@27 30 regex_parser_t* ret = malloc(sizeof(regex_parser_t));
universe@27 31 if (ret != NULL) {
universe@27 32 ret->pattern_list = new_string_list_t();
universe@27 33 ret->matched_lines = 0;
universe@27 34 ret->pattern_match = 0;
universe@27 35 ret->compiled_patterns = NULL;
universe@28 36 ret->compiled_pattern_count = 0;
universe@27 37 }
universe@27 38 return ret;
universe@27 39 }
universe@27 40
universe@54 41 void regex_parser_reset(regex_parser_t* parser) {
universe@54 42 parser->pattern_match = parser->matched_lines = 0;
universe@54 43 }
universe@54 44
universe@28 45 void regex_destcomppats(regex_parser_t* parser) {
universe@28 46 if (parser->compiled_patterns != NULL) {
universe@28 47 for (int i = 0 ; i < parser->compiled_pattern_count ; i++) {
universe@28 48 if (parser->compiled_patterns[i] != NULL) {
universe@28 49 free(parser->compiled_patterns[i]);
universe@28 50 }
universe@28 51 }
universe@28 52 free(parser->compiled_patterns);
universe@28 53 parser->compiled_patterns = NULL;
universe@28 54 parser->compiled_pattern_count = 0;
universe@28 55 }
universe@28 56 }
universe@28 57
universe@27 58 void destroy_regex_parser_t(regex_parser_t* parser) {
universe@28 59 regex_destcomppats(parser);
universe@27 60 destroy_string_list_t(parser->pattern_list);
universe@27 61 free(parser);
universe@27 62 }
universe@27 63
universe@27 64 bool regex_parser_matching(regex_parser_t* parser) {
universe@27 65 return parser->pattern_match > 0;
universe@27 66 }
universe@27 67
universe@28 68 int regex_parser_do(regex_parser_t* parser, char* input) {
universe@28 69 int err = REG_NOMATCH;
universe@28 70 if (parser->compiled_pattern_count > 0) {
universe@28 71 regmatch_t match;
universe@27 72
universe@28 73 if (regex_parser_matching(parser)) {
universe@28 74 parser->matched_lines++;
universe@28 75
universe@28 76 err = regexec(parser->compiled_patterns[parser->pattern_match],
universe@28 77 input, 1, &match, 0);
universe@28 78 if (err > 0 && err != REG_NOMATCH) {
universe@28 79 fprintf(stderr, "Regex-Error: 0x%08x", err);
universe@28 80 }
universe@28 81 if (err == 0) {
universe@28 82 parser->pattern_match = 0;
universe@28 83 /* do not match line, if it does not end with the pattern */
universe@28 84 if (match.rm_eo < strlen(input)) {
universe@28 85 parser->matched_lines--;
universe@28 86 }
universe@28 87 }
universe@28 88 } else {
universe@28 89 for (int i = 0 ; i < parser->compiled_pattern_count - 1 ; i += 2) {
universe@28 90 err = regexec(parser->compiled_patterns[i], input, 1, &match, 0);
universe@28 91 if (err > 0 && err != REG_NOMATCH) {
universe@28 92 fprintf(stderr, "Regex-Error: 0x%08x", err);
universe@28 93 }
universe@28 94 if (err == 0) {
universe@28 95 parser->pattern_match = i+1;
universe@28 96 parser->matched_lines = 0;
universe@28 97 /* Check, if end pattern is also in this line */
universe@28 98 regex_parser_do(parser, input);
universe@28 99 /* do not match line, if it does not start with the pattern */
universe@28 100 if (match.rm_so > 0 && parser->matched_lines > 0) {
universe@28 101 parser->matched_lines--;
universe@28 102 }
universe@28 103 break;
universe@28 104 }
universe@27 105 }
universe@27 106 }
universe@27 107 }
universe@28 108 return err;
universe@27 109 }
universe@28 110
universe@28 111 bool regex_compile_all(regex_parser_t* parser) {
universe@28 112 bool success = true;
universe@28 113 size_t pcount = parser->pattern_list->count;
universe@28 114 if (pcount > 0) {
universe@28 115 regex_destcomppats(parser);
universe@28 116 parser->compiled_patterns = calloc(pcount, sizeof(regex_t));
universe@28 117 parser->compiled_pattern_count = pcount;
universe@28 118
universe@28 119 regex_t* re;
universe@28 120 for (int i = 0 ; i < pcount ; i++) {
universe@28 121 re = malloc(sizeof(regex_t));
universe@28 122 if (regcomp(re, parser->pattern_list->items[i], REG_EXTENDED) == 0) {
universe@28 123 parser->compiled_patterns[i] = re;
universe@28 124 } else {
universe@28 125 fprintf(stderr, "Cannot compile pattern: %s\n",
universe@28 126 (parser->pattern_list->items[i]));
universe@28 127 parser->compiled_patterns[i] = NULL;
universe@28 128 success = false;
universe@28 129 }
universe@28 130 }
universe@28 131 }
universe@28 132 return success;
universe@28 133 }

mercurial