source: regex.c @ 19b2766

release-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 19b2766 was 89b2daf, checked in by Anders Kaseorg <andersk@mit.edu>, 15 years ago
Add const qualifiers for owl_regex *. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Property mode set to 100644
File size: 1.7 KB
RevLine 
[7d4fbcd]1#include <string.h>
2#include "owl.h"
3
[e187445]4void owl_regex_init(owl_regex *re)
5{
[7d4fbcd]6  re->negate=0;
7  re->string=NULL;
8}
9
[e19eb97]10int 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 
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);
[554a2b8]29    owl_function_makemsg("Error in regular expression: %s", buff1);
[7d4fbcd]30    owl_free(re->string);
[a6560fe]31    re->string=NULL;
[7d4fbcd]32    return(-1);
33  }
34
35  return(0);
36}
37
[e19eb97]38int owl_regex_create_quoted(owl_regex *re, const char *string)
[bc08664]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
[89b2daf]48int owl_regex_compare(const owl_regex *re, const char *string, int *start, int *end)
[e187445]49{
[7d4fbcd]50  int out, ret;
[41c9a96]51  regmatch_t match;
[7d4fbcd]52
53  /* if the regex is not set we match */
54  if (!owl_regex_is_set(re)) {
55    return(0);
56  }
57 
[41c9a96]58  ret=regexec(&(re->re), string, 1, &match, 0);
[7d4fbcd]59  out=ret;
60  if (re->negate) {
61    out=!out;
[41c9a96]62    match.rm_so = 0;
63    match.rm_eo = strlen(string);
[7d4fbcd]64  }
[41c9a96]65  if (start != NULL) *start = match.rm_so;
66  if (end != NULL) *end = match.rm_eo;
[7d4fbcd]67  return(out);
68}
69
[89b2daf]70int owl_regex_is_set(const owl_regex *re)
[e187445]71{
[7d4fbcd]72  if (re->string) return(1);
73  return(0);
74}
75
[89b2daf]76const char *owl_regex_get_string(const owl_regex *re)
[e187445]77{
[7d4fbcd]78  return(re->string);
79}
80
[89b2daf]81void owl_regex_copy(const owl_regex *a, owl_regex *b)
[e187445]82{
[41c9a96]83  owl_regex_create(b, a->string);
[7d4fbcd]84}
85
[e187445]86void owl_regex_free(owl_regex *re)
87{
[cb769bb]88    if (re->string) {
89        owl_free(re->string);
90        regfree(&(re->re));
91    }
[7d4fbcd]92}
Note: See TracBrowser for help on using the repository browser.