source: regex.c @ 460413f

release-1.10release-1.9
Last change on this file since 460413f 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
RevLine 
[7d4fbcd]1#include "owl.h"
2
[e187445]3void owl_regex_init(owl_regex *re)
4{
[7d4fbcd]5  re->negate=0;
6  re->string=NULL;
7}
8
[e19eb97]9int owl_regex_create(owl_regex *re, const char *string)
[e187445]10{
[7d4fbcd]11  int ret;
[cbc8657]12  size_t errbuf_size;
13  char *errbuf;
[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) {
[cbc8657]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);
[ddbbcffa]33    g_free(re->string);
[a6560fe]34    re->string=NULL;
[7d4fbcd]35    return(-1);
36  }
37
38  return(0);
39}
40
[e19eb97]41int owl_regex_create_quoted(owl_regex *re, const char *string)
[bc08664]42{
43  char *quoted;
[d427f08]44  int ret;
[bc08664]45 
[d427f08]46  quoted = owl_text_quote(string, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
47  ret = owl_regex_create(re, quoted);
[ddbbcffa]48  g_free(quoted);
[d427f08]49  return ret;
[bc08664]50}
51
[89b2daf]52int owl_regex_compare(const owl_regex *re, const char *string, int *start, int *end)
[e187445]53{
[7d4fbcd]54  int out, ret;
[41c9a96]55  regmatch_t match;
[7d4fbcd]56
57  /* if the regex is not set we match */
58  if (!owl_regex_is_set(re)) {
59    return(0);
60  }
61 
[41c9a96]62  ret=regexec(&(re->re), string, 1, &match, 0);
[7d4fbcd]63  out=ret;
64  if (re->negate) {
65    out=!out;
[41c9a96]66    match.rm_so = 0;
67    match.rm_eo = strlen(string);
[7d4fbcd]68  }
[41c9a96]69  if (start != NULL) *start = match.rm_so;
70  if (end != NULL) *end = match.rm_eo;
[7d4fbcd]71  return(out);
72}
73
[89b2daf]74int owl_regex_is_set(const owl_regex *re)
[e187445]75{
[7d4fbcd]76  if (re->string) return(1);
77  return(0);
78}
79
[89b2daf]80const char *owl_regex_get_string(const owl_regex *re)
[e187445]81{
[7d4fbcd]82  return(re->string);
83}
84
[89b2daf]85void owl_regex_copy(const owl_regex *a, owl_regex *b)
[e187445]86{
[41c9a96]87  owl_regex_create(b, a->string);
[7d4fbcd]88}
89
[5cbc929]90void owl_regex_cleanup(owl_regex *re)
[e187445]91{
[cb769bb]92    if (re->string) {
[ddbbcffa]93        g_free(re->string);
[cb769bb]94        regfree(&(re->re));
95    }
[7d4fbcd]96}
Note: See TracBrowser for help on using the repository browser.