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