Thu, 02 Feb 2012 14:17:35 +0100
added regex parser
/* * 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; ret->compiled_pattern_count = 0; } return ret; } void regex_destcomppats(regex_parser_t* parser) { if (parser->compiled_patterns != NULL) { for (int i = 0 ; i < parser->compiled_pattern_count ; i++) { if (parser->compiled_patterns[i] != NULL) { free(parser->compiled_patterns[i]); } } free(parser->compiled_patterns); parser->compiled_patterns = NULL; parser->compiled_pattern_count = 0; } } void destroy_regex_parser_t(regex_parser_t* parser) { regex_destcomppats(parser); destroy_string_list_t(parser->pattern_list); free(parser); } bool regex_parser_matching(regex_parser_t* parser) { return parser->pattern_match > 0; } int regex_parser_do(regex_parser_t* parser, char* input) { int err = REG_NOMATCH; if (parser->compiled_pattern_count > 0) { regmatch_t match; if (regex_parser_matching(parser)) { parser->matched_lines++; err = regexec(parser->compiled_patterns[parser->pattern_match], input, 1, &match, 0); if (err > 0 && err != REG_NOMATCH) { fprintf(stderr, "Regex-Error: 0x%08x", err); } if (err == 0) { parser->pattern_match = 0; /* do not match line, if it does not end with the pattern */ if (match.rm_eo < strlen(input)) { parser->matched_lines--; } } } else { for (int i = 0 ; i < parser->compiled_pattern_count - 1 ; i += 2) { err = regexec(parser->compiled_patterns[i], input, 1, &match, 0); if (err > 0 && err != REG_NOMATCH) { fprintf(stderr, "Regex-Error: 0x%08x", err); } if (err == 0) { parser->pattern_match = i+1; parser->matched_lines = 0; /* Check, if end pattern is also in this line */ regex_parser_do(parser, input); /* do not match line, if it does not start with the pattern */ if (match.rm_so > 0 && parser->matched_lines > 0) { parser->matched_lines--; } break; } } } } return err; } bool regex_compile_all(regex_parser_t* parser) { bool success = true; size_t pcount = parser->pattern_list->count; if (pcount > 0) { regex_destcomppats(parser); parser->compiled_patterns = calloc(pcount, sizeof(regex_t)); parser->compiled_pattern_count = pcount; regex_t* re; for (int i = 0 ; i < pcount ; i++) { re = malloc(sizeof(regex_t)); if (regcomp(re, parser->pattern_list->items[i], REG_EXTENDED) == 0) { parser->compiled_patterns[i] = re; } else { fprintf(stderr, "Cannot compile pattern: %s\n", (parser->pattern_list->items[i])); parser->compiled_patterns[i] = NULL; success = false; } } } return success; }