3 * Matches wildcard patterns
5 * (c) 2001 Mark Wooding
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Anag: a simple wordgame helper.
12 * Anag is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * Anag is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with Anag; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
31 /*----- Data structures ---------------------------------------------------*/
33 typedef struct node_wild {
38 /*----- Main code ---------------------------------------------------------*/
42 * Arguments: @const char *p@ = pointer to pattern string
43 * @const char *s@ = string to compare with
45 * Returns: Nonzero if the pattern matches the string.
47 * Use: Does simple wildcard matching. This is quite nasty and more
48 * than a little slow. Supports metacharacters `*', `?' and
52 static int match(const char *p, const char *s)
55 char pch = *p++, pche, sch;
79 if (pch == '^' || pch == '!') {
84 if (*p == '-' && p[1] && p[1] != ']') {
87 if (pch <= sch && sch <= pche)
89 } else if (pch == sch)
94 if (!pch || pch == ']')
96 if (*p == '-' && p[1] && p[1] != ']') {
99 if (pch <= sch && sch <= pche)
101 } else if (pch == sch)
113 if (*p == '-' && p[1] && p[1] != ']')
134 /* --- Node matcher --- */
136 static int n_wild(node *nn, const char *p, size_t sz)
138 node_wild *n = (node_wild *)nn;
139 return (match(n->p, p));
142 /* --- Node creation --- */
144 node *wildcard(const char *const *av)
146 node_wild *n = xmalloc(sizeof(*n));
152 /*----- That's all, folks -------------------------------------------------*/