TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: All
from: Frank Adam
date: 1996-05-14 14:23:00
subject: strwildcmp :)

This is the equivalent of the Like operator in VB4.Well close enough. 
I don't think i've seen this sort of function yet, not in SNIPs anyway.
As always, i reserve the right to get flamed for posting such rubbish:)

It can take a string, or pattern as str2.
Pattern can be: char,'#' (digit),'?'(anychar),'*'(don't care, here on).
and in brackets[] or []

Hmmm, this could even replace strcmp()..
I think i've left the iron on, better go ;-)
                                   
#include

#define FALSE 0 /* btw, if TC2 is ANSI compliant why i have to do this?*/
#define TRUE 1  /* I thought they're supposed to be defined already ? */


int Like(char *str1,char *pattern)
{
 char startchar,endchar;int found;
 char *ptr;

 while(*pattern)
 {
  if(!*str1) return FALSE;
 
  switch(*pattern)
  {
   case '*' :   pattern++;
           if(*pattern)
           {
            while(*str1++ != *pattern) if(!*str1) return FALSE;
            pattern++;continue;
            }
           else return TRUE;

   case '?' :   pattern++;str1++;
           break;

   case '#' :   if(*str1  '9') return FALSE;
           str1++;pattern++;
           break;

   case '[' :   pattern++;
           startchar = *pattern;
           if(*++pattern == '-')
           {
            ++pattern;endchar = *pattern++;
            while(*str1)
            {
             if(*str1  endchar) return FALSE;
             str1++;
            }
            return TRUE;
           }
           else if(*pattern == ']')
           {
            if(startchar != *str1++) return FALSE;
            pattern++;
            }

           else{
                pattern--;
                while(*str1)
                {
                 ptr = pattern;found = 0;
                 while(*ptr && *ptr != ']' && !found)
                 {
                  if(*str1 == *ptr++) found = 1;
                  }
                 if(!found) return FALSE;
                 str1++;
                }
               return TRUE;
               }
           break;
   default  :
           if(*str1 == *pattern) {pattern++;str1++;}
         else return FALSE;

    }
   }
if(*str1) return FALSE;
return TRUE;
}



int main(int argc, char* argv[])
{
 int retval;
 retval = Like("1Hello","#?[H-o]");
 printf("\nResult = %s",retval == 1 ? "True" :
"False");
 return 0;
 }

ps: any ideas, how the in-brackets search could be made better ?

___ Blue Wave/DOS v2.21

--- Maximus 3.01
* Origin: The Software Parlour (3:635/544)
SEEN-BY: 50/99 620/243 623/630 632/349 635/503 544 727 711/401 409 410 413
SEEN-BY: 711/430 808 809 934 712/515 713/888 714/906 800/1
@PATH: 635/544 50/99 711/808 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.