Browse Source

* move f2b_regexlist_*() to separate file

master
Alex 'AdUser' Z 9 years ago
parent
commit
db12bc4f60
  1. 31
      src/regexps.c
  2. 3
      src/regexps.h
  3. 41
      src/regexps_posix.c

31
src/regexps.c

@ -0,0 +1,31 @@
/* this file should not be used directly, only with `#include "regexps.c"` */
f2b_regex_t *
f2b_regexlist_append(f2b_regex_t *list, f2b_regex_t *regex) {
assert(regex != NULL);
regex->next = list;
return regex;
}
bool
f2b_regexlist_match(f2b_regex_t *list, const char *line, char *buf, size_t buf_size) {
for (; list != NULL; list = list->next) {
if (f2b_regex_match(list, line, buf, buf_size))
return true;
}
return false;
}
f2b_regex_t *
f2b_regexlist_destroy(f2b_regex_t *list) {
f2b_regex_t *next;
for (; list != NULL; list = next) {
next = list->next;
f2b_regex_destroy(list);
}
return NULL;
}

3
src/regexps.h

@ -6,10 +6,11 @@
typedef struct _regex f2b_regex_t;
f2b_regex_t * f2b_regex_create (const char *pattern, bool icase);
bool f2b_regex_match (f2b_regex_t *regex, const char *line, char *buf, size_t hbuf_size);
void f2b_regex_destroy(f2b_regex_t *regex);
f2b_regex_t * f2b_regexlist_append (f2b_regex_t *list, f2b_regex_t *regex);
bool f2b_regexlist_match (f2b_regex_t *list, const char *line, char *matchbuf, size_t matchbuf_size);
bool f2b_regexlist_match (f2b_regex_t *list, const char *line, char *buf, size_t buf_size);
f2b_regex_t * f2b_regexlist_destroy(f2b_regex_t *list);
#endif /* F2B_REGEX_H_ */

41
src/regexps_posix.c

@ -53,41 +53,26 @@ f2b_regex_destroy(f2b_regex_t * regex) {
FREE(regex);
}
f2b_regex_t *
f2b_regexlist_append(f2b_regex_t *list, f2b_regex_t *regex) {
assert(regex != NULL);
regex->next = list;
return regex;
}
bool
f2b_regexlist_match(f2b_regex_t *list, const char *line, char *matchbuf, size_t matchbuf_size) {
f2b_regex_match(f2b_regex_t *regex, const char *line, char *buf, size_t buf_size) {
size_t match_len = 0;
regmatch_t match[2];
for (; list != NULL; list = list->next) {
if (regexec(&list->regex, line, 2, &match[0], 0) != 0)
continue;
list->matches++;
match_len = match[1].rm_eo - match[1].rm_so;
assert(matchbuf_size > match_len);
strncpy(matchbuf, &line[match[1].rm_so], match_len);
matchbuf[match_len] = '\0';
return true;
}
assert(regex != NULL);
assert(line != NULL);
assert(buf != NULL);
if (regexec(&regex->regex, line, 2, &match[0], 0) != 0)
return false;
}
f2b_regex_t *
f2b_regexlist_destroy(f2b_regex_t *list) {
f2b_regex_t *next;
regex->matches++;
match_len = match[1].rm_eo - match[1].rm_so;
assert(buf_size > match_len);
strncpy(buf, &line[match[1].rm_so], match_len);
buf[match_len] = '\0';
for (; list != NULL; list = next) {
next = list->next;
f2b_regex_destroy(list);
return true;
}
return NULL;
}
/* common f2b_regexlist_*() functions */
#include "regexps.c"

Loading…
Cancel
Save