regex_parser.c

changeset 28
72a98cbcb9f1
parent 27
95a958e3de88
child 29
fa625066ae52
equal deleted inserted replaced
27:95a958e3de88 28:72a98cbcb9f1
12 if (ret != NULL) { 12 if (ret != NULL) {
13 ret->pattern_list = new_string_list_t(); 13 ret->pattern_list = new_string_list_t();
14 ret->matched_lines = 0; 14 ret->matched_lines = 0;
15 ret->pattern_match = 0; 15 ret->pattern_match = 0;
16 ret->compiled_patterns = NULL; 16 ret->compiled_patterns = NULL;
17 ret->compiled_pattern_count = 0;
17 } 18 }
18 return ret; 19 return ret;
19 } 20 }
20 21
22 void regex_destcomppats(regex_parser_t* parser) {
23 if (parser->compiled_patterns != NULL) {
24 for (int i = 0 ; i < parser->compiled_pattern_count ; i++) {
25 if (parser->compiled_patterns[i] != NULL) {
26 free(parser->compiled_patterns[i]);
27 }
28 }
29 free(parser->compiled_patterns);
30 parser->compiled_patterns = NULL;
31 parser->compiled_pattern_count = 0;
32 }
33 }
34
21 void destroy_regex_parser_t(regex_parser_t* parser) { 35 void destroy_regex_parser_t(regex_parser_t* parser) {
36 regex_destcomppats(parser);
22 destroy_string_list_t(parser->pattern_list); 37 destroy_string_list_t(parser->pattern_list);
23 free(parser); 38 free(parser);
24 } 39 }
25 40
26 bool regex_parser_matching(regex_parser_t* parser) { 41 bool regex_parser_matching(regex_parser_t* parser) {
27 return parser->pattern_match > 0; 42 return parser->pattern_match > 0;
28 } 43 }
29 44
30 void regex_compile_all(regex_parser_t* parser) { 45 int regex_parser_do(regex_parser_t* parser, char* input) {
31 size_t pcount = parser->pattern_list->count; 46 int err = REG_NOMATCH;
32 if (pcount > 0) { 47 if (parser->compiled_pattern_count > 0) {
33 if (parser->compiled_patterns != NULL) { 48 regmatch_t match;
34 free(parser->compiled_patterns);
35 }
36 parser->compiled_patterns = calloc(pcount, sizeof(regex_t));
37 49
38 regex_t* re = malloc(sizeof(regex_t)); 50 if (regex_parser_matching(parser)) {
39 for (int i = 0 ; i < pcount ; i++) { 51 parser->matched_lines++;
40 if (regcomp(re, parser->pattern_list->items[i], 52
41 REG_EXTENDED|REG_NOSUB) == 0) { 53 err = regexec(parser->compiled_patterns[parser->pattern_match],
42 parser->compiled_patterns[i] = re; 54 input, 1, &match, 0);
43 } else { 55 if (err > 0 && err != REG_NOMATCH) {
44 fprintf(stderr, "Cannot compile: %s\n", 56 fprintf(stderr, "Regex-Error: 0x%08x", err);
45 (parser->pattern_list->items[i])); 57 }
46 parser->compiled_patterns[i] = NULL; 58 if (err == 0) {
59 parser->pattern_match = 0;
60 /* do not match line, if it does not end with the pattern */
61 if (match.rm_eo < strlen(input)) {
62 parser->matched_lines--;
63 }
64 }
65 } else {
66 for (int i = 0 ; i < parser->compiled_pattern_count - 1 ; i += 2) {
67 err = regexec(parser->compiled_patterns[i], input, 1, &match, 0);
68 if (err > 0 && err != REG_NOMATCH) {
69 fprintf(stderr, "Regex-Error: 0x%08x", err);
70 }
71 if (err == 0) {
72 parser->pattern_match = i+1;
73 parser->matched_lines = 0;
74 /* Check, if end pattern is also in this line */
75 regex_parser_do(parser, input);
76 /* do not match line, if it does not start with the pattern */
77 if (match.rm_so > 0 && parser->matched_lines > 0) {
78 parser->matched_lines--;
79 }
80 break;
81 }
47 } 82 }
48 } 83 }
49 } 84 }
85 return err;
50 } 86 }
87
88 bool regex_compile_all(regex_parser_t* parser) {
89 bool success = true;
90 size_t pcount = parser->pattern_list->count;
91 if (pcount > 0) {
92 regex_destcomppats(parser);
93 parser->compiled_patterns = calloc(pcount, sizeof(regex_t));
94 parser->compiled_pattern_count = pcount;
95
96 regex_t* re;
97 for (int i = 0 ; i < pcount ; i++) {
98 re = malloc(sizeof(regex_t));
99 if (regcomp(re, parser->pattern_list->items[i], REG_EXTENDED) == 0) {
100 parser->compiled_patterns[i] = re;
101 } else {
102 fprintf(stderr, "Cannot compile pattern: %s\n",
103 (parser->pattern_list->items[i]));
104 parser->compiled_patterns[i] = NULL;
105 success = false;
106 }
107 }
108 }
109 return success;
110 }

mercurial