00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include <ctype.h>
00035 #include <string.h>
00036 #include "match.h"
00037 #include "errcodes.h"
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 #define ESCAPE_CHARACTER '~'
00080
00081
00082 static char JokerCharacter[] = "*#$@?&%!+/|<>";
00083
00084 #define F 0xFF
00085 #define E 0xFE
00086 static MatchSets DefaultMatchSets = {
00087
00088 { MULTIJOKER, MULTIJOKER, MULTIJOKER, MULTIJOKER, SINGLEJOKER,
00089
00090 NOJOKER, NOJOKER, NOJOKER, NOJOKER, NOJOKER, NOJOKER, NOJOKER, NOJOKER },
00091
00092 {
00093
00094
00095
00096 { F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F} ,
00097 { 0, 0, 0, 0, 0, 0, F, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} ,
00098 { 0, 0, 0, 0, 0, 0, F, 3, E, F, F, 7, E, F, F, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} ,
00099 { 0, 0, 0, 0, 0, 0, 0, 0, E, F, F, 7, E, F, F, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} ,
00100 { F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F} ,
00101 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} ,
00102 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} ,
00103 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} ,
00104 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} ,
00105
00106 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} ,
00107 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} ,
00108 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
00109 }
00110 };
00111 #undef F
00112 #undef E
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126 unsigned long match_index(char ch
00127 ){
00128
00129
00130 int i;
00131 for( i = 0 ; JokerCharacter[i] ; i++ )
00132 if( JokerCharacter[i] == ch )return i+1;
00133
00134 return 0;
00135 }
00136
00137 static unsigned char *MultiJokerSet(pMatchSets pMS, char ch){
00138 int i;
00139
00140 if( ! (i = match_index(ch)) )return NULL;
00141 i--;
00142 if( pMS->SetType[i] == MULTIJOKER )return pMS->set[i];
00143 return NULL;
00144 }
00145
00146 static unsigned char *SingleJokerSet(pMatchSets pMS, char ch){
00147 int i;
00148
00149 if( !(i = match_index(ch)) )return NULL;
00150 i--;
00151 if( pMS->SetType[i] == SINGLEJOKER )return pMS->set[i];
00152 return NULL;
00153 }
00154
00155 static int JokerMatch(unsigned char *set, unsigned char ch){
00156 int index,offset;
00157
00158 index = ch >> 3;
00159 offset = ch & 7;
00160 index = set[index];
00161 index >>= offset;
00162 return index;
00163 }
00164
00165
00166
00167
00168
00169
00170
00171
00172 void match_InitSets(pMatchSets pMS
00173 ){
00174
00175
00176 memcpy((void *)pMS,(void *)&DefaultMatchSets,sizeof(DefaultMatchSets));
00177 }
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 void match_ModifySet(pMatchSets pMS,
00239 char JokerCharacter,
00240 int nChars,
00241 unsigned char *pch,
00242 int fAction
00243 ){
00244
00245
00246 int i,index,offset;
00247 unsigned char *set,mask;
00248
00249 i = match_index(JokerCharacter);
00250 if( i == 0 )return;
00251 i--;
00252 if( fAction & MATCH_SNOJ )pMS->SetType[i] = NOJOKER;
00253 if( fAction & MATCH_SSIJ )pMS->SetType[i] = SINGLEJOKER;
00254 if( fAction & MATCH_SMUJ )pMS->SetType[i] = MULTIJOKER;
00255
00256 set = pMS->set[i];
00257 if( fAction & MATCH_NULS )
00258 for( i=0 ; i<32 ; i++ )set[i] = 0;
00259 if( fAction & MATCH_FULS )
00260 for( i=0 ; i<32 ; i++ )set[i] = 0xFF;
00261
00262 if( fAction & (MATCH_ADDC|MATCH_REMC) )
00263 while( nChars-- ){
00264 mask = 1;
00265 index = (*pch) >> 3;
00266 offset = (*pch) & 7;
00267 mask <<= offset;
00268 if( fAction & MATCH_ADDC )set[index] |= mask;
00269 if( fAction & MATCH_INVC )set[index] ^= mask;
00270 if( fAction & MATCH_REMC ){
00271 mask = ~mask;
00272 set[index] &= mask;
00273 }
00274 pch++;
00275 }
00276 return;
00277 }
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380 int match_match(char *pszPattern,
00381 unsigned long cbPattern,
00382 char *pszString,
00383 unsigned long cbString,
00384 char **ParameterArray,
00385 unsigned long *pcbParameterArray,
00386 char *pszBuffer,
00387 int cArraySize,
00388 int cbBufferSize,
00389 int fCase,
00390 pMatchSets pThisMatchSets,
00391 int *iResult
00392 ){
00393
00394
00395 char cA,cB;
00396 int ErrorCode;
00397 unsigned char *set;
00398
00399 if( pThisMatchSets == NULL )pThisMatchSets = &DefaultMatchSets;
00400
00401 *iResult = 0;
00402 while( 1 ){
00403
00404 if( ! cbString ){
00405 *iResult = ! cbPattern;
00406 return MATCH_ERROR_SUCCESS;
00407 }
00408
00409 if( set=MultiJokerSet(pThisMatchSets,*pszPattern) ){
00410 if( ! cbString )return MATCH_ERROR_SUCCESS;
00411 if( cArraySize == 0 )return MATCH_ERROR_ARRAY_SHORT;
00412 if( cbBufferSize < 1 )return MATCH_ERROR_BUFFER_SHORT;
00413
00414 cA = *pszString;
00415 if( !fCase && islower(cA) )cA = toupper(cA);
00416 if( ! JokerMatch(set,cA) )return MATCH_ERROR_SUCCESS;
00417
00418
00419 *ParameterArray = pszBuffer;
00420 *pszBuffer++ = *pszString++;
00421 cbString--;
00422 cbBufferSize--;
00423 *pcbParameterArray = 1;
00424
00425 while( (ErrorCode=match_match(pszPattern+1,
00426 cbPattern-1,
00427 pszString,
00428 cbString,
00429 ParameterArray+1,
00430 pcbParameterArray+1,
00431 pszBuffer,
00432 cArraySize-1,
00433 cbBufferSize,
00434 fCase,
00435 pThisMatchSets,
00436 iResult)
00437 ) == MATCH_ERROR_SUCCESS
00438 &&
00439 !*iResult ){
00440 if( ! cbString )return MATCH_ERROR_SUCCESS;
00441 cA = *pszString;
00442 if( !fCase && islower(cA) )cA = toupper(cA);
00443 if( ! JokerMatch(set,cA) )return MATCH_ERROR_SUCCESS;
00444 *pszBuffer++ = *pszString++;
00445 cbBufferSize --;
00446 cbString--;
00447 (*pcbParameterArray)++;
00448 if( ! cbBufferSize )return MATCH_ERROR_BUFFER_SHORT;
00449 }
00450 *iResult = 1;
00451 return MATCH_ERROR_SUCCESS;
00452 }
00453
00454
00455 if( *pszPattern == ESCAPE_CHARACTER ){
00456 pszPattern ++;
00457 cbPattern--;
00458 if( ! cbPattern )return MATCH_ERROR_SYNTAX_ERROR;
00459
00460 if( ! fCase ){
00461 cA = *pszPattern;
00462 cB = *pszString;
00463 if( islower(cA) )cA = toupper(cA);
00464 if( islower(cB) )cB = toupper(cB);
00465 if( cA != cB )return MATCH_ERROR_SUCCESS;
00466 }else{
00467 if( *pszPattern != *pszString )return MATCH_ERROR_SUCCESS;
00468 }
00469 }else{
00470
00471 if( set=SingleJokerSet(pThisMatchSets,*pszPattern) ){
00472 cA = *pszString;
00473 if( !fCase && islower(cA) )cA = toupper(cA);
00474 if( ! JokerMatch(set,cA) )return MATCH_ERROR_SUCCESS;
00475 *ParameterArray++ = pszBuffer;
00476 *pszBuffer++ = *pszString;
00477 cbBufferSize--;
00478 *pcbParameterArray++ = 1;
00479 }
00480 else if( *pszPattern == ' ' ){
00481 if( ! isspace(*pszString) )return MATCH_ERROR_SUCCESS;
00482 while( cbString && isspace( *pszString ) ){
00483 pszString ++;
00484 cbString --;
00485 }
00486 pszString--;
00487 cbString++;
00488 }else{
00489
00490 if( ! fCase ){
00491 cA = *pszPattern;
00492 cB = *pszString;
00493 if( islower(cA) )cA = toupper(cA);
00494 if( islower(cB) )cB = toupper(cB);
00495 if( cA != cB )return MATCH_ERROR_SUCCESS;
00496 }else{
00497 if( *pszPattern != *pszString )return MATCH_ERROR_SUCCESS;
00498 }
00499 }
00500 }
00501 pszPattern++; pszString++;
00502 cbPattern--; cbString--;
00503 }
00504 }
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514 int match_count(char *pszPattern,
00515 unsigned long cbPattern
00516 ){
00517
00518
00519 int iCounter = 0;
00520
00521 while( cbPattern ){
00522 if( match_index(*pszPattern) )iCounter++;
00523 if( *pszPattern == ESCAPE_CHARACTER ){
00524 pszPattern++;
00525 cbPattern--;
00526 if( ! cbPattern )return iCounter;
00527 }
00528 pszPattern++;
00529 cbPattern--;
00530 }
00531 return iCounter;
00532 }
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591 int match_parameter(char *pszFormat,
00592 unsigned long cbFormat,
00593 char **ParameterArray,
00594 unsigned long *pcbParameterArray,
00595 char *pszBuffer,
00596 int cArraySize,
00597 unsigned long *pcbBufferSize
00598 ){
00599
00600
00601 int index;
00602 char *s;
00603 unsigned long ulParamSize,ulResultSize;
00604
00605 ulResultSize = 0;
00606 while( cbFormat ){
00607 if( *pszFormat == '$' ){
00608
00609
00610
00611 pszFormat++;
00612 cbFormat --;
00613 if( cbFormat && *pszFormat == '$' ){
00614 *pszBuffer++ = *pszFormat++;
00615 cbFormat--;
00616 (*pcbBufferSize)--;
00617 ulResultSize;
00618 if( *pcbBufferSize < 1 )return MATCH_ERROR_BUFFER_SHORT;
00619
00620 }else{
00621
00622
00623
00624 if( cbFormat && isdigit( *pszFormat ) ){
00625 index = 0;
00626 while( cbFormat && isdigit( *pszFormat ) ){
00627 index = 10*index + *pszFormat - '0';
00628 pszFormat++;
00629 cbFormat--;
00630 }
00631
00632
00633 if( index >= cArraySize )return MATCH_ERROR_INDEX_OUT_OF_RANGE;
00634 s = ParameterArray[index];
00635 ulParamSize = pcbParameterArray[index];
00636 while( ulParamSize ){
00637 if( *pcbBufferSize < 1 )return MATCH_ERROR_BUFFER_SHORT;
00638 *pszBuffer++ = *s++;
00639 (*pcbBufferSize)--;
00640 ulResultSize++;
00641 ulParamSize--;
00642 }
00643 }else{
00644
00645
00646
00647 *pszBuffer++ = '$';
00648 (*pcbBufferSize)--;
00649 ulResultSize++;
00650 if( *pcbBufferSize < 1 )return MATCH_ERROR_BUFFER_SHORT;
00651 }
00652 }
00653
00654 }else{
00655
00656
00657
00658 if( *pcbBufferSize < 1 )return MATCH_ERROR_BUFFER_SHORT;
00659 *pszBuffer++ = *pszFormat++;
00660 (*pcbBufferSize)--;
00661 ulResultSize++;
00662 cbFormat--;
00663 }
00664 }
00665 *pcbBufferSize = ulResultSize;
00666 return MATCH_ERROR_SUCCESS;
00667 }
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681 int match_size(char *pszFormat,
00682 unsigned long cbFormat,
00683 unsigned long *pcbParameterArray,
00684 int cArraySize,
00685 int *cbBufferSize
00686 ){
00687
00688
00689 int index;
00690
00691 while( cbFormat ){
00692 if( *pszFormat == '$' ){
00693 pszFormat++;
00694 cbFormat--;
00695 if( cbFormat && *pszFormat == '$' ){
00696 pszFormat++;
00697 cbFormat--;
00698 (*cbBufferSize)++;
00699
00700 }else{
00701
00702
00703
00704 if( cbFormat && isdigit( *pszFormat ) ){
00705 index = 0;
00706 while( cbFormat && isdigit( *pszFormat ) ){
00707 index = 10*index + *pszFormat - '0';
00708 pszFormat++;
00709 cbFormat--;
00710 }
00711
00712
00713 if( index >= cArraySize )return MATCH_ERROR_INDEX_OUT_OF_RANGE;
00714 (*cbBufferSize) += pcbParameterArray[index];
00715 }else{
00716
00717
00718
00719 cbBufferSize ++;
00720 }
00721 }
00722
00723 }else{
00724
00725
00726
00727 pszFormat++;
00728 cbFormat--;
00729 (*cbBufferSize)++;
00730 }
00731 }
00732 return MATCH_ERROR_SUCCESS;
00733 }
00734