X-Git-Url: https://git.distorted.org.uk/~mdw/anag/blobdiff_plain/f7ebae384ae1bb948fc062dde36916d2776d3eae..a10122de085ddf5c8bec6fc12c7164ad1dc1459c:/regexp.c diff --git a/regexp.c b/regexp.c new file mode 100644 index 0000000..24c40e3 --- /dev/null +++ b/regexp.c @@ -0,0 +1,104 @@ +/* -*-c-*- + * + * $Id: regexp.c,v 1.1 2002/08/11 12:58:09 mdw Exp $ + * + * Matches regular expressions + * + * (c) 2002 Mark Wooding + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of Anag: a simple wordgame helper. + * + * Anag is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Anag is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Anag; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +/*----- Revision history --------------------------------------------------* + * + * $Log: regexp.c,v $ + * Revision 1.1 2002/08/11 12:58:09 mdw + * Added support for regular expression matching, if supported by the C + * library. + * + * Revision 1.1 2001/02/04 17:14:42 mdw + * Initial checkin + * + */ + +/*----- Header files ------------------------------------------------------*/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifndef HAVE_REGCOMP + extern int dummy; +#else + +#include "anag.h" +#include + +/*----- Data structures ---------------------------------------------------*/ + +typedef struct node_regexp { + node n; + const char *s; + regex_t rx; +} node_regexp; + +/*----- Main code ---------------------------------------------------------*/ + +/* --- Node matcher --- */ + +static int n_regexp(node *nn, const char *p, size_t sz) +{ + node_regexp *n = (node_regexp *)nn; + int e; + + switch (e = regexec(&n->rx, p, 0, 0, 0)) { + case 0: + return 1; + case REG_NOMATCH: + return 0; + default: { + char buf[256]; + regerror(e, &n->rx, buf, sizeof(buf)); + die("error matching regexp `%s' against `%s': %s", + n->s, p, buf); + } break; + } + return (0); +} + +/* --- Node creation --- */ + +node *regexp(const char *const *av) +{ + node_regexp *n = xmalloc(sizeof(*n)); + int e; + n->n.func = n_regexp; + if ((e = regcomp(&n->rx, av[0], + REG_EXTENDED | REG_ICASE | REG_NOSUB)) != 0) { + char buf[256]; + regerror(e, &n->rx, buf, sizeof(buf)); + die("bad regular expression `%s': %s", av[0], buf); + } + return (&n->n); +} + +/*----- That's all, folks -------------------------------------------------*/ + +#endif