-
Notifications
You must be signed in to change notification settings - Fork 6
/
Libconfig.xs
1607 lines (1526 loc) · 46.3 KB
/
Libconfig.xs
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
// vi:filetype=c noet sw=4 ts=4 fdm=marker
#ifdef __cplusplus
extern "C" {
#endif
#include <EXTERN.h>
#include <perl.h>
#include <XSUB.h>
#ifdef __cplusplus
}
#endif
#include <libconfig.h>
#define UINTNUM 2147483647
#define LIBCONFIG_OK 0
#define LIBCONFIG_ERR_COMMON -1
#define LIBCONFIG_ERR_INPUT -2
typedef config_t *Conf__Libconfig;
typedef config_setting_t *Conf__Libconfig__Settings;
void auto_check_and_create(Conf__Libconfig , const char *, config_setting_t **, char **);
int sv2int(config_setting_t *, SV *);
int sv2float(config_setting_t *, SV *);
int sv2string(config_setting_t *, SV *);
int sv2addint(const char *, config_setting_t *, config_setting_t *, SV *);
int sv2addfloat(const char *, config_setting_t *, config_setting_t *, SV *);
int sv2addstring(const char *, config_setting_t *, config_setting_t *, SV *);
int sv2addarray(config_setting_t *, char *, config_setting_t *, SV *);
int sv2addobject(config_setting_t *, char *, config_setting_t *, SV *);
void set_scalar(config_setting_t *, SV *, int , int *);
void set_scalar_elem(config_setting_t *, int, SV *, int, int *);
void set_array(config_setting_t *, AV *, int *);
void set_hash(config_setting_t *, HV *, int *, int);
int set_scalarvalue(config_setting_t *, const char *, SV *, int, int);
int set_arrayvalue(config_setting_t *, const char *, AV *, int);
int set_hashvalue(config_setting_t *, const char *, HV *, int);
int get_general_array(config_setting_t *, SV **);
int get_general_list(config_setting_t *, SV **);
int get_general_object(config_setting_t *, SV **);
int get_general_value(Conf__Libconfig, const char *, SV **);
int set_boolean_value(Conf__Libconfig, const char *, SV *);
int set_general_value(Conf__Libconfig, const char *, SV *);
void remove_scalar_node(config_setting_t *, const char *, int, int *);
void
set_scalar(config_setting_t *settings, SV *value, int valueType, int *status)
{
if (settings == NULL) {
Perl_warn(aTHX_ "[WARN] Settings is null in set_scalar!");
}
switch (valueType) {
case CONFIG_TYPE_INT:
*status = config_setting_set_int(settings, SvIV(value));
break;
case CONFIG_TYPE_INT64:
*status = config_setting_set_int64(settings, SvUV(value));
break;
case CONFIG_TYPE_BOOL:
*status = config_setting_set_bool(settings, SvIV(value));
break;
case CONFIG_TYPE_FLOAT:
*status = config_setting_set_float(settings, SvNV(value));
break;
case CONFIG_TYPE_STRING:
*status = config_setting_set_string(settings, SvPV_nolen(value));
break;
default:
Perl_croak(aTHX_ "[ERROR] Scalar have not this type!");
}
}
void
set_scalar_elem(config_setting_t *settings, int idx, SV *value, int valueType, int *status)
{
config_setting_t *settings_item;
if (settings == NULL) {
Perl_warn(aTHX_ "[WARN] Settings is null in set_scalar_elem!");
}
switch (valueType) {
case CONFIG_TYPE_INT:
settings_item = config_setting_set_int_elem(settings, idx, SvIV(value));
break;
case CONFIG_TYPE_INT64:
settings_item = config_setting_set_int64_elem(settings, idx, SvUV(value));
break;
case CONFIG_TYPE_BOOL:
settings_item = config_setting_set_bool_elem(settings, idx, SvIV(value));
break;
case CONFIG_TYPE_FLOAT:
settings_item = config_setting_set_float_elem(settings, idx, SvNV(value));
break;
case CONFIG_TYPE_STRING:
settings_item = config_setting_set_string_elem(settings, idx, SvPV_nolen(value));
break;
default:
Perl_croak(aTHX_ "[ERROR] Scalar element have not this type!");
}
*status = !settings_item ? 0 : 1;
}
void
set_array(config_setting_t *settings, AV *value, int *status)
{
SV *sv;
int valueMaxIndex;
int i;
int type;
int elemStatus;
int allStatus;
SV *g_v = newSViv(2);
allStatus = 1;
#if PERL_API_REVISION <= 5 && PERL_API_VERSION <= 32
valueMaxIndex = (int)av_len(value);
#else
valueMaxIndex = (int)av_count(value) - 1;
#endif
for (i = 0; i <= valueMaxIndex; i ++)
{
sv = *(av_fetch(value, i, 0));
type = (int)(log(SvIOK(sv) + SvNOK(sv) + SvPOK(sv))/log(2)) - (SvIOK(g_v) == 256 ? 5 : 13);
if (type == 3) {
if (SvUV(sv) <= UINTNUM) type = 2;
}
set_scalar_elem(settings, -1, sv, type, &elemStatus);
allStatus = allStatus | elemStatus;
}
*status = allStatus;
}
void
set_hash(config_setting_t *settings, HV *value, int *status, int booldefinedflag)
{
HE* he;
I32 keyLen;
char *key;
int elemStatus;
int allStatus;
SV* sv;
allStatus = 1;
hv_iterinit(value);
while ((he = hv_iternext(value)))
{
key = hv_iterkey(he, &keyLen);
sv = hv_iterval(value, he);
// Only support simple hash
elemStatus = set_scalarvalue(settings, key, sv, 0, booldefinedflag);
allStatus = allStatus | elemStatus;
}
*status = allStatus;
}
int
set_scalarvalue(config_setting_t *settings, const char *key, SV *value, int flag, int booldefined)
{
int type;
int returnStatus;
config_setting_t *settings_item;
config_setting_t *settings_parent;
SV *g_v = newSViv(2);
if (settings == NULL) {
Perl_warn(aTHX_ "[WARN] Settings is null in set_scalarvalue!");
return 0;
}
if (SvIOK(value) + SvNOK(value) + SvPOK(value) == 0) {
type = (int)(log(SvROK(value))/log(2)) - (SvIOK(g_v) == 256 ? 10 : 18);
} else {
type = (int)(log(SvIOK(value) + SvNOK(value) + SvPOK(value) + SvROK(value))/log(2)) - (SvIOK(g_v) == 256 ? 5 : 13);
}
if (type == 3) {
if (SvUV(value) <= UINTNUM) type = 2;
if ((SvUV(value) == 0 || SvUV(value) == 1) && booldefined == 2) type = 6;
}
returnStatus = 0;
settings_parent = settings->parent;
switch (flag) {
case 1:
{
if (settings->type == type)
set_scalar(settings, value, type, &returnStatus);
else {
size_t nameLength = strlen(settings->name);
char *name = (char *)malloc(nameLength + 1);
if (!name) Perl_croak(aTHX_ "[ERROR] malloc is fail!!");
strncpy(name, settings->name, nameLength);
name[nameLength] = 0;
remove_scalar_node(settings_parent, settings->name, settings->type, &returnStatus);
set_scalarvalue(settings_parent, name, value, 0, 0);
if (name) free(name);
}
break;
}
default:
{
settings_item = config_setting_add(settings, key, type);
set_scalar(settings_item, value, type, &returnStatus);
}
}
return returnStatus;
}
int
set_arrayvalue(config_setting_t *settings, const char *key, AV *value, int flag)
{
int returnStatus;
config_setting_t *settings_item;
if (settings == NULL) {
Perl_warn(aTHX_ "[WARN] Settings is null in set_arrayvalue!");
return 0;
}
returnStatus = 0;
switch (settings->type) {
case CONFIG_TYPE_INT:
case CONFIG_TYPE_INT64:
case CONFIG_TYPE_FLOAT:
case CONFIG_TYPE_BOOL:
case CONFIG_TYPE_STRING:
Perl_croak(aTHX_ "[ERROR] Scalar can't add array node!");
break;
case CONFIG_TYPE_ARRAY:
case CONFIG_TYPE_LIST:
set_array(settings, value, &returnStatus);
break;
case CONFIG_TYPE_GROUP:
settings_item = config_setting_add(settings, key, (flag ? CONFIG_TYPE_LIST : CONFIG_TYPE_ARRAY));
set_array(settings_item, value, &returnStatus);
break;
}
return returnStatus;
}
int
set_hashvalue(config_setting_t *settings, const char *key, HV *value, int booldefinedflag)
{
int returnStatus;
config_setting_t *settings_item;
if (settings == NULL) {
Perl_warn(aTHX_ "[WARN] Settings is null in set_hashvalue!");
return 0;
}
returnStatus = 0;
switch (settings->type) {
case CONFIG_TYPE_INT:
case CONFIG_TYPE_INT64:
case CONFIG_TYPE_FLOAT:
case CONFIG_TYPE_BOOL:
case CONFIG_TYPE_STRING:
Perl_croak(aTHX_ "[ERROR] Scalar can't add hash node!");
break;
case CONFIG_TYPE_ARRAY:
Perl_croak(aTHX_ "[ERROR] Array can't add hash node!");
break;
case CONFIG_TYPE_LIST:
settings_item = config_setting_add(settings, NULL, CONFIG_TYPE_GROUP);
set_hash(settings_item, value, &returnStatus, booldefinedflag);
break;
case CONFIG_TYPE_GROUP:
settings_item = config_setting_add(settings, key, CONFIG_TYPE_GROUP);
set_hash(settings_item, value, &returnStatus, booldefinedflag);
break;
}
return returnStatus;
}
/* {{{ remove_scalar_node */
void
remove_scalar_node(config_setting_t *settings, const char *name, int type, int *status)
{
if (type == CONFIG_TYPE_INT || type == CONFIG_TYPE_INT64 || type == CONFIG_TYPE_FLOAT || type == CONFIG_TYPE_STRING || type == CONFIG_TYPE_BOOL)
*status = config_setting_remove(settings, name);
else
Perl_croak(aTHX_ "[ERROR] Only can remove scalar setttings!");
}
/* }}} */
/* {{{ function */
int sv2int(config_setting_t *setting, SV *val)
{
int ret = LIBCONFIG_OK;
if (SvUV(val) > INT_MAX || SvIV(val) < INT_MIN) {
setting->type = CONFIG_TYPE_INT64;
ret = CONFIG_TRUE == config_setting_set_int64(setting, (long long)SvIV(val)) ? LIBCONFIG_OK : LIBCONFIG_ERR_COMMON;
} else {
setting->type = CONFIG_TYPE_INT;
ret = CONFIG_TRUE == config_setting_set_int(setting, SvIV(val)) ? LIBCONFIG_OK : LIBCONFIG_ERR_COMMON;
}
return ret;
}
int sv2float(config_setting_t *setting, SV *val)
{
setting->type = CONFIG_TYPE_FLOAT;
return CONFIG_TRUE == config_setting_set_float(setting, SvNV(val)) ? LIBCONFIG_OK : LIBCONFIG_ERR_COMMON;
}
int sv2string(config_setting_t *setting, SV *val)
{
setting->type = CONFIG_TYPE_STRING;
return CONFIG_TRUE == config_setting_set_string(setting, SvPV_nolen(val)) ? LIBCONFIG_OK : LIBCONFIG_ERR_COMMON;
}
int sv2addint(const char *key, config_setting_t *setting, config_setting_t *child_setting, SV *val)
{
int ret = LIBCONFIG_OK;
// boolean and int32 are IV, so they can only be reduced to one, namely int32
if (SvUV(val) > INT_MAX || SvIV(val) < INT_MIN)
{
if (child_setting == NULL)
{
child_setting = config_setting_add(setting, key, CONFIG_TYPE_INT64);
}
else
{
child_setting->type = CONFIG_TYPE_INT64;
}
ret = CONFIG_TRUE == config_setting_set_int64(child_setting, (long long)SvIV(val)) ? LIBCONFIG_OK : LIBCONFIG_ERR_COMMON;
}
else
{
if (child_setting == NULL)
{
child_setting = config_setting_add(setting, key, CONFIG_TYPE_INT);
}
else
{
child_setting->type = CONFIG_TYPE_INT;
}
ret = CONFIG_TRUE == config_setting_set_int(child_setting, SvIV(val)) ? LIBCONFIG_OK : LIBCONFIG_ERR_COMMON;
}
return ret;
}
int sv2addfloat(const char *key, config_setting_t *setting, config_setting_t *child_setting, SV *val)
{
if (child_setting == NULL)
{
child_setting = config_setting_add(setting, key, CONFIG_TYPE_FLOAT);
} else {
child_setting->type = CONFIG_TYPE_FLOAT;
}
return CONFIG_TRUE == config_setting_set_float(child_setting, SvNV(val)) ? LIBCONFIG_OK : LIBCONFIG_ERR_COMMON;
}
int sv2addstring(const char *key, config_setting_t *setting, config_setting_t *child_setting, SV *val)
{
if (child_setting == NULL)
{
child_setting = config_setting_add(setting, key, CONFIG_TYPE_STRING);
} else {
child_setting->type = CONFIG_TYPE_STRING;
}
return CONFIG_TRUE == config_setting_set_string(child_setting, SvPV_nolen(val)) ? LIBCONFIG_OK : LIBCONFIG_ERR_COMMON;
}
int sv2addarray(config_setting_t *parent_setting, char *key, config_setting_t *setting, SV *sv)
{
int ret = LIBCONFIG_OK;
AV *av = (AV *)SvRV(sv);
#if PERL_API_REVISION <= 5 && PERL_API_VERSION <= 32
int avlen = (int)av_len(av) + 1;
#else
int avlen = (int)av_count(av);
#endif
if (avlen < 1)
{
return 0;
}
// get the first one
int i = 0;
int is_setting_array_type = 1;
SV *child_sv_0 = *(av_fetch(av, i, 0));
int save_type = SvROK(child_sv_0) ? SvTYPE(SvRV(child_sv_0)) : SvTYPE(child_sv_0);
for (i ++; i < avlen; i ++)
{
SV *child_sv = *(av_fetch(av, i, 0));
if (save_type != (SvROK(child_sv) ? SvTYPE(SvRV(child_sv)) : SvTYPE(child_sv)))
{
is_setting_array_type = 0;
break;
}
}
if (setting == NULL)
{
setting = config_setting_add(parent_setting, key, is_setting_array_type ? CONFIG_TYPE_ARRAY : CONFIG_TYPE_LIST);
}
else
{
setting->type = is_setting_array_type ? CONFIG_TYPE_ARRAY : CONFIG_TYPE_LIST;
}
int settinglen = config_setting_length(setting);
// Perl_warn(aTHX_ "ss name: %s av index: %d, settinglen: %d and setting type: %d", setting->name, avlen, settinglen, setting->type);
for (i = 0; i < avlen; i ++)
{
SV *child_sv = *(av_fetch(av, i, 0));
// check type
config_setting_t *child_setting = NULL;
if (SvROK(child_sv))
{
switch (SvTYPE(SvRV(child_sv)))
{
case SVt_PVAV:
{
child_setting = config_setting_get_elem(setting, i);
ret += sv2addarray(setting, NULL, child_setting, child_sv);
break;
}
case SVt_PVHV:
{
child_setting = config_setting_get_elem(setting, i);
ret += sv2addobject(setting, NULL, child_setting, child_sv);
break;
}
default:
break;
}
}
else
{
switch (SvTYPE(child_sv))
{
case SVt_IV:
{
child_setting = config_setting_get_elem(setting, i);
ret += sv2addint(NULL, setting, child_setting, child_sv);
break;
}
case SVt_NV:
{
child_setting = config_setting_get_elem(setting, i);
ret += sv2addfloat(NULL, setting, child_setting, child_sv);
break;
}
case SVt_PV:
{
child_setting = config_setting_get_elem(setting, i);
ret += sv2addstring(NULL, setting, child_setting, child_sv);
break;
}
default:
break;
}
}
}
while (settinglen > avlen)
{
ret += CONFIG_TRUE == config_setting_remove_elem(setting, avlen) ? LIBCONFIG_OK : LIBCONFIG_ERR_COMMON;
settinglen = config_setting_length(setting);
}
return ret;
}
int sv2addobject(config_setting_t *parent_setting, char *key, config_setting_t *setting, SV *sv)
{
if (setting == NULL)
{
setting = config_setting_add(parent_setting, key, CONFIG_TYPE_GROUP);
}
int ret = LIBCONFIG_OK;
HV *hv = (HV *)SvRV(sv);
hv_iterinit(hv);
HE *he = NULL;
while ((he = hv_iternext(hv)))
{
I32 len;
char *key = hv_iterkey(he, &len);
SV *val = hv_iterval(hv, he);
config_setting_t *child_setting = NULL;
if (SvROK(val))
{
switch (SvTYPE(SvRV(val)))
{
case SVt_PVAV:
{
ret += sv2addarray(setting, key, child_setting, val);
break;
}
case SVt_PVHV:
{
ret += sv2addobject(setting, key, child_setting, val);
break;
}
default:
break;
}
}
else
{
switch (SvTYPE(val))
{
case SVt_IV:
{
ret += sv2addint(key, setting, child_setting, val);
break;
}
case SVt_NV:
{
ret += sv2addfloat(key, setting, child_setting, val);
break;
}
case SVt_PV:
{
ret += sv2addstring(key, setting, child_setting, val);
break;
}
default:
break;
}
}
}
return ret;
}
/* }}} */
/* {{{ set value */
int set_general_value(Conf__Libconfig conf, const char *path, SV *sv)
{
int ret = LIBCONFIG_OK;
config_setting_t *parent_setting;
char *start_path;
config_setting_t *elem = path != NULL && strlen(path) == 0 ? config_root_setting(conf) : config_lookup(conf, path);
if (elem == NULL)
{
auto_check_and_create(conf, path, &parent_setting, &start_path);
}
if (SvROK(sv))
{
switch (SvTYPE(SvRV(sv)))
{
case SVt_PVAV:
{
ret += sv2addarray(parent_setting, start_path, elem, sv);
break;
}
case SVt_PVHV:
{
ret += sv2addobject(parent_setting, start_path, elem, sv);
break;
}
default:
break;
}
}
else
{
switch (SvTYPE(sv))
{
case SVt_IV:
{
ret = elem ? sv2int(elem, sv) : sv2addint(start_path, parent_setting, elem, sv);
break;
}
case SVt_NV:
{
ret = elem ? sv2float(elem, sv) : sv2addfloat(start_path, parent_setting, elem, sv);
break;
}
case SVt_PV:
{
ret = elem ? sv2string(elem, sv) : sv2addstring(start_path, parent_setting, elem, sv);
break;
}
default:
break;
}
}
return ret;
}
void auto_check_and_create(Conf__Libconfig conf, const char *path, config_setting_t **parent_setting_pptr, char **start_path_pptr)
{
config_setting_t *root_setting = config_root_setting(conf);
config_setting_t *parent_setting = root_setting;
config_setting_t *child_setting = root_setting;
int path_len = strlen(path);
char *tmp_path = (char *)malloc(path_len + 1);
char *start_path = (char *)path;
char *end_path = strchr(start_path, '.');
if (end_path != NULL)
{
sprintf(tmp_path, "%.*s", (int)(end_path - start_path), start_path);
start_path = end_path + 1;
child_setting = config_setting_get_member(parent_setting, tmp_path);
if (child_setting == NULL)
{
child_setting = config_setting_add(parent_setting, tmp_path, CONFIG_TYPE_GROUP);
}
parent_setting = child_setting;
while ((end_path = strrchr(start_path, '.')))
{
sprintf(tmp_path, "%.*s", (int)(end_path - start_path), start_path);
start_path = end_path + 1;
child_setting = config_setting_get_member(parent_setting, tmp_path);
if (child_setting == NULL)
{
child_setting = config_setting_add(parent_setting, tmp_path, CONFIG_TYPE_GROUP);
}
parent_setting = child_setting;
}
}
free(tmp_path);
*parent_setting_pptr = parent_setting;
*start_path_pptr = start_path;
}
int set_boolean_value(Conf__Libconfig conf, const char *path, SV *sv)
{
int ret = LIBCONFIG_OK;
switch (SvTYPE(sv))
{
case SVt_IV:
case SVt_PV:
{
config_setting_t *elem = path != NULL && strlen(path) == 0 ? config_root_setting(conf) : config_lookup(conf, path);
if (!elem)
{
config_setting_t *parent_setting;
char *start_path;
auto_check_and_create(conf, path, &parent_setting, &start_path);
elem = config_setting_add(parent_setting, start_path, CONFIG_TYPE_BOOL);
} else {
elem->type = CONFIG_TYPE_BOOL;
}
if (SvTYPE(sv) == SVt_PV) {
if (strcasecmp(SvPV_nolen(sv), "true") == 0) {
ret = CONFIG_TRUE == config_setting_set_bool(elem, 1) ? LIBCONFIG_OK : LIBCONFIG_ERR_COMMON;
} else if (strcasecmp(SvPV_nolen(sv), "false") == 0) {
ret = CONFIG_TRUE == config_setting_set_bool(elem, 0) ? LIBCONFIG_OK : LIBCONFIG_ERR_COMMON;
} else {
ret = LIBCONFIG_ERR_INPUT;
}
} else {
ret = CONFIG_TRUE == config_setting_set_bool(elem, SvIV(sv)) ? LIBCONFIG_OK : LIBCONFIG_ERR_COMMON;
}
break;
}
default:
{
ret = LIBCONFIG_ERR_INPUT;
}
}
return ret;
}
/* }}} */
/* {{{ get value */
int
get_general_value(Conf__Libconfig conf, const char *path, SV **svref)
{
config_setting_t *elem = path != NULL && strlen(path) == 0 ? config_root_setting(conf) : config_lookup(conf, path);
if (NULL == elem)
{
// Perl_warn(aTHX_ "[WARN] elem is null in %s", path);
*svref = newSVpvn("", 0);
return LIBCONFIG_ERR_INPUT;
}
switch (config_setting_type(elem))
{
case CONFIG_TYPE_BOOL:
*svref = newSViv(config_setting_get_bool(elem));
break;
case CONFIG_TYPE_INT:
*svref = newSViv(config_setting_get_int(elem));
break;
case CONFIG_TYPE_INT64:
*svref = newSViv(elem->value.llval);
break;
case CONFIG_TYPE_FLOAT:
*svref = newSVnv(config_setting_get_float(elem));
break;
case CONFIG_TYPE_STRING:
{
const char *val = config_setting_get_string(elem);
*svref = newSVpvn(val, strlen(val));
break;
}
case CONFIG_TYPE_ARRAY:
{
return get_general_array(elem, svref);
}
case CONFIG_TYPE_LIST:
{
return get_general_list(elem, svref);
}
case CONFIG_TYPE_GROUP:
{
return get_general_object(elem, svref);
}
default:
{
Perl_warn(aTHX_ "[WARN] Scalar have not this type: %d in %s", config_setting_type(elem), path);
*svref = newSV(0);
return LIBCONFIG_ERR_COMMON;
}
}
return LIBCONFIG_OK;
}
int get_general_array(config_setting_t *setting, SV **sv_pptr)
{
AV *child_av_ptr = newAV();
if (setting == NULL || setting->type != CONFIG_TYPE_ARRAY)
{
*sv_pptr = newRV_inc((SV *)child_av_ptr);
return LIBCONFIG_ERR_COMMON;
}
int arr_len = config_setting_length(setting);
int i = 0;
for (; i < arr_len; i++)
{
config_setting_t *child_setting = config_setting_get_elem(setting, i);
switch (config_setting_type(child_setting))
{
case CONFIG_TYPE_BOOL:
{
av_push(child_av_ptr, newSViv(config_setting_get_bool(child_setting)));
break;
}
case CONFIG_TYPE_INT:
{
av_push(child_av_ptr, newSViv(config_setting_get_int(child_setting)));
break;
}
case CONFIG_TYPE_INT64:
{
av_push(child_av_ptr, newSViv(child_setting->value.llval));
break;
}
case CONFIG_TYPE_FLOAT:
{
av_push(child_av_ptr, newSVnv(config_setting_get_float(child_setting)));
break;
}
case CONFIG_TYPE_STRING:
{
const char *child_val = config_setting_get_string(child_setting);
av_push(child_av_ptr, newSVpvn(child_val, strlen(child_val)));
break;
}
default:
{
Perl_warn(aTHX_ "[WARN] Array have not this type: %d", config_setting_type(setting));
*sv_pptr = newRV_inc((SV *)child_av_ptr);
return LIBCONFIG_ERR_COMMON;
}
}
}
*sv_pptr = newRV_inc((SV *)child_av_ptr);
return LIBCONFIG_OK;
}
int get_general_list(config_setting_t *setting, SV **sv_pptr)
{
AV *child_av_ptr = newAV();
if (setting == NULL || setting->type != CONFIG_TYPE_LIST)
{
*sv_pptr = newRV_inc((SV *)child_av_ptr);
return LIBCONFIG_ERR_INPUT;
}
int list_size = config_setting_length(setting);
int i = 0;
for (; i < list_size; i++)
{
config_setting_t *child_setting = config_setting_get_elem(setting, i);
switch (config_setting_type(child_setting))
{
case CONFIG_TYPE_BOOL:
{
av_push(child_av_ptr, newSViv(config_setting_get_bool(child_setting)));
break;
}
case CONFIG_TYPE_INT:
{
av_push(child_av_ptr, newSViv(config_setting_get_int(child_setting)));
break;
}
case CONFIG_TYPE_INT64:
{
av_push(child_av_ptr, newSViv(child_setting->value.llval));
break;
}
case CONFIG_TYPE_FLOAT:
{
av_push(child_av_ptr, newSVnv(config_setting_get_float(child_setting)));
break;
}
case CONFIG_TYPE_STRING:
{
const char *child_val = config_setting_get_string(child_setting);
av_push(child_av_ptr, newSVpvn(child_val, strlen(child_val)));
break;
}
case CONFIG_TYPE_ARRAY:
{
SV *sv;
if (get_general_array(child_setting, &sv) == LIBCONFIG_OK)
{
av_push(child_av_ptr, sv);
}
// ignore error
break;
}
case CONFIG_TYPE_LIST:
{
SV *sv;
if (get_general_list(child_setting, &sv) == LIBCONFIG_OK);
{
av_push(child_av_ptr, sv);
}
// ignore error
break;
}
case CONFIG_TYPE_GROUP:
{
SV *sv;
if (get_general_object(child_setting, &sv) == LIBCONFIG_OK);
{
av_push(child_av_ptr, sv);
}
// ignore error
break;
}
default:
{
Perl_warn(aTHX_ "[WARN] List have not this type: %d in [%d]", config_setting_type(setting), i);
*sv_pptr = newRV_inc((SV *)child_av_ptr);
return LIBCONFIG_ERR_COMMON;
}
}
}
*sv_pptr = newRV_inc((SV *)child_av_ptr);
return LIBCONFIG_OK;
}
int get_general_object(config_setting_t *setting, SV **sv_pptr)
{
HV *child_hv_ptr = newHV();
if (setting == NULL || setting->type != CONFIG_TYPE_GROUP)
{
*sv_pptr = newRV_inc((SV *)child_hv_ptr);
return LIBCONFIG_ERR_INPUT;
}
int obj_cnt = config_setting_length(setting);
int i = 0;
for (; i < obj_cnt; i++)
{
config_setting_t *child_setting = config_setting_get_elem(setting, i);
switch (config_setting_type(child_setting))
{
case CONFIG_TYPE_BOOL:
{
hv_store(child_hv_ptr, child_setting->name, strlen(child_setting->name), newSViv(config_setting_get_bool(child_setting)), 0);
break;
}
case CONFIG_TYPE_INT:
{
hv_store(child_hv_ptr, child_setting->name, strlen(child_setting->name), newSViv(config_setting_get_int(child_setting)), 0);
break;
}
case CONFIG_TYPE_INT64:
{
hv_store(child_hv_ptr, child_setting->name, strlen(child_setting->name), newSViv(child_setting->value.llval), 0);
break;
}
case CONFIG_TYPE_FLOAT:
{
hv_store(child_hv_ptr, child_setting->name, strlen(child_setting->name), newSVnv(config_setting_get_float(child_setting)), 0);
break;
}
case CONFIG_TYPE_STRING:
{
const char *child_val = config_setting_get_string(child_setting);
hv_store(child_hv_ptr, child_setting->name, strlen(child_setting->name), newSVpvn(child_val, strlen(child_val)), 0);
break;
}
case CONFIG_TYPE_ARRAY:
{
SV *sv;
if (get_general_array(child_setting, &sv) == LIBCONFIG_OK)
{
hv_store(child_hv_ptr, child_setting->name, strlen(child_setting->name), sv, 0);
}
// ignore error
break;
}
case CONFIG_TYPE_LIST:
{
SV *sv;
if (get_general_list(child_setting, &sv) == LIBCONFIG_OK)
{
hv_store(child_hv_ptr, child_setting->name, strlen(child_setting->name), sv, 0);
}
// ignore error
break;
}
case CONFIG_TYPE_GROUP:
{
SV *sv;
if (get_general_object(child_setting, &sv) == LIBCONFIG_OK)
{
hv_store(child_hv_ptr, child_setting->name, strlen(child_setting->name), sv, 0);
}
// ignore error
break;
}
default:
{
Perl_warn(aTHX_ "[WARN] Object have not this type: %d", config_setting_type(setting));
*sv_pptr = newRV_inc((SV *)child_hv_ptr);
return LIBCONFIG_ERR_COMMON;
}
}
}
*sv_pptr = newRV_inc((SV *)child_hv_ptr);
return LIBCONFIG_OK;
}
/* }}} */
/*
* Module start
**/
MODULE = Conf::Libconfig PACKAGE = Conf::Libconfig PREFIX = libconfig_
Conf::Libconfig
libconfig_new(packname="Conf::Libconfig")
char *packname
PREINIT:
CODE:
{
config_t *pConf = (config_t *)safemalloc(sizeof(config_t));
if (pConf)
config_init(pConf);
RETVAL = pConf;
}
OUTPUT:
RETVAL
void
libconfig_delete(conf)
Conf::Libconfig conf
CODE:
config_destroy(conf);
void
libconfig_DESTROY(conf)
Conf::Libconfig conf
CODE:
config_destroy(conf);
double
libconfig_getversion(conf)
Conf::Libconfig conf
CODE:
{
#if (((LIBCONFIG_VER_MAJOR == 1) && (LIBCONFIG_VER_MINOR >= 4)) \
|| (LIBCONFIG_VER_MAJOR > 1))
char tmpChar[16];
sprintf(tmpChar, "%d.%d%d", LIBCONFIG_VER_MAJOR, LIBCONFIG_VER_MINOR, LIBCONFIG_VER_REVISION);
RETVAL = atof(tmpChar);
#else
RETVAL = 1.32;
#endif
}
OUTPUT:
RETVAL
int
libconfig_read(conf, stream)
Conf::Libconfig conf
FILE *stream
PREINIT:
CODE:
config_read(conf, stream);
int
libconfig_read_file(conf, filename)
Conf::Libconfig conf
const char *filename
CODE:
{
RETVAL = config_read_file(conf, filename);
}
OUTPUT:
RETVAL
int
libconfig_read_string(conf, string)
Conf::Libconfig conf