Here is a quick function the will allow wildcard comparison.
#include < stdio.h >
int striwildcmp( const char *test, const char *value )
{
int result = 0;
if( test == value ) {
return 0;
}
if( !test ) {
return -1;
}
if( !value ) {
return 1;
}
while( result == 0 ) {
// Skip '?' wildcard
if( *test != '?' ) {
result = tolower( *test ) - tolower( *value );
}
// Break once you compare a null terminator
if( (*test == '\0') || *value == '\0' ) {
break;
}
++test;
++value;
}
return result;
} |
#include < stdio.h >
int striwildcmp( const char *test, const char *value )
{
int result = 0;
if( test == value ) {
return 0;
}
if( !test ) {
return -1;
}
if( !value ) {
return 1;
}
while( result == 0 ) {
// Skip '?' wildcard
if( *test != '?' ) {
result = tolower( *test ) - tolower( *value );
}
// Break once you compare a null terminator
if( (*test == '\0') || *value == '\0' ) {
break;
}
++test;
++value;
}
return result;
}
#define TEST(T,V) \
printf("%s ?= %s === %d\n", #T, #V, striwildcmp(#T, #V))
//Main
int main( void )
{
TEST (TE?T, test);
TEST (TE?T, text);
TEST (TEST, test);
TEST (AppleA, Apple);
TEST (Apple, AppleA);
TEST (Incorrect, Wrong);
TEST (Do?, Cat);
return 0;
} |
#define TEST(T,V) \
printf("%s ?= %s === %d\n", #T, #V, striwildcmp(#T, #V))
//Main
int main( void )
{
TEST (TE?T, test);
TEST (TE?T, text);
TEST (TEST, test);
TEST (AppleA, Apple);
TEST (Apple, AppleA);
TEST (Incorrect, Wrong);
TEST (Do?, Cat);
return 0;
}
Gives you results like other strcmp functions
$ gcc test.c -o test.exe && ./test.exe
TE?T ?= test === 0
TE?T ?= text === 0
TEST ?= test === 0
AppleA ?= Apple === 97
Apple ?= AppleA === -97
Incorrect ?= Wrong === -14
Do? ?= Cat === 1 |
$ gcc test.c -o test.exe && ./test.exe
TE?T ?= test === 0
TE?T ?= text === 0
TEST ?= test === 0
AppleA ?= Apple === 97
Apple ?= AppleA === -97
Incorrect ?= Wrong === -14
Do? ?= Cat === 1
This entry was posted
on Friday, March 6th, 2015 at 10:40 am and is filed under Uncategorized.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.