Codebase list lbreakout2 / HEAD common / parser.c
HEAD

Tree @HEAD (Download .tar.gz)

parser.c @HEADraw · history · blame

  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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
/***************************************************************************
                          parser.c  -  description
                             -------------------
    begin                : Sat Mar 9 2002
    copyright            : (C) 2001 by Michael Speck
    email                : kulkanie@gmx.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <stdlib.h>
#include <string.h>
#include "parser.h"
#include "tools.h"

/*
====================================================================
Error string.
====================================================================
*/
static char parser_sub_error[1024];
static char parser_error[1024];

/*
====================================================================
This buffer is used to fully load resource files when the 
compact format is used.
====================================================================
*/
enum { CBUFFER_SIZE = 131072 }; /* 128 KB */
static char cbuffer[CBUFFER_SIZE];
static char* cbuffer_pos = 0; /* position in cbuffer */

/*
====================================================================
As we need constant strings sometimes we have to define a maximum
length for tokens.
====================================================================
*/
enum { PARSER_MAX_TOKEN_LENGTH = 1024 };

/*
====================================================================
Locals
====================================================================
*/

/*
====================================================================
Macro to shorten the fread call for a single character.
====================================================================
*/
#define FILE_READCHAR( file, c ) fread( &c, sizeof( char ), 1, file )

/*
====================================================================
Find next newline in cbuffer and replace it with \0 and return the 
pointer to the current line.
====================================================================
*/
static char* parser_get_next_line()
{
    char *line = cbuffer_pos;
    char *newpos;
    if ( cbuffer_pos[0] == 0 )
        return 0; /* completely read. no more lines. */
    if ( ( newpos = strchr( cbuffer_pos, 10 ) ) == 0 )
        cbuffer_pos += strlen( cbuffer_pos ); /* last line */
    else {
        cbuffer_pos = newpos + 1; /* set pointer to next line */
        newpos[0] = 0; /* terminate current line */
    }
    return line;
}

/*
====================================================================
Set parse error string: "file:line: error"
====================================================================
*/
static void parser_set_parse_error( char *fname, FILE *file, char *error )
{
    int end, pos;
    int line_count = 1;
    char c;
    end = ftell( file ); pos = 0;
    fseek( file, 0, SEEK_SET );
    while ( pos < end ) {
        FILE_READCHAR( file, c ); pos++;
        if ( c == 10 ) line_count++;
    }
    sprintf( parser_error, "%s: %i: %s", 
             fname, line_count, error ); 
}

/*
====================================================================
Check if the given character occurs in the symbol list.
If the first symbol is ' ' it is used as wildcard for all
white-spaces.
====================================================================
*/
static int is_symbol( int c, char *symbols )
{
    int i = 0;
    if ( symbols[0] == ' ' && c <= 32 ) return 1;
    while ( symbols[i] != 0 )
        if ( c == symbols[i++] ) 
            return 1;
    return 0;
}

/*
====================================================================
Move file position forward until reading in the given character.
If stop is ' ' whitespaces will be ignored.
====================================================================
*/
static void file_skip( FILE *file, char stop )
{
    char c = 0;
    FILE_READCHAR( file, c );
    while ( ( ( stop == ' ' && c <= 32 ) || ( stop != ' ' && c != stop ) ) && !feof( file ) )
        FILE_READCHAR( file, c );
    if ( !feof( file ) )
        fseek( file, -1, SEEK_CUR );
}

/*
====================================================================
Read next token from current file position where symbols is a
list of characters used to break up the tokens. The symbols 
themself are returned as tokens. If ' ' occurs in the symbol list
it will be ignored and whitespaces are removed automatically.
The token does not exceed PARSER_MAX_TOKEN_LENGTH.
Enclosing ".." are kept at the token. Use file_compare_token()
to test it's contents.
Returns False on EoF.
====================================================================
*/
static int file_read_token_intern( FILE *file, char *symbols, char *token )
{
    int pos = 0;
    char c;
    token[0] = 0;
    file_skip( file, ' ' );
    FILE_READCHAR( file, c );
    if ( feof( file ) ) { 
        sprintf( parser_sub_error, "unexpected end of file" ); 
        return 0;
    }
    /* string? */
    if ( c == '"' ) {
        token[pos++] = '"';
        FILE_READCHAR( file, c );
        while ( ( !feof( file ) && c != '"' ) ) {
            token[pos++] = c;
            if ( pos == PARSER_MAX_TOKEN_LENGTH - 2 ) {
                token[pos++] = '"';
                token[pos] = 0;
                sprintf( parser_sub_error, "token exceeds limit" );
                return 0;
            }
            FILE_READCHAR( file, c );
        }
        token[pos++] = '"';
        token[pos] = 0;
        if ( feof( file ) ) { 
            sprintf( parser_sub_error, "unexpected end of file" ); 
            token[0] = 0; 
            return 0;
        }
        return 1;
    }
    /* symbol? */
    if ( is_symbol( c, symbols ) ) {
        token[0] = c; token[1] = 0;
        return 1;
    }
    /* other token */
    while ( !is_symbol( c, symbols ) && !feof( file ) ) {
        token[pos++] = c;
        if ( pos == PARSER_MAX_TOKEN_LENGTH - 1 ) {
            token[pos] = 0;
            sprintf( parser_sub_error, "token exceeds limit" );
            return 0;
        }
        FILE_READCHAR( file, c );
    }
    token[pos] = 0;
    if ( feof( file ) ) 
        return 1;
    fseek( file, -1, SEEK_CUR );
    return 1;
}

/*
====================================================================
Skip all tokens until one begins with character 'stop'. This
token is also ignored.
====================================================================
*/
static void file_skip_section( FILE *file, char stop )
{
    char token[PARSER_MAX_TOKEN_LENGTH];
    do {
        file_read_token_intern( file, PARSER_SYMBOLS, token );
    } while ( !feof( file ) && token[0] != stop );
}

/*
====================================================================
Read next token and skip comments enclosed in tokens
skip[0], skip[1] (if skip is not NULL).
Return 0 if EoF.
====================================================================
*/
static int file_read_token( FILE *file, char *symbols, char *skip, char *token )
{
    while ( 1 ) {
        if ( !file_read_token_intern( file, symbols, token ) )
            return 0;
        if ( skip ) {
            if ( token[0] == skip[0] )
                file_skip_section( file, skip[1] );
            else
                break;
        }
        else
            break;
    }
    return 1;
}

/*
====================================================================
Remove quotes if any and return result as newly allocated string.
====================================================================
*/
static char* parser_remove_quotes( char *string )
{
    char *new;
    if ( string[0] != '"' ) 
        return strdup( string );
    new = calloc( strlen( string ) - 1, sizeof( char ) );
    strncpy( new, string + 1, strlen( string ) - 2 );
    new[strlen( string ) - 2] = 0;
    return new;
}

/*
====================================================================
Proceed in the given string until it ends or non-whitespace occurs
and return the new position.
====================================================================
*/
static char* string_ignore_whitespace( char *string )
{
    int i = 0;
    while ( string[i] != 0 && string[i] <= 32 ) i++;
    return string + i;
}

/*
====================================================================
This function searches file from the current position for the next
pdata entry.
====================================================================
*/
static PData* parser_parse_file( FILE *file )
{
    char token[PARSER_MAX_TOKEN_LENGTH];
    PData *pd = 0, *sub = 0;
    
    /* get name */
    if ( !file_read_token( file, PARSER_SYMBOLS, PARSER_SKIP_SYMBOLS, token ) )
        return 0;
    if ( is_symbol( token[0], PARSER_SYMBOLS ) ) {
        sprintf( parser_sub_error, "parse error before '%s'", token );
        return 0;
    }
    pd = calloc( 1, sizeof( PData ) );
    pd->name = parser_remove_quotes( token );
    /* check type */
    if ( !file_read_token( file, PARSER_SYMBOLS, PARSER_SKIP_SYMBOLS, token ) )
        goto failure;
    switch ( token[0] ) {
        case PARSER_SET:
            /* assign single value or list */
            pd->values = list_create( LIST_AUTO_DELETE, LIST_NO_CALLBACK );
            if ( !file_read_token( file, PARSER_SYMBOLS, PARSER_SKIP_SYMBOLS, token ) ) 
                goto failure;
            if ( token[0] != PARSER_LIST_BEGIN ) {
                if ( is_symbol( token[0], PARSER_SYMBOLS ) ) {
                    sprintf( parser_sub_error, "parse error before '%s'", token );
                    goto failure;
                }
                else
                    list_add( pd->values, parser_remove_quotes( token ) );
            }
            else {
                if ( !file_read_token( file, PARSER_SYMBOLS, PARSER_SKIP_SYMBOLS, token ) )
                    goto failure;
                while ( token[0] != PARSER_LIST_END ) {
                    if ( is_symbol( token[0], PARSER_SYMBOLS ) ) {
                        sprintf( parser_sub_error, "parse error before '%s'", token );
                        goto failure;
                    }
                    else
                        list_add( pd->values, parser_remove_quotes( token ) );
                    if ( !file_read_token( file, PARSER_SYMBOLS, PARSER_SKIP_SYMBOLS, token ) )
                        goto failure;
                }
            }
            break;
        case PARSER_GROUP_BEGIN:
            /* check all entries until PARSER_GROUP_END */
            pd->entries = list_create( LIST_NO_AUTO_DELETE, LIST_NO_CALLBACK );
            while ( 1 ) {
                if ( !file_read_token( file, PARSER_SYMBOLS, PARSER_SKIP_SYMBOLS, token ) ) 
                    goto failure;
                if ( token[0] == PARSER_GROUP_END )
                    break;
                fseek( file, -strlen( token ), SEEK_CUR );
                sub = parser_parse_file( file );
                if ( sub ) 
                    list_add( pd->entries, sub );
                else
                    goto failure;
            }
            break;
        default:
            sprintf( parser_sub_error, "parse error before '%s'", token );
            goto failure;
    }
    return pd;
failure:
    parser_free( &pd );
    return 0;
}

/*
====================================================================
Publics
====================================================================
*/

/*
====================================================================
This function splits a string into tokens using the characters
found in symbols as breakpoints. If the first symbol is ' ' all
whitespaces are used as breakpoints though NOT added as a token 
(thus removed from string).
====================================================================
*/
List* parser_split_string( char *string, char *symbols )
{
    int pos;
    char *token = 0;
    List *list = list_create( LIST_AUTO_DELETE, LIST_NO_CALLBACK );
    while ( string[0] != 0 ) {
        if ( symbols[0] == ' ' )
            string = string_ignore_whitespace( string ); 
        if ( string[0] == 0 ) break;
        pos = 1; /* 'read in' first character */
        while ( string[pos - 1] != 0 && !is_symbol( string[pos - 1], symbols ) && string[pos - 1] != '"' ) pos++;
        if ( pos > 1 ) 
            pos--;
        else
            if ( string[pos - 1] == '"' ) {
                /* read a string */
                string = string + 1; pos = 0;
                while ( string[pos] != 0 && string[pos] != '"' ) pos++;
                token = calloc( pos + 1, sizeof( char ) );
                strncpy( token, string, pos ); token[pos] = 0;
                list_add( list, token );
                string = string + pos + (string[pos] != 0);
                continue;
            }
        token = calloc( pos + 1, sizeof( char ) );
        strncpy( token, string, pos); token[pos] = 0;
        list_add( list, token );
        string = string + pos;
    }
    return list;
}
/*
====================================================================
This is the light version of parser_split_string which checks for
just one character and does not add this glue characters to the 
list. It's about 2% faster. Wow.
====================================================================
*/
List *parser_explode_string( char *string, char c )
{
    List *list = list_create( LIST_AUTO_DELETE, LIST_NO_CALLBACK );
    char *next_slash = 0;
    char buffer[64];
    while ( string[0] != 0 && ( next_slash = strchr( string, c ) ) != 0 ) {
        if ( next_slash != string ) {
            strcpy_lt( buffer, string, (next_slash-string>63)?63:(next_slash-string) );
            list_add( list, strdup( buffer ) );
        }
        string += next_slash - string + 1;
    }
    if ( string[0] != 0 )
        list_add( list, strdup( string ) );
    return list;
}

/*
====================================================================
This function reads in a whole file and converts it into a
PData tree struct. If an error occurs NULL is returned and 
parser_error is set.
====================================================================
*/
static int parser_read_file_full( FILE *file, PData *top )
{
    PData *sub = 0;
    char token[1024];
    /* parse file */
    while ( !feof( file ) ) {
        if ( ( sub = parser_parse_file( file ) ) != 0 )
            list_add( top->entries, sub );
        else
            return 0; 
        /* skip comments and whitespaces */
        if ( !file_read_token( file, PARSER_SYMBOLS, PARSER_SKIP_SYMBOLS, token ) ) {
            if ( token[0] != 0 )
                return 0;
            break;
        }
        else
            fseek( file, -strlen( token ), SEEK_CUR );
    }
    return 1;
}
static int parser_read_file_compact( PData *section )
{
    /* section is the parent pdata that needs some 
       entries */
    PData *pd = 0;
    char *line, *cur;
    while ( ( line = parser_get_next_line() ) ) {
        switch ( line[0] ) {
            case '>':
                /* this section is finished */
                return 1;
            case '<':
                /* add a whole subsection */
                pd = calloc( 1, sizeof( PData ) );
                pd->name = strdup( line + 1 );
                pd->entries = list_create( LIST_NO_AUTO_DELETE, LIST_NO_CALLBACK );
                parser_read_file_compact( pd );
                /* add to section */
                list_add( section->entries, pd );
                break;
            default:
                /* read values as subsection */
                pd = calloc( 1, sizeof( PData ) );
                /* check name */
                if ( ( cur = strchr( line, '»' ) ) == 0 ) {
                    sprintf( parser_sub_error, "parse error: use '»' for assignment or '<' for section" );
                    return 0;
                }
                cur[0] = 0; cur++;
                pd->name = strdup( line );
                /* get values */
                pd->values = parser_explode_string( cur, '°' );
                /* add to section */
                list_add( section->entries, pd );
                break;
        }
    }
    return 1;
}
PData* parser_read_file( char *tree_name, char *fname )
{
    int size;
    char magic = 0;
    FILE *file = 0;
    PData *top = 0;
    /* open file */
    if ( ( file = fopen( fname, "r" ) ) == 0 ) {
        sprintf( parser_error, "%s: file not found", fname );
        return 0;
    }
    /* create top level pdata */
    top = calloc( 1, sizeof( PData ) );
    top->name = strdup( tree_name );
    top->entries = list_create( LIST_NO_AUTO_DELETE, LIST_NO_CALLBACK );
    /* parse */
    FILE_READCHAR( file, magic );
    if ( magic == '@' ) {
        /* get the whole contents -- 1 and CBUFFER_SIZE are switched */
        fseek( file, 0, SEEK_END ); size = ftell( file ) - 2;
        if ( size >= CBUFFER_SIZE ) {
            fprintf( stderr, "%s: file's too big to fit the compact buffer (128KB)\n", fname );
            size = CBUFFER_SIZE - 1;
        }
        fseek( file, 2, SEEK_SET );
        fread( cbuffer, 1, size, file );
        cbuffer[size] = 0;
        /* set indicator to beginning of text */
        cbuffer_pos = cbuffer;
        /* parse cbuffer */
        if ( !parser_read_file_compact( top ) ) {
            parser_set_parse_error( fname, file, parser_sub_error );
            goto failure;
        }
    }
    else {
        fseek( file, 0, SEEK_SET );
        if ( !parser_read_file_full( file, top ) ) {
            parser_set_parse_error( fname, file, parser_sub_error );
            goto failure;
        }
    }
    /* finalize */
    fclose( file );
    return top;
failure:
    fclose( file );
    parser_free( &top );
    return 0;
}

/*
====================================================================
This function frees a PData tree struct.
====================================================================
*/
void parser_free( PData **pdata )
{
    PData *entry = 0;
    if ( (*pdata) == 0 ) return;
    if ( (*pdata)->name ) free( (*pdata)->name );
    if ( (*pdata)->values ) list_delete( (*pdata)->values );
    if ( (*pdata)->entries ) {
        list_reset( (*pdata)->entries );
        while ( ( entry = list_next( (*pdata)->entries ) ) )
            parser_free( &entry );
        list_delete( (*pdata)->entries );
    }
    free( *pdata ); *pdata = 0;
}

/*
====================================================================
Functions to access a PData tree. 
'name' is the pass within tree 'pd' where subtrees are separated 
by '/' (e.g.: name = 'config/graphics/animations')
parser_get_pdata   : get pdata entry associated with 'name'
parser_get_entries : get list of subtrees (PData structs) in 'name'
parser_get_values  : get value list of 'name'
parser_get_value   : get a single value from value list of 'name'
parser_get_int     : get first value of 'name' converted to integer
parser_get_double  : get first value of 'name' converted to double
parser_get_string  : get first value of 'name' _duplicated_
If an error occurs result is set NULL, False is returned and
parse_error is set.
====================================================================
*/
int parser_get_pdata  ( PData *pd, char *name, PData  **result )
{
    int i, found;
    PData *pd_next = pd;
    PData *entry = 0;
    char *sub = 0;
    List *path = parser_explode_string( name, '/' );
    for ( i = 0, list_reset( path ); i < path->count; i++ ) {
        sub = list_next( path );
        if ( !pd_next->entries ) {
            sprintf( parser_sub_error, "%s: no subtrees", pd_next->name );
            goto failure;
        }
        list_reset( pd_next->entries ); found = 0;
        while ( ( entry = list_next( pd_next->entries ) ) )
            if ( strlen( entry->name ) == strlen( sub ) && !strncmp( entry->name, sub, strlen( sub ) ) ) {
                pd_next = entry;
                found = 1;
                break;
            }
        if ( !found ) {
            sprintf( parser_sub_error, "%s: subtree '%s' not found", pd_next->name, sub );
            goto failure;
        }
    }
    list_delete( path );
    *result = pd_next;
    return 1;
failure:
    sprintf( parser_error, "parser_get_pdata: %s/%s: %s", pd->name, name, parser_sub_error );
    list_delete( path );
    *result = 0;
    return 0;
}
int parser_get_entries( PData *pd, char *name, List   **result )
{
    PData *entry;
    *result = 0;
    if ( !parser_get_pdata( pd, name, &entry ) ) {
        sprintf( parser_sub_error, "parser_get_entries:\n %s", parser_error );
        strcpy( parser_error, parser_sub_error );
        return 0;
    }
    if ( !entry->entries || entry->entries->count == 0 ) {
        sprintf( parser_error, "parser_get_entries: %s/%s: no subtrees", pd->name, name );
        return 0;
    }
    *result = entry->entries;
    return 1;
}
int parser_get_values ( PData *pd, char *name, List   **result )
{
    PData *entry;
    *result = 0;
    if ( !parser_get_pdata( pd, name, &entry ) ) {
        sprintf( parser_sub_error, "parser_get_values:\n %s", parser_error );
        strcpy( parser_error, parser_sub_error );
        return 0;
    }
    if ( !entry->values || entry->values->count == 0 ) {
        sprintf( parser_error, "parser_get_values: %s/%s: no values", pd->name, name );
        return 0;
    }
    *result = entry->values;
    return 1;
}
int parser_get_value  ( PData *pd, char *name, char   **result, int index )
{
    List *values;
    if ( !parser_get_values( pd, name, &values ) ) {
        sprintf( parser_sub_error, "parser_get_value:\n %s", parser_error );
        strcpy( parser_error, parser_sub_error );
        return 0;
    }
    if ( index >= values->count ) {
        sprintf( parser_error, "parser_get_value: %s/%s: index %i out of range (%i elements)", 
                 pd->name, name, index, values->count );
        return 0;
    }
    *result = list_get( values, index );
    return 1;
}
int parser_get_int    ( PData *pd, char *name, int     *result )
{
    char *value;
    if ( !parser_get_value( pd, name, &value, 0 ) ) {
        sprintf( parser_sub_error, "parser_get_int:\n %s", parser_error );
        strcpy( parser_error, parser_sub_error );
        return 0;
    }
    *result = atoi( value );
    return 1;
}
int parser_get_double ( PData *pd, char *name, double  *result )
{
    char *value;
    if ( !parser_get_value( pd, name, &value, 0 ) ) {
        sprintf( parser_sub_error, "parser_get_double:\n %s", parser_error );
        strcpy( parser_error, parser_sub_error );
        return 0;
    }
    *result = strtod( value, 0 );
    return 1;
}
int parser_get_string ( PData *pd, char *name, char   **result )
{
    char *value;
    if ( !parser_get_value( pd, name, &value, 0 ) ) {
        sprintf( parser_sub_error, "parser_get_string:\n %s", parser_error );
        strcpy( parser_error, parser_sub_error );
        return 0;
    }
    *result = strdup( value );
    return 1;
}

/*
====================================================================
If an error occurred you can query the reason with this function.
====================================================================
*/
char* parser_get_error( void )
{
    return parser_error;
}