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

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

mercurial