src/regex_parser.c

changeset 99
094eff4cfc03
parent 92
51270d470cda
equal deleted inserted replaced
98:09ad5d8ab860 99:094eff4cfc03
25 */ 25 */
26 26
27 #include "regex_parser.h" 27 #include "regex_parser.h"
28 #include <ctype.h> 28 #include <ctype.h>
29 29
30 regex_parser_t* new_regex_parser_t() { 30 regex_parser* new_regex_parser() {
31 regex_parser_t* ret = malloc(sizeof(regex_parser_t)); 31 regex_parser* ret = malloc(sizeof(regex_parser));
32 if (ret != NULL) { 32 if (ret != NULL) {
33 ret->pattern_list = new_string_list_t(); 33 ret->pattern_list = new_string_list();
34 ret->matched_counted = 0; 34 ret->matched_counted = 0;
35 ret->pattern_match = 0; 35 ret->pattern_match = 0;
36 ret->compiled_patterns = NULL; 36 ret->compiled_patterns = NULL;
37 ret->compiled_pattern_count = 0; 37 ret->compiled_pattern_count = 0;
38 ret->count_chars = false; 38 ret->count_chars = false;
39 } 39 }
40 return ret; 40 return ret;
41 } 41 }
42 42
43 void regex_parser_reset(regex_parser_t* parser) { 43 void regex_parser_reset(regex_parser* parser) {
44 parser->pattern_match = parser->matched_counted = 0; 44 parser->pattern_match = parser->matched_counted = 0;
45 } 45 }
46 46
47 void regex_destcomppats(regex_parser_t* parser) { 47 void regex_destcomppats(regex_parser* parser) {
48 if (parser->compiled_patterns != NULL) { 48 if (parser->compiled_patterns != NULL) {
49 for (unsigned i = 0 ; i < parser->compiled_pattern_count ; i++) { 49 for (unsigned i = 0 ; i < parser->compiled_pattern_count ; i++) {
50 regfree(parser->compiled_patterns + i); 50 regfree(parser->compiled_patterns + i);
51 } 51 }
52 free(parser->compiled_patterns); 52 free(parser->compiled_patterns);
53 parser->compiled_patterns = NULL; 53 parser->compiled_patterns = NULL;
54 parser->compiled_pattern_count = 0; 54 parser->compiled_pattern_count = 0;
55 } 55 }
56 } 56 }
57 57
58 void destroy_regex_parser_t(regex_parser_t* parser) { 58 void destroy_regex_parser(regex_parser* parser) {
59 regex_destcomppats(parser); 59 regex_destcomppats(parser);
60 destroy_string_list_t(parser->pattern_list); 60 destroy_string_list(parser->pattern_list);
61 free(parser); 61 free(parser);
62 } 62 }
63 63
64 bool regex_parser_matching(regex_parser_t* parser) { 64 bool regex_parser_matching(regex_parser* parser) {
65 return parser->pattern_match > 0; 65 return parser->pattern_match > 0;
66 } 66 }
67 67
68 static unsigned regex_parser_count_chars(const char* input, 68 static unsigned regex_parser_count_chars(const char* input,
69 unsigned start, unsigned end) { 69 unsigned start, unsigned end) {
72 ret += isspace(input[i]) ? 0 : 1; 72 ret += isspace(input[i]) ? 0 : 1;
73 } 73 }
74 return ret; 74 return ret;
75 } 75 }
76 76
77 int regex_parser_do(regex_parser_t* parser, char* input) { 77 int regex_parser_do(regex_parser* parser, char* input) {
78 int err = REG_NOMATCH; 78 int err = REG_NOMATCH;
79 if (parser->compiled_pattern_count > 0) { 79 if (parser->compiled_pattern_count > 0) {
80 regmatch_t match; 80 regmatch_t match;
81 81
82 if (regex_parser_matching(parser)) { 82 if (regex_parser_matching(parser)) {
137 } 137 }
138 } 138 }
139 return err; 139 return err;
140 } 140 }
141 141
142 bool regex_compile_all(regex_parser_t* parser) { 142 bool regex_compile_all(regex_parser* parser) {
143 size_t pcount = parser->pattern_list->count; 143 size_t pcount = parser->pattern_list->count;
144 if (pcount > 0) { 144 if (pcount > 0) {
145 regex_destcomppats(parser); 145 regex_destcomppats(parser);
146 parser->compiled_patterns = calloc(pcount, sizeof(regex_t)); 146 parser->compiled_patterns = calloc(pcount, sizeof(regex_t));
147 147

mercurial