-
Notifications
You must be signed in to change notification settings - Fork 0
/
core-rc.c
2490 lines (2211 loc) · 52.8 KB
/
core-rc.c
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
///
/// @file core-rc.c @brief core runtime configuration functions
///
/// Copyright (c) 2009 - 2011, 2021 by Lutz Sammer. All Rights Reserved.
///
/// Contributor(s):
///
/// License: AGPLv3
///
/// This program is free software: you can redistribute it and/or modify
/// it under the terms of the GNU Affero General Public License as
/// published by the Free Software Foundation, either version 3 of the
/// License.
///
/// 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 Affero General Public License for more details.
///
/// $Id$
//////////////////////////////////////////////////////////////////////////////
///
/// @mainpage
///
/// core-rc is a simple module for processing structured and readables
/// configuration files.
/// Complete module < 20k (+5k core-array) with ~ 2000 source code lines.
///
/// @par Example
/// Example how a configuration file can look like:
/// @code
/// dia-show = [
/// fullscreen = true ; start with fullscreen
/// delay = 30 ; slideshow delay in s
/// ]
/// ; or if you prefer this style
/// dia-show.fullscreen = true
/// dia-show.delay = 30
/// @endcode
///
/// @par Configuration
///
/// - #USE_CORE_RC_GET_STRINGS
/// Include support for the get functions with strings.
///
/// - #USE_CORE_RC_PRINT
/// Include support to print config objects.
///
/// - #USE_CORE_RC_WRITE
/// Include support to write config objects.
///
/// @ref CoreRc The core runtime configuration module.
///
///
/// @defgroup CoreRc The core runtime configuration module.
///
/// This module contains the runtime configuration and configuration file
/// parsing functions.
/// The syntax of the config file is the CoreScript array constructor.
///
///
/// @todo cleanup module
/// @todo rewrite handling of global array
/// @todo write examples
/// @todo write man pages
///
/// @section examples
///
/// A simple example (incomplete) how to use core-rc:
/// @code
/// #include "core-array/core-array.h"
/// #include "core-rc/core-rc.h"
///
/// ...
///
/// Config * config;
/// config = ConfigReadFile2(NULL, "my.core-rc");
/// @endcode
/// Load runtime configuration from "my.core-rc".
/// @code
/// ConfigFreeMem(config);
/// @endcode
/// Free memory used by configuration. "config" can't be used after
/// this point.
///
/// @section internals
///
/// The core-rc object pointers are tagged pointers.
/// <tt>
/// @n..3.........2.........1.........0
/// @n[10987654321098765432109876543210]
/// @n[xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx1] x = 31 bit signed integer
/// @n[xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx10] x = 30 bit floating point
/// @n[xxxxxxxxxxxxxxxxxxxxxxxxxxxxx100] x = 32 bit string pointer
/// @n[xxxxxxxxxxxxxxxxxxxxxxxxxxxxx000] x = 32 bit object pointer
/// </tt>
///
/// @{
#define _GNU_SOURCE 1 ///< fix stpcpy strchrnul
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#ifndef NO_DEBUG_CORE_RC
#define NO_DEBUG_CORE_RC ///< debug enabled/disabled
#endif
#ifdef CORE_RC_TEST
#define USE_CORE_RC_PRINT ///< include core-rc print support
#define USE_CORE_RC_WRITE ///< include core-rc write support
#define USE_CORE_RC_GET_STRINGS ///< include get functions with strings
#endif
#include "core-array/core-array.h"
#include "core-rc.h"
// ----------------------------------------------------------------------------
// Object pool
// ------------------------------------------------------------------------ //
/**
** Allocate new object node.
**
** @returns random object 8 bytes aligned
**
** @todo FIXME: need to write an object pool.
*/
static inline ConfigObject *ConfigObjectNew(void)
{
ConfigObject *object;
// object is 4 bytes on 32bit 8 bytes on 64bit.
object = malloc(sizeof(*object));
#ifdef DEBUG_CORE_RC
if ((size_t)object & 7) {
fprintf(stderr, "Object: unaligned %p\n", object);
abort();
}
#endif
return object;
}
/**
** Deallocate object node.
**
** @param object config pointer object
**
** @todo FIXME: need to write an object pool.
*/
static inline void ConfigObjectDel(ConfigObject * object)
{
free(object);
}
// ------------------------------------------------------------------------ //
// String pool
// ------------------------------------------------------------------------ //
///
/// @defgroup stringpool The string-pool module.
///
/// This module handles string-pools. String pools handles string
/// memory managment. Only one copy of equal strings is stored.
///
/// @{
/// end of various program segments
///@{
#ifdef TCC
extern char etext, edata, end;
#define ETEXT etext
#define EDATA edata
#define END end
#else
extern char _etext, _edata, _end;
#define ETEXT _etext
#define EDATA _edata
#define END _end
#endif
///@}
/// bigger strings are malloced
static const size_t STRING_POOL_MAX_SIZE = 4096;
/// pool size
static const size_t STRING_POOL_SIZE = 8192;
/**
** String-pool typedef.
*/
typedef struct _string_pool_ StringPool;
/**
** String-pool node typedef
*/
typedef struct _string_node_ StringNode;
/**
** A node of string pool structure.
*/
struct _string_node_
{
StringNode *Next; ///< next node
uint32_t Size; ///< allocated size of node
uint32_t Free; ///< free bytes in node
char Data[1]; ///< string memory
};
/**
** String-pool variables structure.
*/
struct _string_pool_
{
StringNode *Pools; ///< list of pools
Array *Strings; ///< lookup of strings
};
/**
** Generate key for string.
**
** @param len length of key string
** @param str string for key
**
** @returns a 32bit/64bit key for first part of string.
*/
static inline size_t StringPoolKeygen(int len, const uint8_t * str)
{
size_t key;
key = 0;
// 8/4 bytes -> index-key
switch (len) {
default:
#if SIZE_MAX == (18446744073709551615UL)
// 64 bit version
case 8:
key |= str[7] << 0;
// fallthrough
case 7:
key |= str[6] << 8;
// fallthrough
case 6:
key |= str[5] << 16;
// fallthrough
case 5:
key |= str[4] << 24;
// fallthrough
case 4:
key |= (size_t)str[3] << 32;
// fallthrough
case 3:
key |= (size_t)str[2] << 40;
// fallthrough
case 2:
key |= (size_t)str[1] << 48;
// fallthrough
case 1:
key |= (size_t)str[0] << 56;
break;
#else
// 32 bit version
case 4:
key |= str[3] << 0;
// fallthrough
case 3:
key |= str[2] << 8;
// fallthrough
case 2:
key |= str[1] << 16;
// fallthrough
case 1:
key |= str[0] << 24;
break;
#endif
case 0:
break;
}
return key;
}
/**
** Allocate string from pool.
**
** @param pool string-pool to use
** @param string string to be copied into pool
**
** @returns pointer to unique string
*/
static const char *StringPoolAlloc(StringPool * pool, const char *string)
{
unsigned len;
char *dst;
StringNode *node;
if (string < &EDATA) { // string from C constant area
#ifdef DEBUG_CORE_RC
fprintf(stderr, "EDATA %s\n", string);
#endif
return string;
}
len = strlen(string) + 1;
if (len >= STRING_POOL_MAX_SIZE) { // too big, just allocate
return strdup(string);
}
// find pool with empty slot
for (node = pool->Pools; node; node = node->Next) {
if (node->Free >= len) {
goto out;
}
}
node = malloc(STRING_POOL_SIZE); // page aligned nodes
node->Next = pool->Pools;
pool->Pools = node;
node->Free = node->Size = STRING_POOL_SIZE - sizeof(*node) + sizeof(char);
out:
dst = node->Data + node->Size - node->Free;
memcpy(dst, string, len);
node->Free -= len;
return dst;
}
#ifdef never_DEBUG_CORE_RC
/**
** Helper for string pool dump.
**
** @param array array of strings in pool
** @param level indent level
*/
static void StringPoolDump0(const Array * array, int level)
{
size_t index;
size_t *value;
index = 0;
value = ArrayFirst(array, &index);
while (value) {
printf("%*s%0*zx ", level, "", (int)sizeof(size_t) * 2, index);
if (*value & 4) { // strings are tagged
const char *string;
ConfigObject *object;
object = (ConfigObject *) * value;
string = ((ConfigObject *) ((size_t)object & ~7))->Pointer;
printf("= '%s'\n", string);
} else {
printf("->\n");
StringPoolDump0((Array *) * value, level + 8);
}
value = ArrayNext(array, &index);
}
}
/**
** Dump string pool.
**
** @param pool string pool variables
** @param level indent level
*/
static void StringPoolDump(const StringPool * pool, int level)
{
StringNode *node;
StringPoolDump0(pool->Strings, level);
for (node = pool->Pools; node; node = node->Next) {
printf("%p[%d/%d]", node, node->Free, node->Size);
}
if (pool->Pools) {
printf("\n");
}
}
#endif
/**
** Create a new empty string-pool.
**
** @returns new empty string pool.
*/
static inline StringPool *StringPoolNew(void)
{
return calloc(1, sizeof(StringPool));
}
/**
** Free memory used by lookup array.
**
** @param array string pool lookup array
*/
static void StringPoolDelStrings(Array * array)
{
size_t index;
size_t *value;
index = 0;
value = ArrayFirst(array, &index);
while (value) {
size_t temp;
temp = *value;
if (temp & 4) { // strings are tagged
char *s;
size_t n;
s = ((ConfigObject *) (temp & ~7))->Pointer;
n = strlen(s) + 1;
if (n >= STRING_POOL_MAX_SIZE) { // too big, must free
free(s);
}
ConfigObjectDel((ConfigObject *) (temp & ~7));
} else {
StringPoolDelStrings((Array *) temp);
}
value = ArrayNext(array, &index);
}
ArrayFree(array);
}
/**
** Delete a string-pool.
**
** All memory used by string-pool is freed.
**
** @param pool string-pool to be freed.
*/
static void StringPoolDel(StringPool * pool)
{
StringNode *node;
StringPoolDelStrings(pool->Strings);
while ((node = pool->Pools)) { // free all nodes
pool->Pools = node->Next;
free(node);
}
free(pool);
}
/**
** Intern string.
**
** @param pool pool to add string
** @param string string to add
*/
static ConfigObject *StringPoolIntern(StringPool * pool, const char *string)
{
int len;
size_t key;
Array **parent;
size_t *val;
const char *str;
ConfigObject *object;
#if defined(DEBUG_CORE_RC) || defined(DEBUG)
object = NULL;
#endif
str = string;
len = strlen(str);
parent = &pool->Strings;
do {
key = StringPoolKeygen(len, (uint8_t *) str);
val = ArrayIns(parent, key, 0);
if (!*val) { // new key insert string and ready
string = StringPoolAlloc(pool, string);
object = ConfigObjectNew();
object->Pointer = (char *)string;
*val = (size_t)object | 4;
object = (ConfigObject *) * val;
break;
}
str += sizeof(size_t);
len -= sizeof(size_t);
// *val is string or array
if (*val & 4) { // |4 are string objects
const char *old;
int i;
object = (ConfigObject *) * val;
old = ((ConfigObject *) ((size_t)object & ~7))->Pointer;
i = strlen(old) - (str - string);
// compare string, if not same
if (i == len && (len < 0 || !strcmp(str, old + (str - string)))) {
break;
}
// take next 4/8 bytes as next key
key = StringPoolKeygen(i, (uint8_t *) old + (str - string));
*val = (size_t)ArrayNew();
ArrayIns((Array **) val, key, (size_t)object);
}
parent = (Array **) val;
} while (len >= 0);
return object;
}
/// @}
// ----------------------------------------------------------------------------
static StringPool *ConfigStrings; ///< storage of parser strings
/**
** Check if object is fixed integer.
**
** @param object tagged object pointer
**
** @returns true if object is fixed integer object, false otherwise.
*/
static inline int ConfigIsFixed(const ConfigObject * object)
{
return (size_t)object & 1;
}
/**
** Check if object is floating-point number.
**
** @param object tagged object pointer
*/
static inline int ConfigIsFloat(const ConfigObject * object)
{
return !ConfigIsFixed(object) && (size_t)object & 2;
}
#if 0
/**
** Check if object is a pointer object.
**
** @param object tagged object pointer
*/
static inline int ConfigIsPointer(const ConfigObject * object)
{
return object && !((size_t)object & 7);
}
#endif
/**
** Check if object is a word object.
**
** @param object tagged object pointer
*/
static inline int ConfigIsWord(const ConfigObject * object)
{
return (((size_t)object & 7) == 4);
}
/**
** Check if object is an array object.
**
** @param object tagged object pointer
*/
static inline int ConfigIsArray(const ConfigObject * object)
{
return object && !((size_t)object & 7);
}
#if 0
/**
** Create a new fixed integer object.
**
** @param integer 31/63 bit used unsigned integer
**
** @returns tagged fixed integer object pointer.
*/
static inline ConfigObject *ConfigNewInteger(size_t integer)
{
return (ConfigObject *) ((integer << 1) + 1);
}
#endif
/**
** Create a new floating-point number object.
**
** @param number 30/62 bit used floating-point number
**
** @returns tagged floating point object pointer.
*/
inline ConfigObject *ConfigNewDouble(double number)
{
union
{
#if SIZE_MAX == (18446744073709551615UL)
double f;
#else
float f;
#endif
size_t i;
} c;
c.f = number;
return (ConfigObject *) ((c.i & ~3) | 2);
}
/**
** Create a new array object.
**
** @param array core array converted into array object
**
** @returns tagged array object pointer.
**
** @todo should use an object pool
*/
inline ConfigObject *ConfigNewArray(const Array * array)
{
ConfigObject *object;
object = ConfigObjectNew();
object->Pointer = (void *)array;
return object;
}
/**
** Create a new config object.
**
** @param array core array converted into config object
**
** @returns tagged array object pointer.
*/
inline Config *ConfigNewConfig(const Array * array)
{
ConfigObject *object;
if (!ConfigStrings) {
ConfigStrings = StringPoolNew();
#ifdef DEBUG_CORE_RC
fprintf(stderr, "new string pool\n");
#endif
}
object = ConfigObjectNew();
object->Pointer = (void *)array;
return (Config *) object;
}
/**
** Create a new string object.
**
** @param string string converted into fixed string object
**
** @returns tagged fixed string object pointer.
**
** @todo should use an object pool
** @todo second array string -> object unnecessary
*/
inline ConfigObject *ConfigNewString(const char *string)
{
if (!string) {
fprintf(stderr, "core-rc: null string\n");
string = "";
}
return StringPoolIntern(ConfigStrings, string);
}
/**
** Convert (unchecked) fixed object to C integer.
**
** @param object tagged object pointer
**
** @returns fixed integer part of object pointer.
*/
static inline ssize_t ConfigInteger(const ConfigObject * object)
{
return (ssize_t) object >> 1;
}
/**
** Convert (unchecked) fixed object to C unsigned integer.
**
** @param object tagged object pointer
**
** @returns fixed unsigned integer part of object pointer.
*/
static inline size_t ConfigUnsigned(const ConfigObject * object)
{
return (size_t)object >> 1;
}
/**
** Convert (unchecked) float object to C double.
*
** @param object tagged object pointer
**
** @returns floating point part of object pointer.
*/
static inline double ConfigDouble(const ConfigObject * object)
{
union
{
#if SIZE_MAX == (18446744073709551615UL)
double f;
#else
float f;
#endif
size_t i;
} c;
c.i = (size_t)object & ~2;
return c.f;
}
/**
** Convert (unchecked) word object to C string.
**
** @param object tagged object pointer
**
** @returns pointer to fixed word string, stored in object.
*/
static inline const char *ConfigString(const ConfigObject * object)
{
object = (const ConfigObject *)((size_t)object & ~7);
return object->Pointer;
}
/**
** Convert (unchecked) array object to C array.
**
** @param object tagged object pointer
*
** @returns pointer to array, stored in object.
*/
static inline Array *ConfigArray(const ConfigObject * object)
{
return object->Pointer;
}
/**
** Check if value is a fixed integer object.
**
** @param object tagged object pointer
** @param[out] result fixed integer object is stored in result
**
** @returns true if object is fixed integer object, false otherwise.
*/
int ConfigCheckInteger(const ConfigObject * object, ssize_t * result)
{
if (ConfigIsFixed(object)) {
*result = ConfigInteger(object);
return 1;
}
return 0;
}
/**
** Check if value is an unsigned integer object.
**
** @param object tagged object pointer
** @param[out] result unsigned integer object is stored in result
**
** @returns true if object is unsigned integer object, false otherwise.
*/
int ConfigCheckUnsigned(const ConfigObject * object, size_t *result)
{
if (ConfigIsFixed(object)) {
*result = ConfigUnsigned(object);
return 1;
}
return 0;
}
/**
** Check if value is a floating point object.
**
** @param object tagged object pointer
** @param[out] result floating point object is stored in result
**
** @returns true if object is floating point object, false otherwise.
*/
int ConfigCheckDouble(const ConfigObject * object, double *result)
{
if (ConfigIsFloat(object)) {
*result = ConfigDouble(object);
return 1;
}
return 0;
}
/**
** Check if value is a fixed string object.
**
** @param object tagged object pointer
** @param[out] result fixed string is stored in result
**
** @returns true if object is fixed string object, false otherwise.
*/
int ConfigCheckString(const ConfigObject * object, const char **result)
{
if (ConfigIsWord(object)) {
*result = ConfigString(object);
return 1;
}
return 0;
}
/**
** Check if value is an array object.
**
** @param object tagged object pointer
** @param[out] result array object is stored in result
**
** @returns true if object is an array object, false otherwise.
*/
int ConfigCheckArray(const ConfigObject * object, const ConfigObject ** result)
{
if (ConfigIsArray(object)) {
*result = object;
return 1;
}
return 0;
}
#ifdef USE_CORE_RC_GET_STRINGS
/**
** Lookup config object.
**
** @param config config dictionary or sub array
** @param ap array of strings NULL terminated, to select value
**
** @returns object stored in dictionary at index ap
*/
static const ConfigObject *ConfigStringsLookup(const ConfigObject * config,
va_list ap)
{
const char *name;
// loop over all index keys
while ((name = va_arg(ap, const char *)))
{
if (!ConfigIsArray(config)) {
fprintf(stderr, "array required for index '%s'\n", name);
return NULL;
}
config = (const ConfigObject *)
ArrayGet(ConfigArray(config), (size_t)ConfigNewString(name));
}
return config;
}
/**
** Get config any value object.
**
** @param config config dictionary
** @param[out] result object result
** @param ... list of objects NULL terminated, to select value
**
** @returns true if value found at index in dictionary.
*/
int ConfigStringsGetObject(const ConfigObject * config,
const ConfigObject ** result, ...)
{
va_list ap;
const ConfigObject *value;
va_start(ap, result);
value = ConfigStringsLookup(config, ap);
va_end(ap);
if (value) {
*result = value;
return 1;
}
return 0;
}
/**
** Get config integer object.
**
** @param config config dictionary
** @param[out] result signed integer result
** @param ... list of objects NULL terminated, to select value
**
** @returns true if value found at index in dictionary.
*/
int ConfigStringsGetInteger(const ConfigObject * config, ssize_t * result, ...)
{
va_list ap;
const ConfigObject *value;
va_start(ap, result);
value = ConfigStringsLookup(config, ap);
va_end(ap);
if (ConfigIsFixed(value)) {
*result = ConfigInteger(value);
return 1;
}
if (value) {
fprintf(stderr, "value isn't a fixed integer\n");
}
return 0;
}
/**
** Get config unsigned object.
**
** @param config config dictionary
** @param[out] result unsigned integer result
** @param ... list of objects NULL terminated, to select value
**
** @returns true if value found at index in dictionary.
*/
int ConfigStringsGetUnsigned(const ConfigObject * config, size_t *result, ...)
{
va_list ap;
const ConfigObject *value;
va_start(ap, result);
value = ConfigStringsLookup(config, ap);
va_end(ap);
if (ConfigIsFixed(value)) {
*result = ConfigInteger(value);
return 1;
}
if (value) {
fprintf(stderr, "value isn't a fixed unsigned\n");
}
return 0;
}
/**
** Get config boolean object.
**
** @param config config dictionary
** @param ... list of objects NULL terminated, to select value
**
** @retval -1 if index didn't exists in dictionary.
** @retval true if value is not false.
** @retval false if value is 'false' or 0.
*/
int ConfigStringsGetBoolean(const ConfigObject * config, ...)
{
va_list ap;
const ConfigObject *value;
va_start(ap, config);
value = ConfigStringsLookup(config, ap);
va_end(ap);
if (ConfigIsFixed(value)) {
return ConfigInteger(value);
}
if (value) {
fprintf(stderr, "value isn't a fixed integer\n");
}
return -1;
}
/**
** Get config double object.
**
** @param config config dictionary
** @param[out] result double result
** @param ... list of objects NULL terminated, to select value
**
** @returns true if value found at index in dictionary.
*/
int ConfigStringsGetDouble(const ConfigObject * config, double *result, ...)
{
va_list ap;
const ConfigObject *value;
va_start(ap, result);
value = ConfigStringsLookup(config, ap);
va_end(ap);
if (ConfigIsFloat(value)) {
*result = ConfigDouble(value);
return 1;
}
if (value) {
fprintf(stderr, "value isn't a double\n");
}
return 0;
}