source: regex.c @ 7803326

release-1.10release-1.9
Last change on this file since 7803326 was f271129, checked in by Jason Gross <jgross@mit.edu>, 13 years ago
Fix up headers The additions to owl.h and some of the removals were done by Alejandro Sedeño <asedeno@mit.edu> in commit 77a0258b3919468fc9d7f7602588ac427ab36e6c. Notes: * I think owl.c lost the need for sys/time.h when we punted select() in favor of glib's main loop. * We don't actually need to include things like stdarg.h, stdio.h, glib/gstdio.h, glib-object.h. I think they get indirectly included via owl.h and/or glib.h. They're left in (or added in to) the files that use functions/types from them. * I'm not entirely sure what sys/socket.h is doing in message.c. It is there from the initial commit. I suspect it might have had something to do with the call to getnameinfo. message.c compiles without it, but http://pubs.opengroup.org/onlinepubs/009695399/functions/getnameinfo.html suggests that we're supposed to include it? *shrugs* I'm leaving it in, for now. (Rather, I'll leave one copy of the #include in.)
  • Property mode set to 100644
File size: 1.8 KB
Line 
1#include "owl.h"
2
3void owl_regex_init(owl_regex *re)
4{
5  re->negate=0;
6  re->string=NULL;
7}
8
9int owl_regex_create(owl_regex *re, const char *string)
10{
11  int ret;
12  size_t errbuf_size;
13  char *errbuf;
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    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);
33    g_free(re->string);
34    re->string=NULL;
35    return(-1);
36  }
37
38  return(0);
39}
40
41int owl_regex_create_quoted(owl_regex *re, const char *string)
42{
43  char *quoted;
44  int ret;
45 
46  quoted = owl_text_quote(string, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
47  ret = owl_regex_create(re, quoted);
48  g_free(quoted);
49  return ret;
50}
51
52int owl_regex_compare(const owl_regex *re, const char *string, int *start, int *end)
53{
54  int out, ret;
55  regmatch_t match;
56
57  /* if the regex is not set we match */
58  if (!owl_regex_is_set(re)) {
59    return(0);
60  }
61 
62  ret=regexec(&(re->re), string, 1, &match, 0);
63  out=ret;
64  if (re->negate) {
65    out=!out;
66    match.rm_so = 0;
67    match.rm_eo = strlen(string);
68  }
69  if (start != NULL) *start = match.rm_so;
70  if (end != NULL) *end = match.rm_eo;
71  return(out);
72}
73
74int owl_regex_is_set(const owl_regex *re)
75{
76  if (re->string) return(1);
77  return(0);
78}
79
80const char *owl_regex_get_string(const owl_regex *re)
81{
82  return(re->string);
83}
84
85void owl_regex_copy(const owl_regex *a, owl_regex *b)
86{
87  owl_regex_create(b, a->string);
88}
89
90void owl_regex_cleanup(owl_regex *re)
91{
92    if (re->string) {
93        g_free(re->string);
94        regfree(&(re->re));
95    }
96}
Note: See TracBrowser for help on using the repository browser.