-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathwildcard.cpp
96 lines (86 loc) · 2.12 KB
/
wildcard.cpp
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
#include "includes.h"
#include "functions.h"
#include "externs.h"
#ifndef NO_WILDCARD
int wildcardfit(char *wildcard, char *test)
{
int fit = 1;
for (; ('\000' != *wildcard) && (1 == fit) && ('\000' != *test); wildcard++) {
switch (*wildcard) {
case '[':
wildcard++;
fit = set (&wildcard, &test);
break;
case '?':
test++;
break;
case '*':
fit = asterisk (&wildcard, &test);
wildcard--;
break;
default:
fit = (int) (*wildcard == *test);
test++;
}
}
while ((*wildcard == '*') && (1 == fit))
wildcard++;
return ((int) ((1 == fit) && ('\0' == *test) && ('\0' == *wildcard)));
}
int set(char **wildcard, char **test)
{
int fit = 0, negation = 0, 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)
fit = 1 - fit;
if (1 == fit)
(*test)++;
return (fit);
}
int asterisk(char **wildcard, char **test)
{
int fit = 1;
(*wildcard)++;
while (('\000' != (**test)) && (('?' == **wildcard) || ('*' == **wildcard))) {
if ('?' == **wildcard)
(*test)++;
(*wildcard)++;
}
while ('*' == (**wildcard))
(*wildcard)++;
if (('\0' == (**test)) && ('\0' != (**wildcard)))
return (fit = 0);
if (('\0' == (**test)) && ('\0' == (**wildcard)))
return (fit = 1);
else {
if (0 == wildcardfit(*wildcard, (*test))) {
do {
(*test)++;
while (((**wildcard) != (**test)) && ('[' != (**wildcard)) && ('\0' != (**test)))
(*test)++;
} while ((('\0' != **test))?(0 == wildcardfit ((char *)*wildcard, (*test))):(0 != (fit = 0)));
}
if (('\0' == **test) && ('\0' == **wildcard))
fit = 1;
return (fit);
}
}
#endif