source: regex.c @ 6829afc

release-1.10release-1.8release-1.9
Last change on this file since 6829afc was d427f08, checked in by Nelson Elhage <nelhage@mit.edu>, 13 years ago
Use G_GNUC_WARN_UNUSED_RESULT Have gcc warn us when we ignore the result of a function that requires the caller to free the result, or an initilization function that can fail. This might help (slightly) with preventing leaks and segfaults. Additionally changed some functions that should never fail to not return values. (The owl_list_* functions changed only fail if list->size < 0, which we assume is not the case elsewhere.)
  • Property mode set to 100644
File size: 1.7 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, const char *string)
11{
12  int ret;
13  char buff1[LINE];
14  const char *ptr;
15 
16  re->string=g_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    g_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, const char *string)
39{
40  char *quoted;
41  int ret;
42 
43  quoted = owl_text_quote(string, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
44  ret = owl_regex_create(re, quoted);
45  g_free(quoted);
46  return ret;
47}
48
49int owl_regex_compare(const owl_regex *re, const char *string, int *start, int *end)
50{
51  int out, ret;
52  regmatch_t match;
53
54  /* if the regex is not set we match */
55  if (!owl_regex_is_set(re)) {
56    return(0);
57  }
58 
59  ret=regexec(&(re->re), string, 1, &match, 0);
60  out=ret;
61  if (re->negate) {
62    out=!out;
63    match.rm_so = 0;
64    match.rm_eo = strlen(string);
65  }
66  if (start != NULL) *start = match.rm_so;
67  if (end != NULL) *end = match.rm_eo;
68  return(out);
69}
70
71int owl_regex_is_set(const owl_regex *re)
72{
73  if (re->string) return(1);
74  return(0);
75}
76
77const char *owl_regex_get_string(const owl_regex *re)
78{
79  return(re->string);
80}
81
82void owl_regex_copy(const owl_regex *a, owl_regex *b)
83{
84  owl_regex_create(b, a->string);
85}
86
87void owl_regex_cleanup(owl_regex *re)
88{
89    if (re->string) {
90        g_free(re->string);
91        regfree(&(re->re));
92    }
93}
Note: See TracBrowser for help on using the repository browser.