regex_parser.c

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

mercurial