]> git.notmuchmail.org Git - notmuch/commitdiff
Add wrappers for regcomp and regexec to xutil.c.
authorCarl Worth <cworth@cworth.org>
Wed, 21 Oct 2009 22:06:52 +0000 (15:06 -0700)
committerCarl Worth <cworth@cworth.org>
Wed, 21 Oct 2009 22:07:20 +0000 (15:07 -0700)
These will be handy for some parsing.

xutil.c
xutil.h

diff --git a/xutil.c b/xutil.c
index 7ee7a698f3c7142e1cab717eeaa930ced6fd18ad..eadd3783f9477735d43cabd84f96c9a53e900184 100644 (file)
--- a/xutil.c
+++ b/xutil.c
@@ -92,3 +92,42 @@ xstrndup (const char *s, size_t n)
 
     return ret;
 }
+
+void
+xregcomp (regex_t *preg, const char *regex, int cflags)
+{
+    int rerr;
+
+    rerr = regcomp (preg, regex, cflags);
+    if (rerr) {
+       size_t error_size = regerror (rerr, preg, NULL, 0);
+       char *error = xmalloc (error_size);
+
+       regerror (rerr, preg, error, error_size);
+       fprintf (stderr, "Internal error compiling regex %s: %s\n",
+                regex, error);
+       free (error);
+       exit (1);
+    }
+}
+
+int
+xregexec (const regex_t *preg, const char *string,
+         size_t nmatch, regmatch_t pmatch[], int eflags)
+{
+    int i, rerr;
+
+    rerr = regexec (preg, string, nmatch, pmatch, eflags);
+    if (rerr)
+       return rerr;
+
+    for (i = 0; i < nmatch; i++) {
+       if (pmatch[i].rm_so == -1) {
+           fprintf (stderr, "Internal error matching regex against %s: Sub-match %d not found\n",
+                    string, i);
+           exit (1);
+       }
+    }
+
+    return 0;
+}
diff --git a/xutil.h b/xutil.h
index 7a089a5de55cd6f4d77f1e9eef81ec285834f069..b973f7dcbf752f4cca74d69ef86bcf027cde3aaa 100644 (file)
--- a/xutil.h
+++ b/xutil.h
@@ -41,4 +41,11 @@ xstrdup (const char *s);
 char *
 xstrndup (const char *s, size_t n);
 
+void
+xregcomp (regex_t *preg, const char *regex, int cflags);
+
+int
+xregexec (const regex_t *preg, const char *string,
+         size_t nmatch, regmatch_t pmatch[], int eflags);
+
 #endif