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

     1 /*
     2  * regex_parser.c
     3  *
     4  *  Created on: 26.01.2012
     5  *      Author: fox3049
     6  */
     8 #include "regex_parser.h"
    10 regex_parser_t* new_regex_parser_t() {
    11   regex_parser_t* ret = malloc(sizeof(regex_parser_t));
    12   if (ret != NULL) {
    13     ret->pattern_list = new_string_list_t();
    14     ret->matched_lines = 0;
    15     ret->pattern_match = 0;
    16     ret->compiled_patterns = NULL;
    17   }
    18   return ret;
    19 }
    21 void destroy_regex_parser_t(regex_parser_t* parser) {
    22   destroy_string_list_t(parser->pattern_list);
    23   free(parser);
    24 }
    26 bool regex_parser_matching(regex_parser_t* parser) {
    27   return parser->pattern_match > 0;
    28 }
    30 void regex_compile_all(regex_parser_t* parser) {
    31   size_t pcount = parser->pattern_list->count;
    32   if (pcount > 0) {
    33     if (parser->compiled_patterns != NULL) {
    34       free(parser->compiled_patterns);
    35     }
    36     parser->compiled_patterns = calloc(pcount, sizeof(regex_t));
    38     regex_t* re = malloc(sizeof(regex_t));
    39     for (int i = 0 ; i < pcount ; i++) {
    40       if (regcomp(re, parser->pattern_list->items[i],
    41           REG_EXTENDED|REG_NOSUB) == 0) {
    42         parser->compiled_patterns[i] = re;
    43       } else {
    44         fprintf(stderr, "Cannot compile: %s\n",
    45             (parser->pattern_list->items[i]));
    46         parser->compiled_patterns[i] = NULL;
    47       }
    48     }
    49   }
    50 }

mercurial