File: Wildcards.cpp

package info (click to toggle)
eiskaltdcpp 2.4.2-1.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,676 kB
  • sloc: cpp: 97,597; ansic: 5,004; perl: 1,897; xml: 1,440; sh: 1,313; php: 661; javascript: 257; makefile: 39
file content (304 lines) | stat: -rw-r--r-- 9,454 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright (C) 1996 - 2002 Florian Schintke
// Modified 2002 by Opera, opera@home.se
//
// This is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2, or (at your option) any later
// version.
//
// Thanks to the E.S.O. - ACS project that has done this C++ interface
// to the wildcards pttern matching algorithm

#include "stdinc.h"

#include "Wildcards.h"
using namespace std;
using namespace dcpp;
int Wildcard::wildcardfit(const char *wildcard, const char *test, bool useSet)
{
    int fit = 1;

    for (; ('\000' != *wildcard) && (1 == fit) && ('\000' != *test); wildcard++)
    {
        switch (*wildcard)
        {
        case '?':
            test++;
            break;
        case '*':
            fit = asterisk(&wildcard, &test);
            // the asterisk was skipped by asterisk() but the loop will
            // increment by itself. So we have to decrement
            wildcard--;
            break;
        case '[':
            if (useSet)
            {
                wildcard++; // leave out the opening square bracket
                fit = set(&wildcard, &test);
                // we don't need to decrement the wildcard as in case
                // of asterisk because the closing ] is still there
                break;
            } //if we're not using the set option, fall through
        default:
            fit = (int)(*wildcard == *test);
            test++;
        }
    }
    while ((*wildcard == '*') && (1 == fit))
        // here the teststring is empty otherwise you cannot
        // leave the previous loop
        wildcard++;
    return (int)((1 == fit) && ('\0' == *test) && ('\0' == *wildcard));
}

int Wildcard::wildcardfit(const wchar_t *wildcard, const wchar_t *test, bool useSet)
{
    int fit = 1;

    for (; (L'\000' != *wildcard) && (1 == fit) && (L'\000' != *test); wildcard++)
    {
        switch (*wildcard)
        {
        case L'?':
            test++;
            break;
        case L'*':
            fit = asterisk(&wildcard, &test);
            // the asterisk was skipped by asterisk() but the loop will
            // increment by itself. So we have to decrement
            wildcard--;
            break;
        case L'[':
            if (useSet)
            {
                wildcard++; // leave out the opening square bracket
                fit = set(&wildcard, &test);
                // we don't need to decrement the wildcard as in case
                // of asterisk because the closing ] is still there
                break;
            }//if we're not using the set option, fall through
        default:
            fit = (int)(*wildcard == *test);
            test++;
        }
    }
    while ((*wildcard == L'*') && (1 == fit))
        // here the teststring is empty otherwise you cannot
        // leave the previous loop
        wildcard++;
    return (int)((1 == fit) && (L'\0' == *test) && (L'\0' == *wildcard));
}

int Wildcard::set(const char **wildcard, const char **test)
{
    int fit = 0;
    int negation = 0;
    int at_beginning = 1;

    if ('!' == **wildcard)
    {
        negation = 1;
        (*wildcard)++;
    }
    while ((']' != **wildcard) || (1 == at_beginning))
    {
        if (0 == fit)
        {
            if (('-' == **wildcard)
                    && ((*(*wildcard - 1)) < (*(*wildcard + 1)))
                    && (']' != *(*wildcard + 1))
                    && (0 == at_beginning))
            {
                if (((**test) >= (*(*wildcard - 1)))
                        && ((**test) <= (*(*wildcard + 1))))
                {
                    fit = 1;
                    (*wildcard)++;
                }
            }
            else if ((**wildcard) == (**test))
            {
                fit = 1;
            }
        }
        (*wildcard)++;
        at_beginning = 0;
    }
    if (1 == negation)
        /* change from zero to one and vice versa */
        fit = 1 - fit;
    if (1 == fit)
        (*test)++;

    return (fit);
}

int Wildcard::set(const wchar_t **wildcard, const wchar_t **test)
{
    int fit = 0;
    int negation = 0;
    int at_beginning = 1;

    if (L'!' == **wildcard)
    {
        negation = 1;
        (*wildcard)++;
    }
    while ((L']' != **wildcard) || (1 == at_beginning))
    {
        if (0 == fit)
        {
            if ((L'-' == **wildcard)
                    && ((*(*wildcard - 1)) < (*(*wildcard + 1)))
                    && (L']' != *(*wildcard + 1))
                    && (0 == at_beginning))
            {
                if (((**test) >= (*(*wildcard - 1)))
                        && ((**test) <= (*(*wildcard + 1))))
                {
                    fit = 1;
                    (*wildcard)++;
                }
            }
            else if ((**wildcard) == (**test))
            {
                fit = 1;
            }
        }
        (*wildcard)++;
        at_beginning = 0;
    }
    if (1 == negation)
        // change from zero to one and vice versa
        fit = 1 - fit;
    if (1 == fit)
        (*test)++;

    return (fit);
}

int Wildcard::asterisk(const char **wildcard, const char **test) {
    /* Warning: uses multiple returns */
    int fit = 1;

    /* erase the leading asterisk */
    (*wildcard)++;
    while (('\000' != (**test))
           && (('?' == **wildcard)
               || ('*' == **wildcard))) {
        if ('?' == **wildcard)
            (*test)++;
        (*wildcard)++;
    }
    /* Now it could be that test is empty and wildcard contains */
    /* aterisks. Then we delete them to get a proper state */
    while ('*' == (**wildcard))
        (*wildcard)++;

    if (('\0' == (**test)) && ('\0' != (**wildcard)))
        return (fit = 0);
    if (('\0' == (**test)) && ('\0' == (**wildcard)))
        return (fit = 1);
    else {
        /* Neither test nor wildcard are empty!          */
        /* the first character of wildcard isn't in [*?] */
        if (0 == wildcardfit(*wildcard, (*test))) {
            do {
                (*test)++;
                /* skip as much characters as possible in the teststring */
                /* stop if a character match occurs */
                while (((**wildcard) != (**test))
                       && ('['  != (**wildcard))
                       && ('\0' != (**test)))
                    (*test)++;
            }
            while ((('\0' != **test)) ?
                   (0 == wildcardfit(*wildcard, (*test)))
                   : (0 != (fit = 0)));
        }
        if (('\0' == **test) && ('\0' == **wildcard))
            fit = 1;
        return (fit);
    }
}

int Wildcard::asterisk(const wchar_t **wildcard, const wchar_t **test) {
    // Warning: uses multiple returns
    int fit = 1;

    // erase the leading asterisk
    (*wildcard)++;
    while ((L'\000' != (**test))
           && ((L'?' == **wildcard)
               || (L'*' == **wildcard))) {
        if (L'?' == **wildcard)
            (*test)++;
        (*wildcard)++;
    }
    // Now it could be that test is empty and wildcard contains
    // aterisks. Then we delete them to get a proper state
    while ('*' == (**wildcard))
        (*wildcard)++;

    if ((L'\0' == (**test)) && (L'\0' != (**wildcard)))
        return (fit = 0);
    if ((L'\0' == (**test)) && (L'\0' == (**wildcard)))
        return (fit = 1);
    else {
        // Neither test nor wildcard are empty!
        // the first character of wildcard isn't in [*?]
        if (0 == wildcardfit(*wildcard, (*test))) {
            do {
                (*test)++;
                // skip as much characters as possible in the teststring
                // stop if a character match occurs
                while (((**wildcard) != (**test))
                       && (L'['  != (**wildcard))
                       && (L'\0' != (**test)))
                    (*test)++;
            }
            while (((L'\0' != **test)) ?
                   (0 == wildcardfit(*wildcard, (*test)))
                   : (0 != (fit = 0)));
        }
        if ((L'\0' == **test) && (L'\0' == **wildcard))
            fit = 1;
        return (fit);
    }
}


bool Wildcard::patternMatch(const string& text, const string& pattern, bool useSet) {
    string sText = Text::toLower(text);
    string sPattern = Text::toLower(pattern);
    return (wildcardfit(sPattern.c_str(), sText.c_str(), useSet) == 1);
}

bool Wildcard::patternMatch(const wstring& text, const wstring& pattern, bool useSet) {
    wstring sText = Text::toLower(text);
    wstring sPattern = Text::toLower(pattern);
    return (wildcardfit(sPattern.c_str(), sText.c_str(), useSet) == 1);
}

bool Wildcard::patternMatch(const string& text, const string& patternlist, char delimiter, bool useSet) {
    StringTokenizer<string> st(patternlist, delimiter);
    bool bMatched = false;
    for (auto& i : st.getTokens()) {
        bMatched = patternMatch(text, i, useSet);
        if (bMatched)
            return true;
    }
    return bMatched;
}

bool Wildcard::patternMatch(const wstring& text, const wstring& patternlist, wchar_t delimiter, bool useSet) {
    StringTokenizer<wstring> st(patternlist, delimiter);
    bool bMatched = false;
    for (WStringIter i = st.getTokens().begin(); i != st.getTokens().end(); ++i) {
        bMatched = patternMatch(text, *i, useSet);
        if (bMatched)
            return true;
    }
    return bMatched;
}