]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/wildcard.cpp
Updated copyrights in headers etc using perl inplace edit
[user/henk/code/inspircd.git] / src / wildcard.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <string>
20 #include "inspircd_config.h"
21 #include "inspircd.h"
22 #include "helperfuncs.h"
23 #include "inspstring.h"
24
25 // Wed 27 Apr 2005 - Brain
26 // I've taken our our old wildcard routine -
27 // although comprehensive, it was topheavy and very
28 // slow, and ate masses of cpu when doing lots of
29 // comparisons. This is the 'de-facto' routine used
30 // by many, nobody really knows who wrote it first
31 // or what license its under, i've seen examples of it
32 // (unattributed to any author) all over the 'net.
33 // For now, we'll just consider this public domain.
34
35 int wildcmp(char *wild, char *string)
36 {
37         char *cp, *mp;
38         while ((*string) && (*wild != '*'))
39         {
40                 if ((*wild != *string) && (*wild != '?'))
41                 {
42                         return 0;
43                 }
44                 wild++;
45                 string++;
46         }
47
48         while (*string)
49         {
50                 if (*wild == '*')
51                 {
52                         if (!*++wild)
53                         {
54                                 return 1;
55                         }
56                         mp = wild;
57                         cp = string+1;
58                 }
59                 else
60                 if ((*wild == *string) || (*wild == '?'))
61                 {
62                         wild++;
63                         string++;
64                 }
65                 else
66                 {
67                         wild = mp;
68                         string = cp++;
69                 }
70
71         }
72
73         while (*wild == '*')
74         {
75                 wild++;
76         }
77
78         return !*wild;
79 }
80
81 // This wrapper function is required to convert both
82 // strings to 'scandanavian lowercase' and make copies
83 // of them to a safe location. It also ensures we don't
84 // bite off more than we can chew with the length of
85 // the string.
86
87 bool match(const char* literal, const char* mask)
88 {
89         static char L[10240];
90         static char M[10240];
91         strlcpy(L,literal,10240);
92         strlcpy(M,mask,10240);
93         strlower(L);
94         strlower(M);
95         return wildcmp(M,L);
96 }