regex_parser.c

changeset 34
fa9bda32de17
parent 33
1a2d7298bc82
child 35
35120de6ee53
     1.1 --- a/regex_parser.c	Tue Oct 02 10:49:25 2012 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,110 +0,0 @@
     1.4 -/*
     1.5 - * regex_parser.c
     1.6 - *
     1.7 - *  Created on: 26.01.2012
     1.8 - *      Author: Mike
     1.9 - */
    1.10 -
    1.11 -#include "regex_parser.h"
    1.12 -
    1.13 -regex_parser_t* new_regex_parser_t() {
    1.14 -  regex_parser_t* ret = malloc(sizeof(regex_parser_t));
    1.15 -  if (ret != NULL) {
    1.16 -    ret->pattern_list = new_string_list_t();
    1.17 -    ret->matched_lines = 0;
    1.18 -    ret->pattern_match = 0;
    1.19 -    ret->compiled_patterns = NULL;
    1.20 -    ret->compiled_pattern_count = 0;
    1.21 -  }
    1.22 -  return ret;
    1.23 -}
    1.24 -
    1.25 -void regex_destcomppats(regex_parser_t* parser) {
    1.26 -  if (parser->compiled_patterns != NULL) {
    1.27 -    for (int i = 0 ; i < parser->compiled_pattern_count ; i++) {
    1.28 -      if (parser->compiled_patterns[i] != NULL) {
    1.29 -        free(parser->compiled_patterns[i]);
    1.30 -      }
    1.31 -    }
    1.32 -    free(parser->compiled_patterns);
    1.33 -    parser->compiled_patterns = NULL;
    1.34 -    parser->compiled_pattern_count = 0;
    1.35 -  }
    1.36 -}
    1.37 -
    1.38 -void destroy_regex_parser_t(regex_parser_t* parser) {
    1.39 -  regex_destcomppats(parser);
    1.40 -  destroy_string_list_t(parser->pattern_list);
    1.41 -  free(parser);
    1.42 -}
    1.43 -
    1.44 -bool regex_parser_matching(regex_parser_t* parser) {
    1.45 -  return parser->pattern_match > 0;
    1.46 -}
    1.47 -
    1.48 -int regex_parser_do(regex_parser_t* parser, char* input) {
    1.49 -  int err = REG_NOMATCH;
    1.50 -  if (parser->compiled_pattern_count > 0) {
    1.51 -    regmatch_t match;
    1.52 -
    1.53 -    if (regex_parser_matching(parser)) {
    1.54 -      parser->matched_lines++;
    1.55 -
    1.56 -      err = regexec(parser->compiled_patterns[parser->pattern_match],
    1.57 -          input, 1, &match, 0);
    1.58 -      if (err > 0 && err != REG_NOMATCH) {
    1.59 -        fprintf(stderr, "Regex-Error: 0x%08x", err);
    1.60 -      }
    1.61 -      if (err == 0) {
    1.62 -        parser->pattern_match = 0;
    1.63 -        /* do not match line, if it does not end with the pattern */
    1.64 -        if (match.rm_eo < strlen(input)) {
    1.65 -          parser->matched_lines--;
    1.66 -        }
    1.67 -      }
    1.68 -    } else {
    1.69 -      for (int i = 0 ; i < parser->compiled_pattern_count - 1 ; i += 2) {
    1.70 -        err = regexec(parser->compiled_patterns[i], input, 1, &match, 0);
    1.71 -        if (err > 0 && err != REG_NOMATCH) {
    1.72 -          fprintf(stderr, "Regex-Error: 0x%08x", err);
    1.73 -        }
    1.74 -        if (err == 0) {
    1.75 -          parser->pattern_match = i+1;
    1.76 -          parser->matched_lines = 0;
    1.77 -          /* Check, if end pattern is also in this line */
    1.78 -          regex_parser_do(parser, input);
    1.79 -          /* do not match line, if it does not start with the pattern */
    1.80 -          if (match.rm_so > 0 && parser->matched_lines > 0) {
    1.81 -            parser->matched_lines--;
    1.82 -          }
    1.83 -          break;
    1.84 -        }
    1.85 -      }
    1.86 -    }
    1.87 -  }
    1.88 -  return err;
    1.89 -}
    1.90 -
    1.91 -bool regex_compile_all(regex_parser_t* parser) {
    1.92 -  bool success = true;
    1.93 -  size_t pcount = parser->pattern_list->count;
    1.94 -  if (pcount > 0) {
    1.95 -    regex_destcomppats(parser);
    1.96 -    parser->compiled_patterns = calloc(pcount, sizeof(regex_t));
    1.97 -    parser->compiled_pattern_count = pcount;
    1.98 -
    1.99 -    regex_t* re;
   1.100 -    for (int i = 0 ; i < pcount ; i++) {
   1.101 -      re = malloc(sizeof(regex_t));
   1.102 -      if (regcomp(re, parser->pattern_list->items[i], REG_EXTENDED) == 0) {
   1.103 -        parser->compiled_patterns[i] = re;
   1.104 -      } else {
   1.105 -        fprintf(stderr, "Cannot compile pattern: %s\n",
   1.106 -            (parser->pattern_list->items[i]));
   1.107 -        parser->compiled_patterns[i] = NULL;
   1.108 -        success = false;
   1.109 -      }
   1.110 -    }
   1.111 -  }
   1.112 -  return success;
   1.113 -}

mercurial