regex_parser.c

changeset 27
95a958e3de88
child 28
72a98cbcb9f1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/regex_parser.c	Thu Jan 26 15:55:52 2012 +0100
     1.3 @@ -0,0 +1,50 @@
     1.4 +/*
     1.5 + * regex_parser.c
     1.6 + *
     1.7 + *  Created on: 26.01.2012
     1.8 + *      Author: fox3049
     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 +  }
    1.21 +  return ret;
    1.22 +}
    1.23 +
    1.24 +void destroy_regex_parser_t(regex_parser_t* parser) {
    1.25 +  destroy_string_list_t(parser->pattern_list);
    1.26 +  free(parser);
    1.27 +}
    1.28 +
    1.29 +bool regex_parser_matching(regex_parser_t* parser) {
    1.30 +  return parser->pattern_match > 0;
    1.31 +}
    1.32 +
    1.33 +void regex_compile_all(regex_parser_t* parser) {
    1.34 +  size_t pcount = parser->pattern_list->count;
    1.35 +  if (pcount > 0) {
    1.36 +    if (parser->compiled_patterns != NULL) {
    1.37 +      free(parser->compiled_patterns);
    1.38 +    }
    1.39 +    parser->compiled_patterns = calloc(pcount, sizeof(regex_t));
    1.40 +
    1.41 +    regex_t* re = malloc(sizeof(regex_t));
    1.42 +    for (int i = 0 ; i < pcount ; i++) {
    1.43 +      if (regcomp(re, parser->pattern_list->items[i],
    1.44 +          REG_EXTENDED|REG_NOSUB) == 0) {
    1.45 +        parser->compiled_patterns[i] = re;
    1.46 +      } else {
    1.47 +        fprintf(stderr, "Cannot compile: %s\n",
    1.48 +            (parser->pattern_list->items[i]));
    1.49 +        parser->compiled_patterns[i] = NULL;
    1.50 +      }
    1.51 +    }
    1.52 +  }
    1.53 +}

mercurial