]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/wildcard.cpp
Optimized stuff
[user/henk/code/inspircd.git] / src / wildcard.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 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 #include <string>
18 #include "inspircd_config.h"
19 #include "inspircd.h"
20 #include "inspstring.h"
21
22 void Delete(char* str,int pos)
23 {
24         char moo[MAXBUF];
25         strlcpy(moo,str,MAXBUF);
26         moo[pos] = '\0';
27         strlcpy(str,moo,MAXBUF);
28         strlcat(str,moo+pos+1,MAXBUF);
29 }
30
31 void Insert(char* substr,char* str,int pos)
32 {
33         std::string a = str;
34         a.insert(pos,substr);
35         strlcpy(str,a.c_str(),MAXBUF);
36 }
37
38
39 int MWC = 0;
40
41 bool match2(char* literal,char* mask)
42 {
43
44 char OldM[MAXBUF];
45 int I,I2;
46
47 if (MWC)
48         return true;
49
50 int lenliteral = strlen(literal);
51
52 if ((strchr(mask,'*')==0) && (lenliteral != (strlen(mask))))
53         return 0;
54  I=0;
55  I2=0;
56  while (I < strlen(mask))
57  {
58    if (I2 >= lenliteral)
59            return 0;
60  
61    if ((mask[I]=='*') && (MWC==0))
62    {
63      strlcpy(OldM,mask,MAXBUF);
64      
65      Delete(mask,I);
66      
67      while (strlen(mask)<255)
68      {
69        match2(literal,mask);
70        if (MWC==2)
71                return 1;
72
73        Insert("?",mask,I);
74      }
75      strlcpy(mask,OldM,MAXBUF);
76      Delete(mask,I);
77      Insert("?",mask,I);
78    }
79    if (mask[I]=='?')
80    {
81      I++;
82      I2++;
83      continue;
84    }
85    if (mask[I] != literal[I2])
86            return 0;
87    if (MWC)
88            return 1;
89    I++;
90    I2++;
91  }
92  if (lenliteral==strlen(mask))
93                  MWC=2;
94
95 }
96
97 bool match(const char* literal, const char* mask)
98 {
99         char L[10240];
100         char M[10240];
101         MWC = 0;
102         strlcpy(L,literal,10240);
103         strlcpy(M,mask,10240);
104         strlower(L);
105         strlower(M);
106         // short circuit literals
107         log(DEBUG,"Match '%s' to '%s'",L,M);
108         if ((!strchr(M,'*')) && (!strchr(M,'?')))
109         {
110                 if (!strcasecmp(L,M))
111                 {
112                         return true;
113                 }
114         }
115         match2(L,M);
116         return (MWC == 2);
117 }
118