Using recursive procedure to deal with the wildcard search. It is too easy to comment.

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
#include
#include
//
// b_full means the whole string matching
//
bool WildSearch(char *psz_buf,
int n_buflen,
char *psz_sub,
int n_sublen,
bool b_full,
char sz_prechar)
{
if (n_buflen == 0 && n_sublen != 0)
{
if (n_sublen == 1 && psz_sub[0] == '*')
{
return true;
}
else
{
return false;
}
}
else if (n_buflen != 0 && n_sublen == 0)
{
if (b_full)
{
return false;
}
else
{
return true;
}
}
else if (n_buflen == 0 && n_sublen == 0)
{
return true;
}
if (psz_sub[0] == '*')
{
psz_sub++;
n_sublen--;
if (n_sublen == 0)
{
return true;
}
return WildSearch(psz_buf, n_buflen, psz_sub, n_sublen, b_full, '*');
}
else if (psz_sub[0] == '?')
{
psz_buf++;
n_buflen--;
psz_sub++;
n_sublen--;
return WildSearch(psz_buf, n_buflen, psz_sub, n_sublen, b_full, '?');
}
else
{
char *psz_star_pos = strchr(psz_sub, '*');
char *psz_question_pos = strchr(psz_sub, '?');
int n_len_to_star = (psz_star_pos == NULL) ? (int)strlen(psz_sub) : (int)(psz_star_pos - psz_sub);
int n_len_to_question = (psz_question_pos == NULL) ? (int)strlen(psz_sub) : (int)(psz_question_pos - psz_sub);
int n_compare_len = n_len_to_question > n_len_to_star ? n_len_to_star : n_len_to_question;
if (_strnicmp(psz_buf, psz_sub, n_compare_len) == 0)
//if (psz_buf[0] == psz_sub[0])
{
sz_prechar = psz_sub[0];
psz_buf++;
n_buflen--;
psz_sub++;
n_sublen--;
return WildSearch(psz_buf, n_buflen, psz_sub, n_sublen, b_full, sz_prechar);
}
else
{
if (sz_prechar != '*')
{
return false;
}
psz_buf++;
n_buflen--;
return WildSearch(psz_buf, n_buflen, psz_sub, n_sublen, b_full, sz_prechar);
}
}
return true;
}
int main(int argc, char** argv)
{
char sz_buf[] = "abcdefghijk";
char sz_sub[] = "abc?e*fg?i?*k*";
//char sz_sub[] = "ae";
bool b_result = WildSearch(sz_buf, (int)strlen(sz_buf), sz_sub, (int)strlen(sz_sub), false, ' ');
printf("%d\n", b_result);
return 0;
}

Comments