NO

Author Topic: compare with wild args  (Read 118 times)

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1612
compare with wild args
« on: August 13, 2017, 03:10:45 PM »
At masm32 forum:
http://masm32.com/board/index.php?topic=6463.msg69298#msg69298

Similar for PellesC:
Function name renamed to avoid conflict with original name.
Code: [Select]
int CmpWildArgs(char *s1, char *s2)
{
char *p1 = s1;
char *p2 = s2;
while (1)
{ // endless loop
if (*p2 == '*')
{
p2++;
if (*p2 != '.') // mask continues
continue;
while (*p1 && *p2 && *p1++ == *p2++)
if (!*p1 || !*p2)
break; // end of string 1 or 2
}
else
{
if (*p1 != *p2 && *p2 != '?') // differ but not a mask
break; // difference found
p1++; // advance string 1
p2++; // advance string 2
}
};
if (*p1) // string 1 searched
return 1;
else
return 0;
}
test for it
Code: [Select]
int __cdecl main(void)
{
printf("1:%d\n", CmpWildArgs("file", "*") == 1);
printf("1:%d\n", CmpWildArgs("file", "*.*") == 1);
printf("1:%d\n", CmpWildArgs("file", "f*.*") == 1);
printf("1:%d\n", CmpWildArgs("file", "file*") == 1);
printf("1:%d\n", CmpWildArgs("file.c", "file.?") == 1);
printf("0:%d\n", CmpWildArgs("file.c", "file.??") == 0);
printf("0:%d\n", CmpWildArgs("file.c", "???.?") == 0);
printf("1:%d\n", CmpWildArgs("file.c", "????.?") == 1);
printf("1:%d\n", CmpWildArgs("file.c", "file.c*") == 1);
printf("0:%d\n", CmpWildArgs("file.c", "file.c?") == 0);
printf("1:%d\n", CmpWildArgs("file.x.c", "*.c") == 1);
printf("1:%d\n", CmpWildArgs("file.x.c", "????.*.c") == 1);
printf("0:%d\n", CmpWildArgs("file.x.c", "????.*.b") == 0);
printf("0:%d\n", CmpWildArgs("file.x.c", "*.?.b") == 0);
printf("0:%d\n", CmpWildArgs("file.x.c", "*.*.b") == 0);
printf("0:%d\n", CmpWildArgs("file.x.c", "*.*.c") == 0);
printf("0:%d\n", CmpWildArgs("file.x.c", "*.?.c") == 0);
printf("1:%d\n", CmpWildArgs("file.x.c", "*?x.c") == 1);
printf("1:%d\n", CmpWildArgs("file.ext", "*") == 1);
printf("1:%d\n", CmpWildArgs("file.prj", "*.*") == 1);
printf("0:%d\n", CmpWildArgs("file.ext", "x*.*") == 0);
printf("1:%d\n", CmpWildArgs("ab39.ext", "?B39.??T") == 1);
printf("0:%d\n", CmpWildArgs("abcd.ext", "?b?.?x?") == 0);
printf("1:%d\n", CmpWildArgs("abcd.ext", "?b*.?x?") == 1);
printf("0:%d\n", CmpWildArgs("abcd.ext", "?b*.?z?") == 0);
return 0;
}
« Last Edit: August 13, 2017, 03:44:48 PM by TimoVJL »
May the source be with you