File: valid.c

package info (click to toggle)
xcingb 2.3.02-5
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,492 kB
  • ctags: 877
  • sloc: ansic: 9,779; sh: 1,460; makefile: 299; awk: 12
file content (177 lines) | stat: -rw-r--r-- 4,066 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#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)

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);

}