/[cvs]/eggdrop1.9/testcode/match.c
ViewVC logotype

Contents of /eggdrop1.9/testcode/match.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Thu Oct 10 04:41:59 2002 UTC (17 years, 5 months ago) by stdarg
Branch: MAIN
Changes since 1.1: +3 -0 lines
File MIME type: text/x-chdr
* Moved bind tables to libeggdrop

1 /*
2 * match.c
3 * new wildcard matching functions updated for speed.
4 * --Ian.
5 */
6 /*
7 * Copyright (C) 2001, 2002 Eggheads Development Team
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24 #define QUOTE '\\' /* quoting character... for matching "*" etc. */
25 #define WILDS '*' /* matches any number of characters */
26 #define WILDP '%' /* matches any nunber of non-space characters */
27 #define WILDQ '?' /* matches exactly one character */
28 #define WILDT '~' /* matches any number of spaces */
29
30 #include <ctype.h>
31 #define irctoupper toupper
32
33 /* matching for binds */
34 int wild_match_per(char *wild, char *matchtext) {
35 char *fallback=0;
36 int match = 0, saved = 0, count = 0, wildcard = 0;
37
38 /* If either string is NULL, they can't match */
39 if (!matchtext || !wild)
40 return 0;
41
42 while(*matchtext && *wild) {
43 switch (*wild) {
44 case WILDP :
45 fallback = wild++; /* setup fallback */
46 match = 1;
47 while((*(wild++)==WILDP)); /* get rid of redundant %s ... %%%% */
48 if (*wild==QUOTE) /* is the match char quoted? */
49 wild++;
50 while(*matchtext && (irctoupper(*wild) != irctoupper(*matchtext)))
51 if (*matchtext==' ') {
52 match=0; /* loop until we find the next char... or don't */
53 break;
54 } else
55 matchtext++;
56 break;
57 case WILDQ : /* inc both... let the while() check for errors */
58 wild++;
59 matchtext++;
60 break;
61 case WILDT :
62 wild++;
63 if (*matchtext!=' '){ /* need at least one space */
64 count = saved;
65 if (!fallback) /* no match and no fallback? die */
66 return 0;
67 wild = fallback-1; /* fall back */
68 break;
69 }
70 while ((*(matchtext++)==' ')); /* loop through spaces */
71 count++;
72 break;
73 case WILDS :
74 if (!*++wild) {
75 *matchtext=0;
76 return (count+saved+1);
77 }
78 wildcard = 1;
79 break;
80 default :
81 if (*wild==QUOTE) /* is it quoted? */
82 wild++;
83 if (wildcard) {
84 saved = count;
85 match = count = 0;
86 fallback = wild; /* setup fallback in case we hit a false positive */
87 while(*matchtext && (irctoupper(*wild) != irctoupper(*matchtext)))
88 matchtext++;
89 if (!*matchtext) {
90 match=1;
91 break;
92 }
93 }
94 if (irctoupper(*wild) != irctoupper(*matchtext)) {
95 if (!fallback) /* no match and no fallback? die */
96 return 0;
97 count = saved;
98 wild = fallback-1; /* fall back */
99 break;
100 }
101 count++;
102 wildcard = 0;
103 match=1;
104 if (*wild)
105 wild++;
106 if (*matchtext)
107 matchtext++;
108 }
109 }
110 return (!match || *wild || *matchtext)?0:(count+saved+1);
111 }
112
113 /* generic hostmask matching... */
114 int wild_match(char *wild, char *matchtext) {
115 char *fallback=0;
116 int match = 0, count = 0, saved = 0, wildcard = 0;
117
118 /* If either string is NULL, they can't match */
119 if (!matchtext || !wild)
120 return 0;
121
122 while(*matchtext && *wild) {
123 switch (*wild) {
124 case WILDQ : /* inc both... let the while() check for errors */
125 wild++;
126 matchtext++;
127 break;
128 while ((*(matchtext++)==' ')); /* loop through spaces */
129 break;
130 case WILDS :
131 if (!*++wild) {
132 *matchtext=0;
133 return (count+saved+1);
134 }
135 wildcard = 1;
136 break;
137 default :
138 if (*wild==QUOTE) /* is it quoted? */
139 wild++;
140 if (wildcard) {
141 saved = count;
142 match = count = 0;
143 fallback = wild; /* setup fallback in case we hit a false positive */
144 while(*matchtext && (irctoupper(*wild) != irctoupper(*matchtext)))
145 matchtext++;
146 if (!*matchtext) {
147 match=1;
148 break;
149 }
150 }
151 if (irctoupper(*wild) != irctoupper(*matchtext)) {
152 if (!fallback) /* no match and no fallback? die */
153 return 0;
154 count = saved;
155 wild = fallback-1; /* fall back */
156 break;
157 }
158 wildcard = 0;
159 count++;
160 match=1;
161 if (*wild)
162 wild++;
163 if (*matchtext)
164 matchtext++;
165 }
166 }
167 return (!match || *wild || *matchtext)?0:(count+saved+1);
168 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23