[7d4fbcd] | 1 | #include "owl.h" |
---|
| 2 | |
---|
[e187445] | 3 | void owl_regex_init(owl_regex *re) |
---|
| 4 | { |
---|
[7d4fbcd] | 5 | re->negate=0; |
---|
| 6 | re->string=NULL; |
---|
| 7 | } |
---|
| 8 | |
---|
[e19eb97] | 9 | int owl_regex_create(owl_regex *re, const char *string) |
---|
[e187445] | 10 | { |
---|
[7d4fbcd] | 11 | int ret; |
---|
[cbc8657] | 12 | size_t errbuf_size; |
---|
| 13 | char *errbuf; |
---|
[e19eb97] | 14 | const char *ptr; |
---|
[7d4fbcd] | 15 | |
---|
[d4927a7] | 16 | re->string=g_strdup(string); |
---|
[7d4fbcd] | 17 | |
---|
| 18 | ptr=string; |
---|
| 19 | re->negate=0; |
---|
| 20 | if (string[0]=='!') { |
---|
| 21 | ptr++; |
---|
| 22 | re->negate=1; |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | /* set the regex */ |
---|
| 26 | ret=regcomp(&(re->re), ptr, REG_EXTENDED|REG_ICASE); |
---|
| 27 | if (ret) { |
---|
[cbc8657] | 28 | errbuf_size = regerror(ret, NULL, NULL, 0); |
---|
| 29 | errbuf = g_new(char, errbuf_size); |
---|
| 30 | regerror(ret, NULL, errbuf, errbuf_size); |
---|
| 31 | owl_function_error("Error in regular expression: %s", errbuf); |
---|
| 32 | g_free(errbuf); |
---|
[ddbbcffa] | 33 | g_free(re->string); |
---|
[a6560fe] | 34 | re->string=NULL; |
---|
[7d4fbcd] | 35 | return(-1); |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | return(0); |
---|
| 39 | } |
---|
| 40 | |
---|
[e19eb97] | 41 | int owl_regex_create_quoted(owl_regex *re, const char *string) |
---|
[bc08664] | 42 | { |
---|
| 43 | char *quoted; |
---|
[d427f08] | 44 | int ret; |
---|
[bc08664] | 45 | |
---|
[d427f08] | 46 | quoted = owl_text_quote(string, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH); |
---|
| 47 | ret = owl_regex_create(re, quoted); |
---|
[ddbbcffa] | 48 | g_free(quoted); |
---|
[d427f08] | 49 | return ret; |
---|
[bc08664] | 50 | } |
---|
| 51 | |
---|
[89b2daf] | 52 | int owl_regex_compare(const owl_regex *re, const char *string, int *start, int *end) |
---|
[e187445] | 53 | { |
---|
[7d4fbcd] | 54 | int out, ret; |
---|
[41c9a96] | 55 | regmatch_t match; |
---|
[7d4fbcd] | 56 | |
---|
| 57 | /* if the regex is not set we match */ |
---|
| 58 | if (!owl_regex_is_set(re)) { |
---|
| 59 | return(0); |
---|
| 60 | } |
---|
| 61 | |
---|
[41c9a96] | 62 | ret=regexec(&(re->re), string, 1, &match, 0); |
---|
[7d4fbcd] | 63 | out=ret; |
---|
| 64 | if (re->negate) { |
---|
| 65 | out=!out; |
---|
[41c9a96] | 66 | match.rm_so = 0; |
---|
| 67 | match.rm_eo = strlen(string); |
---|
[7d4fbcd] | 68 | } |
---|
[41c9a96] | 69 | if (start != NULL) *start = match.rm_so; |
---|
| 70 | if (end != NULL) *end = match.rm_eo; |
---|
[7d4fbcd] | 71 | return(out); |
---|
| 72 | } |
---|
| 73 | |
---|
[89b2daf] | 74 | int owl_regex_is_set(const owl_regex *re) |
---|
[e187445] | 75 | { |
---|
[7d4fbcd] | 76 | if (re->string) return(1); |
---|
| 77 | return(0); |
---|
| 78 | } |
---|
| 79 | |
---|
[89b2daf] | 80 | const char *owl_regex_get_string(const owl_regex *re) |
---|
[e187445] | 81 | { |
---|
[7d4fbcd] | 82 | return(re->string); |
---|
| 83 | } |
---|
| 84 | |
---|
[89b2daf] | 85 | void owl_regex_copy(const owl_regex *a, owl_regex *b) |
---|
[e187445] | 86 | { |
---|
[41c9a96] | 87 | owl_regex_create(b, a->string); |
---|
[7d4fbcd] | 88 | } |
---|
| 89 | |
---|
[5cbc929] | 90 | void owl_regex_cleanup(owl_regex *re) |
---|
[e187445] | 91 | { |
---|
[cb769bb] | 92 | if (re->string) { |
---|
[ddbbcffa] | 93 | g_free(re->string); |
---|
[cb769bb] | 94 | regfree(&(re->re)); |
---|
| 95 | } |
---|
[7d4fbcd] | 96 | } |
---|