Desktop softwares and information
Posts tagged wildcard
Wildcard search with source code (C language)
Feb 4th
Using recursive procedure to deal with the wildcard search. It is too easy to comment.
#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;
}
Recent Comments