source: regex.c @ 004d683

release-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 004d683 was d43edd2, checked in by Anders Kaseorg <andersk@mit.edu>, 15 years ago
Death to RCS keywords. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Property mode set to 100644
File size: 1.5 KB
Line 
1#include <string.h>
2#include "owl.h"
3
4void owl_regex_init(owl_regex *re)
5{
6  re->negate=0;
7  re->string=NULL;
8}
9
10int owl_regex_create(owl_regex *re, char *string)
11{
12  int ret;
13  char buff1[LINE];
14  char *ptr;
15 
16  re->string=owl_strdup(string);
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);
29    owl_function_makemsg("Error in regular expression: %s", buff1);
30    owl_free(re->string);
31    re->string=NULL;
32    return(-1);
33  }
34
35  return(0);
36}
37
38int owl_regex_create_quoted(owl_regex *re, char *string)
39{
40  char *quoted;
41 
42  quoted=owl_text_quote(string, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
43  owl_regex_create(re, quoted);
44  owl_free(quoted);
45  return(0);
46}
47
48int owl_regex_compare(owl_regex *re, char *string)
49{
50  int out, ret;
51
52  /* if the regex is not set we match */
53  if (!owl_regex_is_set(re)) {
54    return(0);
55  }
56 
57  ret=regexec(&(re->re), string, 0, NULL, 0);
58  out=ret;
59  if (re->negate) {
60    out=!out;
61  }
62  return(out);
63}
64
65int owl_regex_is_set(owl_regex *re)
66{
67  if (re->string) return(1);
68  return(0);
69}
70
71char *owl_regex_get_string(owl_regex *re)
72{
73  return(re->string);
74}
75
76void owl_regex_copy(owl_regex *a, owl_regex *b)
77{
78  b->negate=a->negate;
79  b->string=owl_strdup(a->string);
80  memcpy(&(b->re), &(a->re), sizeof(regex_t));
81}
82
83void owl_regex_free(owl_regex *re)
84{
85    if (re->string) {
86        owl_free(re->string);
87        regfree(&(re->re));
88    }
89}
Note: See TracBrowser for help on using the repository browser.