1
- /* $Id: cfg.c,v 1.40 2004/06/20 10: 09:52 reinelt Exp $^
1
+ /* $Id: cfg.c,v 1.41 2004/06/26 09:27:20 reinelt Exp $^
2
2
*
3
3
* config file stuff
4
4
*
23
23
*
24
24
*
25
25
* $Log: cfg.c,v $
26
+ * Revision 1.41 2004/06/26 09:27:20 reinelt
27
+ *
28
+ * added '-W' to CFLAGS
29
+ * changed all C++ comments to C ones ('//' => '/* */ ')
30
+ * cleaned up a lot of signed/unsigned mistakes
31
+ *
26
32
* Revision 1.40 2004 /06 /20 10 :09 :52 reinelt
27
33
*
28
34
* 'const' ified the whole source
@@ -282,7 +288,7 @@ static ENTRY *Config=NULL;
282
288
static int nConfig = 0 ;
283
289
284
290
285
- // bsearch compare function for config entries
291
+ /* bsearch compare function for config entries */
286
292
static int c_lookup (const void * a , const void * b )
287
293
{
288
294
char * key = (char * )a ;
@@ -292,7 +298,7 @@ static int c_lookup (const void *a, const void *b)
292
298
}
293
299
294
300
295
- // qsort compare function for variables
301
+ /* qsort compare function for variables */
296
302
static int c_sort (const void * a , const void * b )
297
303
{
298
304
ENTRY * ea = (ENTRY * )a ;
@@ -302,7 +308,7 @@ static int c_sort (const void *a, const void *b)
302
308
}
303
309
304
310
305
- // remove leading and trailing whitespace
311
+ /* remove leading and trailing whitespace */
306
312
static char * strip (char * s , const int strip_comments )
307
313
{
308
314
char * p ;
@@ -321,7 +327,7 @@ static char *strip (char *s, const int strip_comments)
321
327
}
322
328
323
329
324
- // unquote a string
330
+ /* unquote a string */
325
331
static char * dequote (char * string )
326
332
{
327
333
int quote = 0 ;
@@ -358,16 +364,16 @@ static char *dequote (char *string)
358
364
}
359
365
360
366
361
- // which if a string contains only valid chars
362
- // i.e. start with a char and contains chars and nums
367
+ /* which if a string contains only valid chars */
368
+ /* i.e. start with a char and contains chars and nums */
363
369
static int validchars (const char * string )
364
370
{
365
371
const char * c ;
366
372
367
373
for (c = string ; * c ; c ++ ) {
368
- // first and following chars
374
+ /* first and following chars */
369
375
if ((* c >='A' && * c <='Z' ) || (* c >='a' && * c <='z' ) || (* c == '_' )) continue ;
370
- // only following chars
376
+ /* only following chars */
371
377
if ((c > string ) && ((* c >='0' && * c <='9' ) || (* c == '.' ) || (* c == '-' ))) continue ;
372
378
return 0 ;
373
379
}
@@ -380,18 +386,18 @@ static void cfg_add (const char *section, const char *key, const char *val, cons
380
386
char * buffer ;
381
387
ENTRY * entry ;
382
388
383
- // allocate buffer
389
+ /* allocate buffer */
384
390
buffer = malloc (strlen (section )+ strlen (key )+ 2 );
385
391
* buffer = '\0' ;
386
392
387
- // prepare section.key
393
+ /* prepare section.key */
388
394
if (section != NULL && * section != '\0' ) {
389
395
strcpy (buffer , section );
390
396
strcat (buffer , "." );
391
397
}
392
398
strcat (buffer , key );
393
399
394
- // does the key already exist?
400
+ /* does the key already exist? */
395
401
entry = bsearch (buffer , Config , nConfig , sizeof (ENTRY ), c_lookup );
396
402
397
403
if (entry != NULL ) {
@@ -439,19 +445,19 @@ char *cfg_list (const char *section)
439
445
int i , len ;
440
446
char * key , * list ;
441
447
442
- // calculate key length
448
+ /* calculate key length */
443
449
len = strlen (section )+ 1 ;
444
450
445
- // prepare search key
451
+ /* prepare search key */
446
452
key = malloc (len + 1 );
447
453
strcpy (key , section );
448
454
strcat (key , "." );
449
455
450
- // start with empty string
456
+ /* start with empty string */
451
457
list = malloc (1 );
452
458
* list = '\0' ;
453
459
454
- // search matching entries
460
+ /* search matching entries */
455
461
for (i = 0 ; i < nConfig ; i ++ ) {
456
462
if (strncasecmp (Config [i ].key , key , len )== 0 ) {
457
463
list = realloc (list , strlen (list )+ strlen (Config [i ].key )- len + 2 );
@@ -471,26 +477,26 @@ static char *cfg_lookup (const char *section, const char *key)
471
477
char * buffer ;
472
478
ENTRY * entry ;
473
479
474
- // calculate key length
480
+ /* calculate key length */
475
481
len = strlen (key )+ 1 ;
476
482
if (section != NULL )
477
483
len += strlen (section )+ 1 ;
478
484
479
- // allocate buffer
485
+ /* allocate buffer */
480
486
buffer = malloc (len );
481
487
* buffer = '\0' ;
482
488
483
- // prepare section:key
489
+ /* prepare section:key */
484
490
if (section != NULL && * section != '\0' ) {
485
491
strcpy (buffer , section );
486
492
strcat (buffer , "." );
487
493
}
488
494
strcat (buffer , key );
489
495
490
- // search entry
496
+ /* search entry */
491
497
entry = bsearch (buffer , Config , nConfig , sizeof (ENTRY ), c_lookup );
492
498
493
- // free buffer again
499
+ /* free buffer again */
494
500
free (buffer );
495
501
496
502
if (entry != NULL )
@@ -540,9 +546,9 @@ int cfg_number (const char *section, const char *key, const int defval, const in
540
546
void * tree = NULL ;
541
547
RESULT result = {0 , 0 , 0 , NULL };
542
548
543
- // start with default value
544
- // in case of an (uncatched) error, you have the
545
- // default value set, which may be handy...
549
+ /* start with default value */
550
+ /* in case of an (uncatched) error, you have the */
551
+ /* default value set, which may be handy... */
546
552
* value = defval ;
547
553
548
554
expression = cfg_get_raw (section , key , NULL );
@@ -635,84 +641,84 @@ static int cfg_read (const char *file)
635
641
return -1 ;
636
642
}
637
643
638
- // start with empty section
644
+ /* start with empty section */
639
645
strcpy (section , "" );
640
646
641
647
error = 0 ;
642
648
lineno = 0 ;
643
649
while ((line = fgets (buffer ,256 ,stream ))!= NULL ) {
644
650
645
- // increment line number
651
+ /* increment line number */
646
652
lineno ++ ;
647
653
648
- // skip empty lines
654
+ /* skip empty lines */
649
655
if (* (line = strip (line , 1 ))== '\0' ) continue ;
650
656
651
- // reset section flags
657
+ /* reset section flags */
652
658
section_open = 0 ;
653
659
section_close = 0 ;
654
660
655
- // key is first word
661
+ /* key is first word */
656
662
key = line ;
657
663
658
- // search first blank between key and value
664
+ /* search first blank between key and value */
659
665
for (val = line ; * val ; val ++ ) {
660
666
if (isblank (* val )) {
661
667
* val ++ = '\0' ;
662
668
break ;
663
669
}
664
670
}
665
671
666
- // strip value
672
+ /* strip value */
667
673
val = strip (val , 1 );
668
674
669
- // search end of value
675
+ /* search end of value */
670
676
if (* val ) for (end = val ; * (end + 1 ); end ++ );
671
677
else end = val ;
672
678
673
- // if last char is '{', a section has been opened
679
+ /* if last char is '{', a section has been opened */
674
680
if (* end == '{' ) {
675
681
section_open = 1 ;
676
682
* end = '\0' ;
677
683
val = strip (val , 0 );
678
684
}
679
685
680
- // provess "value" in double-quotes
686
+ /* provess "value" in double-quotes */
681
687
if (* val == '"' && * end == '"' ) {
682
688
* end = '\0' ;
683
689
val ++ ;
684
690
}
685
691
686
- // if key is '}', a section has been closed
692
+ /* if key is '}', a section has been closed */
687
693
if (strcmp (key , "}" )== 0 ) {
688
694
section_close = 1 ;
689
695
* key = '\0' ;
690
696
}
691
697
692
- // sanity check: '}' should be the only char in a line
698
+ /* sanity check: '}' should be the only char in a line */
693
699
if (section_close && (section_open || * val != '\0' )) {
694
700
error ("error in config file '%s' line %d: garbage after '}'" , file , lineno );
695
701
error = 1 ;
696
702
break ;
697
703
}
698
704
699
- // check key for valid chars
705
+ /* check key for valid chars */
700
706
if (!validchars (key )) {
701
707
error ("error in config file '%s' line %d: key '%s' is invalid" , file , lineno , key );
702
708
error = 1 ;
703
709
break ;
704
710
}
705
711
706
- // on section-open, check value for valid chars
712
+ /* on section-open, check value for valid chars */
707
713
if (section_open && !validchars (val )) {
708
714
error ("error in config file '%s' line %d: section '%s' is invalid" , file , lineno , val );
709
715
error = 1 ;
710
716
break ;
711
717
}
712
718
713
- // on section-open, append new section name
719
+ /* on section-open, append new section name */
714
720
if (section_open ) {
715
- // is the section[] array big enough?
721
+ /* is the section[] array big enough? */
716
722
if (strlen (section )+ strlen (key )+ 3 > sizeof (section )) {
717
723
error ("error in config file '%s' line %d: section buffer overflow" , file , lineno );
718
724
error = 1 ;
@@ -727,9 +733,9 @@ static int cfg_read (const char *file)
727
733
continue ;
728
734
}
729
735
730
- // on section-close, remove last section name
736
+ /* on section-close, remove last section name */
731
737
if (section_close ) {
732
- // sanity check: section already empty?
738
+ /* sanity check: section already empty? */
733
739
if (* section == '\0' ) {
734
740
error ("error in config file '%s' line %d: unmatched closing brace" , file , lineno );
735
741
error = 1 ;
@@ -744,12 +750,12 @@ static int cfg_read (const char *file)
744
750
continue ;
745
751
}
746
752
747
- // finally: add key
753
+ /* finally: add key */
748
754
cfg_add (section , key , val , 0 );
749
755
750
756
}
751
757
752
- // sanity check: are the braces balanced?
758
+ /* sanity check: are the braces balanced? */
753
759
if (!error && * section != '\0' ) {
754
760
error ("error in config file '%s' line %d: unbalanced braces" , file , lineno );
755
761
error = 1 ;
0 commit comments