regex_parser.c

Thu, 26 Jan 2012 15:55:52 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 26 Jan 2012 15:55:52 +0100
changeset 27
95a958e3de88
child 28
72a98cbcb9f1
permissions
-rw-r--r--

added regexp_parser struct and compile function

/*
 * regex_parser.c
 *
 *  Created on: 26.01.2012
 *      Author: fox3049
 */

#include "regex_parser.h"

regex_parser_t* new_regex_parser_t() {
  regex_parser_t* ret = malloc(sizeof(regex_parser_t));
  if (ret != NULL) {
    ret->pattern_list = new_string_list_t();
    ret->matched_lines = 0;
    ret->pattern_match = 0;
    ret->compiled_patterns = NULL;
  }
  return ret;
}

void destroy_regex_parser_t(regex_parser_t* parser) {
  destroy_string_list_t(parser->pattern_list);
  free(parser);
}

bool regex_parser_matching(regex_parser_t* parser) {
  return parser->pattern_match > 0;
}

void regex_compile_all(regex_parser_t* parser) {
  size_t pcount = parser->pattern_list->count;
  if (pcount > 0) {
    if (parser->compiled_patterns != NULL) {
      free(parser->compiled_patterns);
    }
    parser->compiled_patterns = calloc(pcount, sizeof(regex_t));

    regex_t* re = malloc(sizeof(regex_t));
    for (int i = 0 ; i < pcount ; i++) {
      if (regcomp(re, parser->pattern_list->items[i],
          REG_EXTENDED|REG_NOSUB) == 0) {
        parser->compiled_patterns[i] = re;
      } else {
        fprintf(stderr, "Cannot compile: %s\n",
            (parser->pattern_list->items[i]));
        parser->compiled_patterns[i] = NULL;
      }
    }
  }
}

mercurial