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

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

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


Revision 1.1 - (hide annotations) (download) (as text)
Mon May 6 04:52:11 2002 UTC (17 years, 11 months ago) by guppy
Branch: MAIN
File MIME type: text/x-chdr
added zip's matching code

1 guppy 1.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     /* matching for binds */
31     int wild_match_per(char *wild, char *matchtext) {
32     char *fallback=0;
33     int match = 0, saved = 0, count = 0, wildcard = 0;
34    
35     /* If either string is NULL, they can't match */
36     if (!matchtext || !wild)
37     return 0;
38    
39     while(*matchtext && *wild) {
40     switch (*wild) {
41     case WILDP :
42     fallback = wild++; /* setup fallback */
43     match = 1;
44     while((*(wild++)==WILDP)); /* get rid of redundant %s ... %%%% */
45     if (*wild==QUOTE) /* is the match char quoted? */
46     wild++;
47     while(*matchtext && (irctoupper(*wild) != irctoupper(*matchtext)))
48     if (*matchtext==' ') {
49     match=0; /* loop until we find the next char... or don't */
50     break;
51     } else
52     matchtext++;
53     break;
54     case WILDQ : /* inc both... let the while() check for errors */
55     wild++;
56     matchtext++;
57     break;
58     case WILDT :
59     wild++;
60     if (*matchtext!=' '){ /* need at least one space */
61     count = saved;
62     if (!fallback) /* no match and no fallback? die */
63     return 0;
64     wild = fallback-1; /* fall back */
65     break;
66     }
67     while ((*(matchtext++)==' ')); /* loop through spaces */
68     count++;
69     break;
70     case WILDS :
71     if (!*++wild) {
72     *matchtext=0;
73     return (count+saved+1);
74     }
75     wildcard = 1;
76     break;
77     default :
78     if (*wild==QUOTE) /* is it quoted? */
79     wild++;
80     if (wildcard) {
81     saved = count;
82     match = count = 0;
83     fallback = wild; /* setup fallback in case we hit a false positive */
84     while(*matchtext && (irctoupper(*wild) != irctoupper(*matchtext)))
85     matchtext++;
86     if (!*matchtext) {
87     match=1;
88     break;
89     }
90     }
91     if (irctoupper(*wild) != irctoupper(*matchtext)) {
92     if (!fallback) /* no match and no fallback? die */
93     return 0;
94     count = saved;
95     wild = fallback-1; /* fall back */
96     break;
97     }
98     count++;
99     wildcard = 0;
100     match=1;
101     if (*wild)
102     wild++;
103     if (*matchtext)
104     matchtext++;
105     }
106     }
107     return (!match || *wild || *matchtext)?0:(count+saved+1);
108     }
109    
110     /* generic hostmask matching... */
111     int wild_match(char *wild, char *matchtext) {
112     char *fallback=0;
113     int match = 0, count = 0, saved = 0, wildcard = 0;
114    
115     /* If either string is NULL, they can't match */
116     if (!matchtext || !wild)
117     return 0;
118    
119     while(*matchtext && *wild) {
120     switch (*wild) {
121     case WILDQ : /* inc both... let the while() check for errors */
122     wild++;
123     matchtext++;
124     break;
125     while ((*(matchtext++)==' ')); /* loop through spaces */
126     break;
127     case WILDS :
128     if (!*++wild) {
129     *matchtext=0;
130     return (count+saved+1);
131     }
132     wildcard = 1;
133     break;
134     default :
135     if (*wild==QUOTE) /* is it quoted? */
136     wild++;
137     if (wildcard) {
138     saved = count;
139     match = count = 0;
140     fallback = wild; /* setup fallback in case we hit a false positive */
141     while(*matchtext && (irctoupper(*wild) != irctoupper(*matchtext)))
142     matchtext++;
143     if (!*matchtext) {
144     match=1;
145     break;
146     }
147     }
148     if (irctoupper(*wild) != irctoupper(*matchtext)) {
149     if (!fallback) /* no match and no fallback? die */
150     return 0;
151     count = saved;
152     wild = fallback-1; /* fall back */
153     break;
154     }
155     wildcard = 0;
156     count++;
157     match=1;
158     if (*wild)
159     wild++;
160     if (*matchtext)
161     matchtext++;
162     }
163     }
164     return (!match || *wild || *matchtext)?0:(count+saved+1);
165     }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23