3 * $Id: wildcard.c,v 1.2 2004/04/08 01:36:19 mdw Exp $
5 * Matches wildcard patterns
7 * (c) 2001 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Anag: a simple wordgame helper.
14 * Anag is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * Anag is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Anag; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Header files ------------------------------------------------------*/
33 /*----- Data structures ---------------------------------------------------*/
35 typedef struct node_wild {
40 /*----- Main code ---------------------------------------------------------*/
44 * Arguments: @const char *p@ = pointer to pattern string
45 * @const char *s@ = string to compare with
47 * Returns: Nonzero if the pattern matches the string.
49 * Use: Does simple wildcard matching. This is quite nasty and more
50 * than a little slow. Supports metacharacters `*', `?' and
54 static int match(const char *p, const char *s)
57 char pch = *p++, pche, sch;
81 if (pch == '^' || pch == '!') {
86 if (*p == '-' && p[1] && p[1] != ']') {
89 if (pch <= sch && sch <= pche)
91 } else if (pch == sch)
96 if (!pch || pch == ']')
98 if (*p == '-' && p[1] && p[1] != ']') {
101 if (pch <= sch && sch <= pche)
103 } else if (pch == sch)
115 if (*p == '-' && p[1] && p[1] != ']')
136 /* --- Node matcher --- */
138 static int n_wild(node *nn, const char *p, size_t sz)
140 node_wild *n = (node_wild *)nn;
141 return (match(n->p, p));
144 /* --- Node creation --- */
146 node *wildcard(const char *const *av)
148 node_wild *n = xmalloc(sizeof(*n));
154 /*----- That's all, folks -------------------------------------------------*/