File: lists.c

package info (click to toggle)
pgn-extract 16.7-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 936 kB
  • ctags: 722
  • sloc: ansic: 9,243; makefile: 63
file content (628 lines) | stat: -rw-r--r-- 21,881 bytes parent folder | download | duplicates (2)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
/*
 *  Program: pgn-extract: a Portable Game Notation (PGN) extractor.
 *  Copyright (C) 1994-2005 David Barnes
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 1, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  David Barnes may be contacted as D.J.Barnes@kent.ac.uk
 *  http://www.cs.kent.ac.uk/people/staff/djb/
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "bool.h"
#include "mymalloc.h"
#include "defs.h"
#include "typedef.h"
#include "lists.h"
#include "taglist.h"

    /* Define a type to permit tag strings to be associated with
     * a TagOperator for selecting relationships between them
     * and a game to be matched.
     */
typedef struct{
    char *tag_string;
    TagOperator operator;
} TagSelection;

    /* Definitions for maintaining arrays of tag strings.
     * These arrays are used for various purposes:
     *        lists of white/black players to extract on.
     *        lists of other criteria to extract on.
     */

typedef struct {
    /* How many elements we have currently allocated for.
     * If this is > 0 then we should always allocated exactly
     * one more than this number to simplify the (char **)NULL
     * termination of the list.
     */
    unsigned num_allocated_elements;
    /* num_used_elements should always be <= num_allocated elements. */
    unsigned num_used_elements;
    /* The list of elements.
     * Elements 0 .. num_used_elements point to null-terminated strings.
     * list[num_used_elements] == (char **)NULL once the list is complete.
     */
    TagSelection *tag_strings;
} StringArray;

        /* Functions to allow creation of string lists. */
/* Allow a string list for every known tag.
 * It is important that these lists should be initialised to
 *         { 0, 0, (TagSelection *) NULL }
 * which happens by default, anyway.
 * This array, and its length (tag_list_length) are initialised
 * by calling init_tag_lists.
 */
static StringArray *TagLists;
static int tag_list_length  =  0;

static char *soundex(const char *str);
static Boolean CheckList(int tag,const char *tag_string,StringArray *list);

void init_tag_lists(void)
{
    tag_list_length = ORIGINAL_NUMBER_OF_TAGS;
    TagLists = (StringArray *) MallocOrDie(tag_list_length*sizeof(*TagLists));
}

static void extend_tag_list_length(int new_length)
{
    if(new_length < tag_list_length){
        fprintf(GlobalState.logfile,
                "Internal error: inappropriate call to extend_tag_list_length().\n");
        fprintf(GlobalState.logfile,
                "New length of %d is not greater than existing length of %d\n",
                new_length,tag_list_length);
        exit(1);
    }
    else{
        int i;
        TagLists = (StringArray *) ReallocOrDie((void *) TagLists,
                                           new_length*sizeof(*TagLists));
        for(i = tag_list_length; i < new_length; i++){
             TagLists[i].num_allocated_elements  = 0;
             TagLists[i].num_used_elements  = 0;
             TagLists[i].tag_strings  = (TagSelection *) NULL;
        }
        tag_list_length = new_length;
    }
}

        /* Add str to the list of strings in list.
         * List may be a new list, in which case space is allocated
         * for it.
         * Return the index on success, otherwise -1.
         */
static int
add_to_taglist(const char *str,StringArray *list)
{   Boolean everything_ok = TRUE;

    if(list->num_allocated_elements == list->num_used_elements){
        /* We need more space. */
        if(list->num_allocated_elements == 0){
            /* No elements in the list. */
            list->tag_strings = (TagSelection *)MallocOrDie((INIT_LIST_SPACE+1)*
                                                sizeof(TagSelection));
            if(list->tag_strings != NULL){
                list->num_allocated_elements = INIT_LIST_SPACE;
                list->num_used_elements = 0;
            }
            else{
                everything_ok = FALSE;
            }
        }
        else{
            list->tag_strings = (TagSelection *)realloc((void *)list->tag_strings,
                                (list->num_allocated_elements+MORE_LIST_SPACE+1)*
                                                        sizeof(TagSelection));
            if(list->tag_strings != NULL){
                list->num_allocated_elements += MORE_LIST_SPACE;
            }
            else{
                everything_ok = FALSE;
            }
        }
    }
    if(everything_ok){
        /* There is space. */
        unsigned ix = list->num_used_elements;

        list->tag_strings[ix].operator = NONE;
        list->tag_strings[ix].tag_string = (char *) MallocOrDie(strlen(str)+1);
        if(list->tag_strings[ix].tag_string != NULL){
            strcpy(list->tag_strings[ix].tag_string,str);
            list->num_used_elements++;
            /* Make sure that the list is properly terminated at all times. */
            list->tag_strings[ix+1].tag_string = NULL;
            return (int) ix;
        }
        else{
            return -1;
        }
    }
    else{
        return -1;
    }
}

    /* Simple soundex code supplied by John Brogan
     * (jwbrogan@unix2.netaxs.com), 26th Aug 1994.
     * John writes:
     * "In recognition of the large number of strong players from countries
     * with Slavic based languages, I tried to tailor the soundex code
     * to match any reasonable transliteration of a Slavic name into
     * English.  Thus, Nimzovich will match Nimsowitsch, Tal will match
     * Talj, etc.  Unfortunately, in order to be sure not to miss any
     * valid matches, I had to make the code so tolerant that it sometimes
     * comes up with some wildly false matches.  This, to me, is better
     * than missing some games, but your mileage may differ."
     *
     * This looks like it was originally derived from the public domain
     * version released by N. Dean Pentcheff, 1989, which was, itself,
     * based on that in D.E. Knuth's "The art of computer programming.",
     * Volume 3: Sorting and searching.  Addison-Wesley Publishing Company:
     * Reading, Mass. Page 392.
     * Amended by David Barnes, 2nd Sep 1994.
     */

/* Define a maximum length for the soundex result. */
#define MAXSOUNDEX 50

        /* Calculate a soundex string for instr.
         * The space used is statically allocated, so the caller
         * will have to allocate its own for the result if it
         * to be retained across different calls.
         */
static char *
soundex(const char *str)
{   static char sbuf[MAXSOUNDEX+1];
    /* An index into sbuf. */
    unsigned sindex = 0;
    /* Keep track of the last character to compress repeated letters. */
    char lastc = ' ';
    /*                     ABCDEFGHIJKLMNOPQRSTUVWXYZ */
    const char *mapping = "01230120002455012622011202";
    char initial_letter = *str;

    /* Special case for names that begin with 'J',
     * otherwise Janosevic == Nimzovich.
     * In addition, we really want Yusupov to match Jusupov.
     */
    if(islower((int) initial_letter)){
        initial_letter = toupper(initial_letter);
    }
    if((initial_letter == 'Y') || (initial_letter == 'J')){
          sbuf[sindex] = '7';
          str++;
          sindex++;
    }

    while((*str != '\0') && (sindex < MAXSOUNDEX)){
        char ch = *str;

        /* We are only interested in alphabetics, and duplicate
         * characters are reduced to singletons.
         */
        if(isalpha((int) ch) && (ch != lastc)){
             char translation;

             if(islower((int) ch)){
                 ch = toupper(ch);
             }
             /* Pick up the translation. */
             translation = mapping[ch - 'A'];
             if((translation != '0') && (translation != lastc)){
                sbuf[sindex] = translation;
                sindex++;
                lastc = translation;
             }
        }
        str++; 
    }
    sbuf[sindex] = '\0';
    return(sbuf);
}

        /* Return TRUE if tag is one on which soundex matching should
         * be used, if requested.
         */
static Boolean
soundex_tag(int tag)
{   Boolean use_soundex = FALSE;

    switch(tag){
        case WHITE_TAG:
        case BLACK_TAG:
        case PSEUDO_PLAYER_TAG:
        case EVENT_TAG:
        case SITE_TAG:
        case ANNOTATOR_TAG:
            use_soundex = TRUE;
            break;
    }
    return use_soundex;
}

        /* Add tagstr to the list of tags to be matched.
         * If we are using soundex matching, then store
         * its soundex version rather than its plain text.
         */
void
add_tag_to_list(int tag,const char *tagstr,TagOperator operator)
{
    if(tag >= tag_list_length){
        /* A new tag - one without a pre-defined _TAG #define.
         * Make sure there is room for it in TagList.
         */
        extend_tag_list_length(tag+1);
    }
    if((tag >= 0) && (tag < tag_list_length)){
        const char *string_to_store = tagstr;
        int ix;

        if(GlobalState.use_soundex){
            if(soundex_tag(tag)){
                string_to_store = soundex(tagstr);
            }
        }
        ix = add_to_taglist(string_to_store,&TagLists[tag]);
        if(ix >= 0){
            TagLists[tag].tag_strings[ix].operator = operator;
        }
        /* Ensure that we know we are checking tags. */
        GlobalState.check_tags = TRUE;
    }
    else{
        fprintf(GlobalState.logfile,
                "Illegal tag number %d in add_tag_to_list.\n",tag);
    }
}

        /* Argstr is an extraction argument.
         * The type of argument is indicated by the first letter of
         * argstr:
         *        a -- annotator of the game
         *        b -- player of the black pieces
         *        d -- date of the game
         *        e -- ECO code
         *        p -- player of either colour
         *        r -- result
         *        w -- player of the white pieces
         * The remainder of argstr is the argument string to be entered
         * into the appropriate list.
         */
void
extract_tag_argument(const char *argstr)
{   const char *arg = &(argstr[1]);

    switch(*argstr){
        case 'a':
            add_tag_to_list(ANNOTATOR_TAG,arg,NONE);
            break;
        case 'b':
            add_tag_to_list(BLACK_TAG,arg,NONE);
            break;
        case 'd':
            add_tag_to_list(DATE_TAG,arg,NONE);
            break;
        case 'e':
            add_tag_to_list(ECO_TAG,arg,NONE);
            break;
        case 'p':
            add_tag_to_list(PSEUDO_PLAYER_TAG,arg,NONE);
            break;
        case 'r':
            add_tag_to_list(RESULT_TAG,arg,NONE);
            break;
        case 'w':
            add_tag_to_list(WHITE_TAG,arg,NONE);
            break;
        default:
            fprintf(GlobalState.logfile,
                    "Unknown type of tag extraction argument: %s\n",
                        argstr);
            exit(1);
            break;
    }
}

        /* Check for one of list->strings matching the date_string.
         * Return TRUE on match, FALSE on failure.
         * It is only necessary for a prefix of tag to match
         * the string.
         */

/* Set limits on the allowable ranges for dates extracted from games.
 * Because of the imminent century change, it is difficult to know what
 * best to do with two digit year numbers; so exclude them.
 */
#define MINDATE 100
#define MAXDATE 3000

static Boolean
CheckDate(const char *date_string,StringArray *list)
{   unsigned list_index;
    Boolean wanted = TRUE;

    for(list_index = 0; (list_index < list->num_used_elements) && wanted;
                                list_index++){
        const char *list_string = list->tag_strings[list_index].tag_string;
        TagOperator operator = list->tag_strings[list_index].operator;

        if(*list_string == 'b'){
            operator = LESS_THAN;
            list_string++;
        }
        else if(*list_string == 'a'){
            operator = GREATER_THAN;
            list_string++;
        }
        else{
            /* No prefix. */
        }
        if(operator != NONE){
            /* We have a relational comparison. */
            unsigned game_date, list_date;
            /* Try to extract dates from both strings. */
            if((sscanf(list_string,"%u",&list_date) == 1) &&
                  (sscanf(date_string,"%u",&game_date) == 1) &&
                  (game_date > MINDATE) && (game_date < MAXDATE)){
                switch(operator){
                    case LESS_THAN:
                        wanted = game_date < list_date;
                        break;
                    case LESS_THAN_OR_EQUAL_TO:
                        wanted = game_date <= list_date;
                        break;
                    case GREATER_THAN:
                        wanted = game_date > list_date;
                        break;
                    case GREATER_THAN_OR_EQUAL_TO:
                        wanted = game_date >= list_date;
                        break;
                    case EQUAL_TO:
                        wanted = game_date == list_date;
                        break;
                    case NOT_EQUAL_TO:
                        wanted = game_date != list_date;
                        break;
                    case NONE:
                        /* Already covered above. */
                        break;
                }
            }
            else{
                /* Bad format, or out of range. Assume not wanted. */
            }
        }
        else{
            /* Just a straight prefix match. */
            wanted = strncmp(date_string,list_string,strlen(list_string)) == 0;
        }
    }
    return wanted;
}

static Boolean
CheckElo(const char *elo_string,StringArray *list)
{   unsigned list_index;
    Boolean wanted = TRUE;

    for(list_index = 0; (list_index < list->num_used_elements) && wanted;
                                list_index++){
        const char *list_string = list->tag_strings[list_index].tag_string;
        TagOperator operator = list->tag_strings[list_index].operator;

        if(operator != NONE){
            /* We have a relational comparison. */
            unsigned game_elo, list_elo;
            /* Try to extract elos from both strings. */
            if((sscanf(list_string,"%u",&list_elo) == 1) &&
                  (sscanf(elo_string,"%u",&game_elo) == 1)){
                switch(operator){
                    case LESS_THAN:
                        wanted = game_elo < list_elo;
                        break;
                    case LESS_THAN_OR_EQUAL_TO:
                        wanted = game_elo <= list_elo;
                        break;
                    case GREATER_THAN:
                        wanted = game_elo > list_elo;
                        break;
                    case GREATER_THAN_OR_EQUAL_TO:
                        wanted = game_elo >= list_elo;
                        break;
                    case EQUAL_TO:
                        wanted = game_elo == list_elo;
                        break;
                    case NOT_EQUAL_TO:
                        wanted = game_elo != list_elo;
                        break;
                    case NONE:
                        /* Already covered above. */
                        break;
                }
            }
            else{
                /* Bad format, or out of range. Assume not wanted. */
                wanted = FALSE;
            }
        }
        else{
            /* Just a straight prefix match. */
            if(strncmp(elo_string,list_string,strlen(list_string)) == 0){
                wanted = TRUE;
            }
        }
    }
    return wanted;
}

        /* Check for one of list->strings matching the tag.
         * Return TRUE on match, FALSE on failure.
         * It is only necessary for a prefix of tag to match
         * the string.
         */
static Boolean
CheckList(int tag,const char *tag_string,StringArray *list)
{   unsigned list_index;
    Boolean wanted = FALSE;
    const char *search_str;

    if(GlobalState.use_soundex && soundex_tag(tag)){
        search_str = soundex(tag_string);
    }
    else{
        search_str = tag_string;
    }
    for(list_index = 0; (list_index < list->num_used_elements) && !wanted;
                                list_index++){
        const char *list_string = list->tag_strings[list_index].tag_string;

        if(strncmp(search_str,list_string,strlen(list_string)) == 0){
            wanted = TRUE;
        }
    }
    return wanted;
}

        /* Check the Tag Details of this current game against those in
         * the wanted lists. Check all details apart from any ECO
         * tag as this is checked separately by CheckECOTag.
         * An empty list implies that there is no restriction on
         * the values in the corresponding tag field.
         * In consequence, completely empty lists imply that all
         * games reaching this far are wanted.
         * Return TRUE if wanted, FALSE otherwise.
         */
Boolean
CheckTagDetailsNotECO(char *Details[],int num_details)
{   Boolean wanted = TRUE;
    int tag;

    if(GlobalState.check_tags){
        /* Sanity check. */
        if(num_details < tag_list_length){
            fprintf(GlobalState.logfile,
                   "Internal error: mismatch in tag set lengths in ");
            fprintf(GlobalState.logfile,
                   "CheckTagDetailsNotECO: %d vs %d\n",
                   num_details,tag_list_length);
            exit(1);
        }

        /* PSEUDO_PLAYER_TAG and PSEUDO_ELO_TAG are treated differently,
         * since they have the effect of or-ing together the WHITE_ and BLACK_ lists.
         * Otherwise, different tag lists are and-ed.
         */
        if(TagLists[PSEUDO_PLAYER_TAG].num_used_elements != 0){
            /* Check both the White and Black lists. */
            if(Details[WHITE_TAG] != NULL){
                wanted = CheckList(WHITE_TAG,Details[WHITE_TAG],
                                    &TagLists[PSEUDO_PLAYER_TAG]);
                /* If we didn't find a player there, try for the opponent. */
                if(!wanted && (Details[BLACK_TAG] != NULL)){
                    wanted = CheckList(BLACK_TAG,Details[BLACK_TAG],
                                    &TagLists[PSEUDO_PLAYER_TAG]);
                }
            }
            else if(Details[BLACK_TAG] != NULL){
                wanted = CheckList(BLACK_TAG,Details[BLACK_TAG],
                                    &TagLists[PSEUDO_PLAYER_TAG]);
            }
            else{
                wanted = FALSE;
            }
        }
        else if(TagLists[PSEUDO_ELO_TAG].num_used_elements != 0){
            /* Check both the White and Black lists. */
            if(Details[WHITE_ELO_TAG] != NULL){
                wanted = CheckElo(Details[WHITE_ELO_TAG],
                                    &TagLists[PSEUDO_ELO_TAG]);
                /* If we didn't find a player there, try for the opponent. */
                if(!wanted && (Details[BLACK_ELO_TAG] != NULL)){
                    wanted = CheckElo(Details[BLACK_ELO_TAG],
                                    &TagLists[PSEUDO_ELO_TAG]);
                }
            }
            else if(Details[BLACK_ELO_TAG] != NULL){
                wanted = CheckElo(Details[BLACK_ELO_TAG],
                                    &TagLists[PSEUDO_ELO_TAG]);
            }
            else{
                wanted = FALSE;
            }
        }
        else{
            /* No PSEUDO_*_TAG info to check. */
        }

        /* Check the remaining tags in turn as long as we still have a match. */
        for(tag = 0; (tag < tag_list_length) && wanted; tag++){
            if(tag == PSEUDO_PLAYER_TAG){
            }
            else if(tag == PSEUDO_ELO_TAG){
            }
            else if(tag == ECO_TAG){
                /* This is handled separately. */
            }
            else if(TagLists[tag].num_used_elements != 0){
                if(Details[tag] != NULL){
                    if(tag == DATE_TAG){
                        wanted = CheckDate(Details[tag],&TagLists[tag]);
                    }
                    else if((tag == WHITE_ELO_TAG) || (tag == BLACK_ELO_TAG)){
                        wanted = CheckElo(Details[tag],&TagLists[tag]);
                    }
                    else{
                        wanted = CheckList(tag,Details[tag],&TagLists[tag]);
                    }
                }
                else{
                    /* Required tag not present. */
                    wanted = FALSE;
                }
            }
            else{
                /* Not used. */
            }
        }
    }
    return wanted;
}

        /* Check just the ECO tag from the game's tag details. */
Boolean
CheckECOTag(char *Details[])
{   Boolean wanted = TRUE;

    if(GlobalState.check_tags){
        if(TagLists[ECO_TAG].num_used_elements != 0){
            if(Details[ECO_TAG] != NULL){
                wanted = CheckList(ECO_TAG,Details[ECO_TAG],&TagLists[ECO_TAG]);
            }
            else{
                /* Required tag not present. */
                wanted = FALSE;
            }
        }
    }
    return wanted;
}