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
|
/*
* Copyright (C) 2010-2014 Tobias Brunner
* Copyright (C) 2008 Martin Willi
* Hochschule fuer Technik Rapperswil
*
* 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. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* 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.
*/
#define _GNU_SOURCE
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <ctype.h>
#include "settings.h"
#include "settings_types.h"
#include "collections/array.h"
#include "collections/hashtable.h"
#include "collections/linked_list.h"
#include "threading/rwlock.h"
#include "utils/debug.h"
typedef struct private_settings_t private_settings_t;
/**
* Parse functions provided by the generated parser.
*/
bool settings_parser_parse_file(section_t *root, char *name);
bool settings_parser_parse_string(section_t *root, char *settings);
/**
* Private data of settings
*/
struct private_settings_t {
/**
* Public interface
*/
settings_t public;
/**
* Top level section
*/
section_t *top;
/**
* Contents of replaced settings (char*)
*
* FIXME: This is required because the pointer returned by get_str()
* is not refcounted. Might cause ever increasing usage stats.
*/
array_t *contents;
/**
* Lock to safely access the settings
*/
rwlock_t *lock;
};
/**
* Print a format key, but consume already processed arguments
*/
static bool print_key(char *buf, int len, char *start, char *key, va_list args)
{
va_list copy;
char *pos = start;
bool res;
va_copy(copy, args);
while (TRUE)
{
pos = memchr(pos, '%', key - pos);
if (!pos)
{
break;
}
pos++;
switch (*pos)
{
case 'd':
va_arg(copy, int);
break;
case 's':
va_arg(copy, char*);
break;
case 'N':
va_arg(copy, enum_name_t*);
va_arg(copy, int);
break;
case '%':
break;
default:
DBG1(DBG_CFG, "settings with %%%c not supported!", *pos);
break;
}
pos++;
}
res = vsnprintf(buf, len, key, copy) < len;
va_end(copy);
return res;
}
/**
* Find a section by a given key, using buffered key, reusable buffer.
* If "ensure" is TRUE, the sections are created if they don't exist.
*/
static section_t *find_section_buffered(section_t *section,
char *start, char *key, va_list args, char *buf, int len,
bool ensure)
{
char *pos;
section_t *found = NULL;
if (section == NULL)
{
return NULL;
}
pos = strchr(key, '.');
if (pos)
{
*pos = '\0';
pos++;
}
if (!print_key(buf, len, start, key, args))
{
return NULL;
}
if (!strlen(buf))
{
found = section;
}
else if (array_bsearch(section->sections, buf, settings_section_find,
&found) == -1)
{
if (ensure)
{
found = settings_section_create(strdup(buf));
settings_section_add(section, found, NULL);
}
}
if (found && pos)
{
return find_section_buffered(found, start, pos, args, buf, len, ensure);
}
return found;
}
/**
* Find all sections via a given key considering fallbacks, using buffered key,
* reusable buffer.
*/
static void find_sections_buffered(section_t *section, char *start, char *key,
va_list args, char *buf, int len, array_t **sections)
{
section_t *found = NULL, *fallback;
char *pos;
int i;
if (!section)
{
return;
}
pos = strchr(key, '.');
if (pos)
{
*pos = '\0';
}
if (!print_key(buf, len, start, key, args))
{
return;
}
if (pos)
{ /* restore so we can follow fallbacks */
*pos = '.';
}
if (!strlen(buf))
{
found = section;
}
else
{
array_bsearch(section->sections, buf, settings_section_find, &found);
}
if (found)
{
if (pos)
{
find_sections_buffered(found, start, pos+1, args, buf, len,
sections);
}
else
{
array_insert_create(sections, ARRAY_TAIL, found);
for (i = 0; i < array_count(found->fallbacks); i++)
{
array_get(found->fallbacks, i, &fallback);
array_insert_create(sections, ARRAY_TAIL, fallback);
}
}
}
if (section->fallbacks)
{
for (i = 0; i < array_count(section->fallbacks); i++)
{
array_get(section->fallbacks, i, &fallback);
find_sections_buffered(fallback, start, key, args, buf, len,
sections);
}
}
}
/**
* Ensure that the section with the given key exists (thread-safe).
*/
static section_t *ensure_section(private_settings_t *this, section_t *section,
const char *key, va_list args)
{
char buf[128], keybuf[512];
section_t *found;
if (snprintf(keybuf, sizeof(keybuf), "%s", key) >= sizeof(keybuf))
{
return NULL;
}
/* we might have to change the tree */
this->lock->write_lock(this->lock);
found = find_section_buffered(section, keybuf, keybuf, args, buf,
sizeof(buf), TRUE);
this->lock->unlock(this->lock);
return found;
}
/**
* Find a section by a given key with its fallbacks (not thread-safe!).
* Sections are returned in depth-first order (array is allocated). NULL is
* returned if no sections are found.
*/
static array_t *find_sections(private_settings_t *this, section_t *section,
char *key, va_list args)
{
char buf[128], keybuf[512];
array_t *sections = NULL;
if (snprintf(keybuf, sizeof(keybuf), "%s", key) >= sizeof(keybuf))
{
return NULL;
}
find_sections_buffered(section, keybuf, keybuf, args, buf,
sizeof(buf), §ions);
return sections;
}
/**
* Check if the given fallback section already exists
*/
static bool fallback_exists(section_t *section, section_t *fallback)
{
if (section == fallback)
{
return TRUE;
}
else if (section->fallbacks)
{
section_t *existing;
int i;
for (i = 0; i < array_count(section->fallbacks); i++)
{
array_get(section->fallbacks, i, &existing);
if (existing == fallback)
{
return TRUE;
}
}
}
return FALSE;
}
/**
* Ensure that the section with the given key exists and add the given fallback
* section (thread-safe).
*/
static void add_fallback_to_section(private_settings_t *this,
section_t *section, const char *key, va_list args,
section_t *fallback)
{
char buf[128], keybuf[512];
section_t *found;
if (snprintf(keybuf, sizeof(keybuf), "%s", key) >= sizeof(keybuf))
{
return;
}
this->lock->write_lock(this->lock);
found = find_section_buffered(section, keybuf, keybuf, args, buf,
sizeof(buf), TRUE);
if (!fallback_exists(found, fallback))
{
/* to ensure sections referred to as fallback are not purged, we create
* the array there too */
if (!fallback->fallbacks)
{
fallback->fallbacks = array_create(0, 0);
}
array_insert_create(&found->fallbacks, ARRAY_TAIL, fallback);
}
this->lock->unlock(this->lock);
}
/**
* Find the key/value pair for a key, using buffered key, reusable buffer
* If "ensure" is TRUE, the sections (and key/value pair) are created if they
* don't exist.
* Fallbacks are only considered if "ensure" is FALSE.
*/
static kv_t *find_value_buffered(section_t *section, char *start, char *key,
va_list args, char *buf, int len, bool ensure)
{
int i;
char *pos;
kv_t *kv = NULL;
section_t *found = NULL;
if (section == NULL)
{
return NULL;
}
pos = strchr(key, '.');
if (pos)
{
*pos = '\0';
if (!print_key(buf, len, start, key, args))
{
return NULL;
}
/* restore so we can retry for fallbacks */
*pos = '.';
if (!strlen(buf))
{
found = section;
}
else if (array_bsearch(section->sections, buf, settings_section_find,
&found) == -1)
{
if (ensure)
{
found = settings_section_create(strdup(buf));
settings_section_add(section, found, NULL);
}
}
if (found)
{
kv = find_value_buffered(found, start, pos+1, args, buf, len,
ensure);
}
if (!kv && !ensure && section->fallbacks)
{
for (i = 0; !kv && i < array_count(section->fallbacks); i++)
{
array_get(section->fallbacks, i, &found);
kv = find_value_buffered(found, start, key, args, buf, len,
ensure);
}
}
}
else
{
if (!print_key(buf, len, start, key, args))
{
return NULL;
}
if (array_bsearch(section->kv, buf, settings_kv_find, &kv) == -1)
{
if (ensure)
{
kv = settings_kv_create(strdup(buf), NULL);
settings_kv_add(section, kv, NULL);
}
else if (section->fallbacks)
{
for (i = 0; !kv && i < array_count(section->fallbacks); i++)
{
array_get(section->fallbacks, i, &found);
kv = find_value_buffered(found, start, key, args, buf, len,
ensure);
}
}
}
}
return kv;
}
/**
* Find the string value for a key (thread-safe).
*/
static char *find_value(private_settings_t *this, section_t *section,
char *key, va_list args)
{
char buf[128], keybuf[512], *value = NULL;
kv_t *kv;
if (snprintf(keybuf, sizeof(keybuf), "%s", key) >= sizeof(keybuf))
{
return NULL;
}
this->lock->read_lock(this->lock);
kv = find_value_buffered(section, keybuf, keybuf, args, buf, sizeof(buf),
FALSE);
if (kv)
{
value = kv->value;
}
this->lock->unlock(this->lock);
return value;
}
/**
* Set a value to a copy of the given string (thread-safe).
*/
static void set_value(private_settings_t *this, section_t *section,
char *key, va_list args, char *value)
{
char buf[128], keybuf[512];
kv_t *kv;
if (snprintf(keybuf, sizeof(keybuf), "%s", key) >= sizeof(keybuf))
{
return;
}
this->lock->write_lock(this->lock);
kv = find_value_buffered(section, keybuf, keybuf, args, buf, sizeof(buf),
TRUE);
if (kv)
{
settings_kv_set(kv, strdupnull(value), this->contents);
}
this->lock->unlock(this->lock);
}
METHOD(settings_t, get_str, char*,
private_settings_t *this, char *key, char *def, ...)
{
char *value;
va_list args;
va_start(args, def);
value = find_value(this, this->top, key, args);
va_end(args);
if (value)
{
return value;
}
return def;
}
/**
* Described in header
*/
inline bool settings_value_as_bool(char *value, bool def)
{
if (value)
{
if (strcaseeq(value, "1") ||
strcaseeq(value, "yes") ||
strcaseeq(value, "true") ||
strcaseeq(value, "enabled"))
{
return TRUE;
}
else if (strcaseeq(value, "0") ||
strcaseeq(value, "no") ||
strcaseeq(value, "false") ||
strcaseeq(value, "disabled"))
{
return FALSE;
}
}
return def;
}
METHOD(settings_t, get_bool, bool,
private_settings_t *this, char *key, bool def, ...)
{
char *value;
va_list args;
va_start(args, def);
value = find_value(this, this->top, key, args);
va_end(args);
return settings_value_as_bool(value, def);
}
/**
* Described in header
*/
inline int settings_value_as_int(char *value, int def)
{
int intval;
char *end;
if (value)
{
errno = 0;
intval = strtol(value, &end, 10);
if (errno == 0 && *end == 0 && end != value)
{
return intval;
}
}
return def;
}
METHOD(settings_t, get_int, int,
private_settings_t *this, char *key, int def, ...)
{
char *value;
va_list args;
va_start(args, def);
value = find_value(this, this->top, key, args);
va_end(args);
return settings_value_as_int(value, def);
}
/**
* Described in header
*/
inline double settings_value_as_double(char *value, double def)
{
double dval;
char *end;
if (value)
{
errno = 0;
dval = strtod(value, &end);
if (errno == 0 && *end == 0 && end != value)
{
return dval;
}
}
return def;
}
METHOD(settings_t, get_double, double,
private_settings_t *this, char *key, double def, ...)
{
char *value;
va_list args;
va_start(args, def);
value = find_value(this, this->top, key, args);
va_end(args);
return settings_value_as_double(value, def);
}
/**
* Described in header
*/
inline u_int32_t settings_value_as_time(char *value, u_int32_t def)
{
char *endptr;
u_int32_t timeval;
if (value)
{
errno = 0;
timeval = strtoul(value, &endptr, 10);
if (endptr == value)
{
return def;
}
if (errno == 0)
{
while (isspace(*endptr))
{
endptr++;
}
switch (*endptr)
{
case 'd': /* time in days */
timeval *= 24 * 3600;
break;
case 'h': /* time in hours */
timeval *= 3600;
break;
case 'm': /* time in minutes */
timeval *= 60;
break;
case 's': /* time in seconds */
case '\0':
break;
default:
return def;
}
return timeval;
}
}
return def;
}
METHOD(settings_t, get_time, u_int32_t,
private_settings_t *this, char *key, u_int32_t def, ...)
{
char *value;
va_list args;
va_start(args, def);
value = find_value(this, this->top, key, args);
va_end(args);
return settings_value_as_time(value, def);
}
METHOD(settings_t, set_str, void,
private_settings_t *this, char *key, char *value, ...)
{
va_list args;
va_start(args, value);
set_value(this, this->top, key, args, value);
va_end(args);
}
METHOD(settings_t, set_bool, void,
private_settings_t *this, char *key, bool value, ...)
{
va_list args;
va_start(args, value);
set_value(this, this->top, key, args, value ? "1" : "0");
va_end(args);
}
METHOD(settings_t, set_int, void,
private_settings_t *this, char *key, int value, ...)
{
char val[16];
va_list args;
va_start(args, value);
if (snprintf(val, sizeof(val), "%d", value) < sizeof(val))
{
set_value(this, this->top, key, args, val);
}
va_end(args);
}
METHOD(settings_t, set_double, void,
private_settings_t *this, char *key, double value, ...)
{
char val[64];
va_list args;
va_start(args, value);
if (snprintf(val, sizeof(val), "%f", value) < sizeof(val))
{
set_value(this, this->top, key, args, val);
}
va_end(args);
}
METHOD(settings_t, set_time, void,
private_settings_t *this, char *key, u_int32_t value, ...)
{
char val[16];
va_list args;
va_start(args, value);
if (snprintf(val, sizeof(val), "%u", value) < sizeof(val))
{
set_value(this, this->top, key, args, val);
}
va_end(args);
}
METHOD(settings_t, set_default_str, bool,
private_settings_t *this, char *key, char *value, ...)
{
char *old;
va_list args;
va_start(args, value);
old = find_value(this, this->top, key, args);
va_end(args);
if (!old)
{
va_start(args, value);
set_value(this, this->top, key, args, value);
va_end(args);
return TRUE;
}
return FALSE;
}
/**
* Data for enumerators
*/
typedef struct {
/** settings_t instance */
private_settings_t *settings;
/** sections to enumerate */
array_t *sections;
/** sections/keys that were already enumerated */
hashtable_t *seen;
} enumerator_data_t;
/**
* Destroy enumerator data
*/
static void enumerator_destroy(enumerator_data_t *this)
{
this->settings->lock->unlock(this->settings->lock);
this->seen->destroy(this->seen);
array_destroy(this->sections);
free(this);
}
/**
* Enumerate section names, not sections
*/
static bool section_filter(hashtable_t *seen, section_t **in, char **out)
{
*out = (*in)->name;
if (seen->get(seen, *out))
{
return FALSE;
}
seen->put(seen, *out, *out);
return TRUE;
}
/**
* Enumerate sections of the given section
*/
static enumerator_t *section_enumerator(section_t *section,
enumerator_data_t *data)
{
return enumerator_create_filter(
array_create_enumerator(section->sections_order),
(void*)section_filter, data->seen, NULL);
}
METHOD(settings_t, create_section_enumerator, enumerator_t*,
private_settings_t *this, char *key, ...)
{
enumerator_data_t *data;
array_t *sections;
va_list args;
this->lock->read_lock(this->lock);
va_start(args, key);
sections = find_sections(this, this->top, key, args);
va_end(args);
if (!sections)
{
this->lock->unlock(this->lock);
return enumerator_create_empty();
}
INIT(data,
.settings = this,
.sections = sections,
.seen = hashtable_create(hashtable_hash_str, hashtable_equals_str, 8),
);
return enumerator_create_nested(array_create_enumerator(sections),
(void*)section_enumerator, data, (void*)enumerator_destroy);
}
/**
* Enumerate key and values, not kv_t entries
*/
static bool kv_filter(hashtable_t *seen, kv_t **in, char **key,
void *none, char **value)
{
*key = (*in)->key;
if (seen->get(seen, *key) || !(*in)->value)
{
return FALSE;
}
*value = (*in)->value;
seen->put(seen, *key, *key);
return TRUE;
}
/**
* Enumerate key/value pairs of the given section
*/
static enumerator_t *kv_enumerator(section_t *section, enumerator_data_t *data)
{
return enumerator_create_filter(array_create_enumerator(section->kv_order),
(void*)kv_filter, data->seen, NULL);
}
METHOD(settings_t, create_key_value_enumerator, enumerator_t*,
private_settings_t *this, char *key, ...)
{
enumerator_data_t *data;
array_t *sections;
va_list args;
this->lock->read_lock(this->lock);
va_start(args, key);
sections = find_sections(this, this->top, key, args);
va_end(args);
if (!sections)
{
this->lock->unlock(this->lock);
return enumerator_create_empty();
}
INIT(data,
.settings = this,
.sections = sections,
.seen = hashtable_create(hashtable_hash_str, hashtable_equals_str, 8),
);
return enumerator_create_nested(array_create_enumerator(sections),
(void*)kv_enumerator, data, (void*)enumerator_destroy);
}
METHOD(settings_t, add_fallback, void,
private_settings_t *this, const char *key, const char *fallback, ...)
{
section_t *section;
va_list args;
/* find/create the fallback */
va_start(args, fallback);
section = ensure_section(this, this->top, fallback, args);
va_end(args);
va_start(args, fallback);
add_fallback_to_section(this, this->top, key, args, section);
va_end(args);
}
/**
* Load settings from files matching the given file pattern or from a string.
* All sections and values are added relative to "parent".
* All files (even included ones) have to be loaded successfully.
* If merge is FALSE the contents of parent are replaced with the parsed
* contents, otherwise they are merged together.
*/
static bool load_internal(private_settings_t *this, section_t *parent,
char *pattern, bool merge, bool string)
{
section_t *section;
bool loaded;
if (pattern == NULL || !pattern[0])
{ /* TODO: Clear parent if merge is FALSE? */
return TRUE;
}
section = settings_section_create(NULL);
loaded = string ? settings_parser_parse_string(section, pattern) :
settings_parser_parse_file(section, pattern);
if (!loaded)
{
settings_section_destroy(section, NULL);
return FALSE;
}
this->lock->write_lock(this->lock);
settings_section_extend(parent, section, this->contents, !merge);
this->lock->unlock(this->lock);
settings_section_destroy(section, NULL);
return TRUE;
}
METHOD(settings_t, load_files, bool,
private_settings_t *this, char *pattern, bool merge)
{
return load_internal(this, this->top, pattern, merge, FALSE);
}
METHOD(settings_t, load_files_section, bool,
private_settings_t *this, char *pattern, bool merge, char *key, ...)
{
section_t *section;
va_list args;
va_start(args, key);
section = ensure_section(this, this->top, key, args);
va_end(args);
if (!section)
{
return FALSE;
}
return load_internal(this, section, pattern, merge, FALSE);
}
METHOD(settings_t, load_string, bool,
private_settings_t *this, char *settings, bool merge)
{
return load_internal(this, this->top, settings, merge, TRUE);
}
METHOD(settings_t, load_string_section, bool,
private_settings_t *this, char *settings, bool merge, char *key, ...)
{
section_t *section;
va_list args;
va_start(args, key);
section = ensure_section(this, this->top, key, args);
va_end(args);
if (!section)
{
return FALSE;
}
return load_internal(this, section, settings, merge, TRUE);
}
METHOD(settings_t, destroy, void,
private_settings_t *this)
{
settings_section_destroy(this->top, NULL);
array_destroy_function(this->contents, (void*)free, NULL);
this->lock->destroy(this->lock);
free(this);
}
static private_settings_t *settings_create_base()
{
private_settings_t *this;
INIT(this,
.public = {
.get_str = _get_str,
.get_int = _get_int,
.get_double = _get_double,
.get_time = _get_time,
.get_bool = _get_bool,
.set_str = _set_str,
.set_int = _set_int,
.set_double = _set_double,
.set_time = _set_time,
.set_bool = _set_bool,
.set_default_str = _set_default_str,
.create_section_enumerator = _create_section_enumerator,
.create_key_value_enumerator = _create_key_value_enumerator,
.add_fallback = _add_fallback,
.load_files = _load_files,
.load_files_section = _load_files_section,
.load_string = _load_string,
.load_string_section = _load_string_section,
.destroy = _destroy,
},
.top = settings_section_create(NULL),
.contents = array_create(0, 0),
.lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
);
return this;
}
/*
* see header file
*/
settings_t *settings_create(char *file)
{
private_settings_t *this = settings_create_base();
load_files(this, file, FALSE);
return &this->public;
}
/*
* see header file
*/
settings_t *settings_create_string(char *settings)
{
private_settings_t *this = settings_create_base();
load_string(this, settings, FALSE);
return &this->public;
}
|