Logo Search packages:      
Sourcecode: xcingb version File versions  Download package

valid.c

#define MustOne(num) (num)==1 ? 1:0
#define NonZero(num) (num)> 0 ? 1:0
#define Delete(old,pos,num) strcpy((old)+(pos)-1,(old)+(pos)+(num)-1)

extern char *strstr(const char *haystack, const char *needle);
extern char *strchr(const char *s, int c);

int isBig5(unsigned char first, unsigned char second)
{
 return (  ((0xA1 <= first) && (first <= 0xFE)) &&
           (
            ((0x40 <= second) && (second <= 0x7E)) ||
            ((0xA1 <= second) && (second <= 0xFE))
           )
        );
}

static char *MyStrrev(char *old)
/* 此函式相当於 strrev(),只不过 FreeBSD 上没有这个函式,只好自己写一个 */
{
   char temp,*p1=old,*p2=old+strlen(old)-1;

   for(;p1<p2;temp=*p2,*p2--=*p1,*p1++=temp) ;
   return old;
}

static int LoPos( char *small,char *big)
{
   char *p,*pos;
   int found=0;

   p=pos=big;
   for (;  *p  && (p = strstr(p, small))  && !found ; found = (pos == p++))
      while ( pos < p)  pos += isBig5(*pos, *(pos+1)) ? 2 : 1;

   return (found) ?  pos-big+1 : 0;
}


static char* Copy(char *nnew,char *old,int pos,int num)
{
  char *p1=nnew,*p2=old+pos-1,*p3=old+pos+num-2;

  while ( (p2<=p3) ) *(p1++)=*(p2++);

  *p1='\0';

  return nnew;
}

static int ask(char *small, char *big)
{
  int j=0,found=0;

  while( ( j<=strlen(big)-strlen(small) ) && !found )
  {
      int i = 0;

      j++;
      found = 1;

      while (( i++ <= strlen(small)-1 ) && found )
        if ( small[i-1] != '?' )
          if ( small[i-1] != big[i+j-2] ) found = 0 ;
  }

  return found ? j : 0;
}


int MyPos(char *small,char *big,int count1, int count2)
{
  char P1[20],P2[20];
  char Small[20],Big[20];

  strcpy(Small,small);
  strcpy(Big,big);

  if (count1 < 3) count1++;

  if ( !strchr(Small,'*') && !strchr(Small,'?') ) return LoPos(Small,Big);

  if ( Small[0] == '*' )
  {
    Delete(Small,1,1);

    switch ( Small[strlen(Small)-1] )
    {
      case '*':
        Delete(Small,strlen(Small),1);
        return MyPos(Small,Big,count1,2);
      default: 
        return MustOne(MyPos(MyStrrev(Small),MyStrrev(Big),count1,1 ));
      }
    }

  if ( Small[strlen(Small)-1] == '*')
  {
    Delete(Small,strlen(Small),1);
    return MustOne(MyPos(Small,Big,count1,3));
    }

  if ( strchr(Small,'*') )
  {
    Copy( P1 , Small , 1 , LoPos("*",Small)-1 );
    Copy( P2 , Small , LoPos("*",Small)+1 , strlen(Small)-LoPos("*",Small) );

    if (count1 == 1)
    {
      if ( MustOne(MyPos(P1,Big,count1,4)) )
      {
          Delete(Big,1,strlen(P1));
          return MustOne(MyPos(MyStrrev(P2),MyStrrev(Big),count1,4));
          }
      else return 0;
      }

    if (count1 == 3)
    {
        if ( NonZero(MyPos(P1,Big,count1,4)) )
        {
          Delete(Big,1,MyPos(P1,Big,count1,4)+strlen(P1)-1);
          return NonZero(MyPos(MyStrrev(P2),MyStrrev(Big),count1,4));
          }
        else return 0;
        }

    if (count1 == 2)
    {
        if ( count2 == 1 )
        {
           if ( MustOne(MyPos(P1,Big,count1-1,count2)) )
           {
              Delete(Big,1,strlen(P1));
              return NonZero(MyPos(MyStrrev(P2),MyStrrev(Big),count1,4));
              }
           return 0;
           }

        if (count2 == 2)
        {
          if ( NonZero(MyPos(P1,Big,count1,4)) )
          {
             Delete(Big,1,MyPos(P1,Big,count1,4)+strlen(P1)-1);
             return NonZero(MyPos(MyStrrev(P2),MyStrrev(Big),count1,4));
             }
          return 0;
          }

      if (count2 == 3)
      {
          if ( MustOne(MyPos(P1,Big,count1-1,count2)) )
          {
            Delete(Big,1,strlen(P1));
            return NonZero(MyPos(MyStrrev(P2),MyStrrev(Big),count1,4));
            }
          return 0;
          }

      if (count2 == 4)
      {
         if ( MustOne(MyPos(P1,Big,count1-1,count2)) )
         {
            Delete(Big,1,strlen(P1));
            return NonZero(MyPos(MyStrrev(P2),MyStrrev(Big),count1,4));
            }
         return 0;
         }
    }
  }


  if ( strlen(Small)>strlen(Big) ) return 0;
  if ( (count1 == 1) && (strlen(Small)<strlen(Big)) ) return 0;
  if (count1 == 1)
     return ( strlen(Small)<strlen(Big) ) ? 0 : MustOne(ask(Small,Big));
  else return ask(Small,Big);

}


Generated by  Doxygen 1.6.0   Back to index