forked from xinzsky/libutils-c
/
conf.c
867 lines (760 loc) · 20.6 KB
/
conf.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
/* Copyright (c) 2007 Liu xing zhi. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <ctype.h>
#include "conf.h"
#include "xstring.h"
#include "xmalloc.h"
/* conf 文件比较大的话,建议定义一下 WITH_HASH */
/* uncomment the next line to enable hashing */
/* #define WITH_HASH */
/* #define _WIN32 */
#if defined(_WIN32) && !defined(__BORLANDC__)
#ifndef strcasecmp
#define strcasecmp stricmp
#endif
#endif
/* const variable define */
#ifdef WITH_HASH
#define CONF_HASH_SECTION_SIZE 1024 /* size of the section hash table */
#define CONF_HASH_KEY_SIZE 1024 /* size of the key hash table of each section */
#endif
#define CONF_SIZE_INCREMENT 10 /* increment of memory allocation for section/key */
#define CONF_LINE_MAX_LEN 1024
/* user data type define */
typedef struct
{
char *key_name;
char *key_value;
}conf_key_t;
typedef struct
{
char *section_name;
int key_count;
int current_size; /* section 中当前已分配内存的key数 */
conf_key_t *keys;/* 指向key数组*/
#ifdef WITH_HASH
conf_key_t * hash[CONF_HASH_KEY_SIZE];/* 指向key的指针数组 */
#endif
}conf_section_t;
typedef struct {
conf_section_t *psection;/* 用于线性查找的数据,指向section数组 */
int section_count;
int current_size; /* 当前已分配内存的section数 */
#ifdef WITH_HASH
conf_section_t **m_Hash; /* 用于Hash查找的数据,指向section的指针数组, 指针数组中的指针指向一个节*/
#endif
}cfgdata_t;
/* function prototype */
#ifdef WITH_HASH
static unsigned short get_hash_value(const char *sz);
#endif
static void free_all_the_stuff(cfgdata_t *cd);
static int valid(cfgdata_t *cd);
void *init_conf(void)
{
cfgdata_t *cd;
cd = (cfgdata_t *)xcalloc(1,sizeof(cfgdata_t));
cd->psection = NULL;
cd->section_count = 0;
cd->current_size = 0;
#ifdef WITH_HASH
cd->m_Hash = (conf_section_t **)xcalloc(CONF_HASH_SECTION_SIZE, sizeof(conf_section_t *));
#endif
return cd;
}
// 支持配置跨行
static char *getconfline(FILE *fp,char *linebuf,int bufsize)
{
int len,pos,span;
char str[CONF_LINE_MAX_LEN+1];
len = 0;
pos = 0;
str[0] = 0;
linebuf[0] = 0;
span = 0;
while (fgets(str, CONF_LINE_MAX_LEN + 1, fp))
{
len = strlen(str);
if(str[len-1] == '\n') str[--len] = 0;
if(str[len-1] == '\r') str[--len] = 0;
if(!str[0] && span) break;
if(!str[0]) continue;
if(str[len-1] == '\\') {
span = 1;
str[len-1] = '\0';
len--;
if(pos+len >= bufsize)
goto EXIT;
memcpy(linebuf+pos,str,len);
pos += len;
linebuf[pos] = 0;
} else {
if(pos+len >= bufsize)
goto EXIT;
memcpy(linebuf+pos,str,len);
pos += len;
linebuf[pos] = 0;
break;
}
}
if(linebuf[0])
return linebuf;
else
return NULL;
EXIT:
printf("configuration line too long.\n");
exit(1);
}
int load_conf(void *cfgdata,const char *filename)
{
FILE *fp;
char str[CONF_LINE_MAX_LEN*8+1];
char section[CONF_LINE_MAX_LEN+1];
int length;
char *p;
cfgdata_t *cd = (cfgdata_t *)cfgdata;
section[0] = '\0';
fp = fopen(filename, "r");
if (fp == NULL)
{
return -1;
}
/* if we already have a file loaded, free it first */
if (valid(cd))
{
free_all_the_stuff(cd);
}
while (getconfline(fp,str,CONF_LINE_MAX_LEN*8+1)) // fgets(str, CONF_LINE_MAX_LEN + 1, fp)
{
trim(str); /* trim all the blanks or linefeeds(换行) */
/* skip all comment lines or empty lines */
if (!str[0] || str[0] == ';' || str[0] == '/' || str[0] == '#')
{
continue;
}
length = strlen(str);
/* check if this is a section line (e.g., [SECTION]) */
if (str[0] == '[' && str[length - 1] == ']')
{
strcpy(section, &str[1]);
section[length - 2] = 0; /* remove the ] */
trim(section); /* trim section name after removing [] */
}
else
{
/* 配置项是名值对,中间用一个或多个空格或垂直制表符间隔 */
if (((p = strchr(str, ' ')) != NULL) || ((p = strchr(str, '\t')) != NULL) || ((p = strchr(str, '=')) != NULL))
{
*(p++) = '\0';
trim(str); /* str指向key name */
trim(p); /* p指向key value */
set_key_value(cd,section, str, p);
}
}
}
fclose(fp); /* load completed; close the file */
return 0;
}
// 由于使用了strtok()不能用于多线程环境.多线程环境需要修改为strtok_r()
int parse_conf(void *cfgdata,char *conf_str)
{
char str[CONF_LINE_MAX_LEN+1];
char section[CONF_LINE_MAX_LEN+1];
int length;
char *p;
char *line;
cfgdata_t *cd = (cfgdata_t *)cfgdata;
section[0] = '\0';
/* if we already have a file loaded, free it first */
if (valid(cd))
{
free_all_the_stuff(cd);
}
line = strtok(conf_str, "\n");
while (line)
{
if (strlen(line) <= CONF_LINE_MAX_LEN)
strcpy(str, line);
else
return -1;
trim(str); /* trim all the blanks or linefeeds(换行) */
/* skip all comment lines or empty lines */
if (!str[0] || str[0] == ';' || str[0] == '/' || str[0] == '#')
{
line = strtok(NULL, "\n");
continue;
}
length = strlen(str);
/* check if this is a section line (e.g., [SECTION]) */
if (str[0] == '[' && str[length - 1] == ']')
{
strcpy(section, &str[1]);
section[length - 2] = 0; /* remove the ] */
trim(section); /* trim section name after removing [] */
}
else
{
/* 配置项是名值对,中间用一个或多个空格或垂直制表符间隔 */
if (((p = strchr(str, '=')) != NULL) || ((p = strchr(str, ' ')) != NULL) || ((p = strchr(str, '\t')) != NULL))
{
*(p++) = '\0';
trim(str); /* str指向key name */
trim(p); /* p指向key value */
set_key_value(cd,section, str, p);
}
}
line = strtok(NULL, "\n");
}
return 0;
}
int save_conf(void *cfgdata,const char *filename)
{
int i,j;
cfgdata_t *cd = (cfgdata_t *)cfgdata;
FILE *fp = fopen(filename, "w");
if (fp == NULL)
{
return -1;
}
for (i = 0; i < cd->section_count; i++)
{
fprintf(fp, "[%s]\n", cd->psection[i].section_name); /* write the section name */
/* write all the keys... */
for (j = 0; j < cd->psection[i].key_count; j++)
{
fprintf(fp, "%s %s\n", cd->psection[i].keys[j].key_name, cd->psection[i].keys[j].key_value);
}
fprintf(fp, "\n"); /* add a line feed */
}
fclose(fp); /* save completed; close the file */
return 0;
}
/* 释放配置模块中的内存资源*/
void free_conf(void *cfgdata)
{
cfgdata_t *cd = (cfgdata_t *)cfgdata;
free_all_the_stuff(cd);
#ifdef WITH_HASH
free(cd->m_Hash);
#endif
xfree(cd);
}
const char * get_key_value(void *cfgdata,const char *section, const char *key_name, const char *def)
{
conf_section_t *psect = NULL;
int i;
#ifdef WITH_HASH
unsigned short vhash;
#endif
cfgdata_t *cd = (cfgdata_t *)cfgdata;
#ifdef WITH_HASH
/* search for the section name... */
/* first search in the hash table... */
vhash = get_hash_value(section) % CONF_HASH_SECTION_SIZE;
if (cd->m_Hash[vhash] != NULL && strcasecmp(section, cd->m_Hash[vhash]->section_name) == 0)
{
psect = cd->m_Hash[vhash]; /* found this section in the hash table */
}
#endif
if (psect == NULL)
{
/* Not found in the hash table, do a normal search... */
for (i = 0; i < cd->section_count; i++)
{
if (strcasecmp(cd->psection[i].section_name, section) == 0)
{
psect = &cd->psection[i];
break;
}
}
}
if (psect != NULL)
{
#ifdef WITH_HASH
/* section found, search for the key in this section... */
vhash = get_hash_value(key_name) % CONF_HASH_KEY_SIZE;
if (psect->hash[vhash] != NULL && strcasecmp(key_name, psect->hash[vhash]->key_name) == 0)
{
return psect->hash[vhash]->key_value;
}
#endif
/* not found in the hash table, do a normal linear search... */
for (i = 0; i < psect->key_count; i++)
{
if (strcasecmp(psect->keys[i].key_name, key_name) == 0)
{
return psect->keys[i].key_value;
}
}
}
return def; /* key is not found; use default value */
}
/* set the value in conf file */
/* 该函数把<setcion_name,key_name,key_value>写到内存中配置数据
数据结构中,如果section_name,key_name不存在,则会创建一个节、一个键并设置值*/
int set_key_value(void *cfgdata,const char *section, const char *key_name, const char *set)
{
conf_section_t *psect = NULL;
conf_key_t *pkey = NULL;
int i;
#ifdef WITH_HASH
unsigned short vhash;
#endif
cfgdata_t *cd = (cfgdata_t *)cfgdata;
#ifdef WITH_HASH
/* search if this section already exists... */
/* first search in the hash table... */
vhash = get_hash_value(section) % CONF_HASH_SECTION_SIZE;
if (cd->m_Hash[vhash] != NULL && strcasecmp(section, cd->m_Hash[vhash]->section_name) == 0)
{
psect = cd->m_Hash[vhash]; /* found this section in the hash table */
}
#endif
if (psect == NULL)
{
/* not found in the hash table, do a normal search... */
for (i = 0; i < cd->section_count; i++)
{
if (strcasecmp(cd->psection[i].section_name, section) == 0)
{
psect = &cd->psection[i];
break;
}
}
}
/* if this is a new section, try to allocate memory for it */
if (psect == NULL)
{
cd->section_count++;
/* if we don't have enough room for this new section, try to allocate more memory */
if (cd->section_count > cd->current_size)
{
cd->current_size += CONF_SIZE_INCREMENT;
if (cd->psection)
{
cd->psection = (conf_section_t *)realloc(cd->psection, sizeof(conf_section_t) * cd->current_size);
}
else
{
cd->psection = (conf_section_t *)malloc(sizeof(conf_section_t) * cd->current_size);
}
if (!cd->psection)
{
return -1;
}
}
psect = &cd->psection[cd->section_count - 1];
psect->section_name = strdup(section);
psect->keys = NULL;
psect->key_count = 0;
psect->current_size = 0;
#ifdef WITH_HASH
memset(psect->hash, 0, sizeof(psect->hash)); /* zero out the hash table */
/* store this new section in the hash table... */
vhash = get_hash_value(psect->section_name) % CONF_HASH_SECTION_SIZE;
cd->m_Hash[vhash] = psect;
#endif
}
#ifdef WITH_HASH
/* search if the key is already in the section... */
vhash = get_hash_value(key_name) % CONF_HASH_KEY_SIZE;
if (psect->hash[vhash] != NULL && strcasecmp(key_name, psect->hash[vhash]->key_name) == 0)
{
pkey = psect->hash[vhash]; /* we have found the key in the hash table */
}
#endif
if (pkey == NULL)
{
/* key is not found in the hash table, do a normal search... */
for (i = 0; i < psect->key_count; i++)
{
if (strcasecmp(key_name, psect->keys[i].key_name) == 0)
{
/* we have found the value */
pkey = &psect->keys[i];
break;
}
}
}
if (pkey != NULL)
{
/* this key already exists in the section... */
free(pkey->key_value);
pkey->key_value = strdup(set);
if (pkey->key_value == NULL)
{
return -1;
}
}
else
{
/* this is a new key... */
psect->key_count++;
/* if we don't have enough room for this new key, try to allocate more memory */
if (psect->key_count > psect->current_size)
{
psect->current_size += CONF_SIZE_INCREMENT;
if (psect->keys)
{
psect->keys = (conf_key_t *)realloc(psect->keys, sizeof(conf_key_t) * psect->current_size);
}
else
{
psect->keys = (conf_key_t *)malloc(sizeof(conf_key_t) * psect->current_size);
}
if (psect->keys == NULL)
{
return -1;
}
}
psect->keys[psect->key_count - 1].key_name = strdup(key_name);
psect->keys[psect->key_count - 1].key_value = strdup(set);
if (psect->keys[psect->key_count - 1].key_value == NULL || psect->keys[psect->key_count - 1].key_name == NULL)
{
return -1;
}
trim(psect->keys[psect->key_count - 1].key_value);
trim(psect->keys[psect->key_count - 1].key_name);
#ifdef WITH_HASH
/* store this new key in the hash table */
vhash = get_hash_value(psect->keys[psect->key_count - 1].key_name) % CONF_HASH_KEY_SIZE;
psect->hash[vhash] = &psect->keys[psect->key_count - 1];
#endif
}
return 0;
}
#ifdef WITH_HASH
/**
* This hash function has been taken from an Article in Dr Dobbs Journal.
* This is normally a collision-free function, distributing keys evenly.
* Collision can be avoided by comparing the key itself in last resort.
*/
static unsigned short get_hash_value(const char *sz)
{
unsigned short hash = 0;
char a;
while (*sz)
{
/* convert all the characters to be upper case, so that it will be case insensitive */
a = *sz;
if (a >= 'a' && a <= 'z')
{
a -= 'a' - 'A';
}
hash += (unsigned short)a;
hash += (hash << 10);
hash ^= (hash >> 6);
sz++;
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
#endif
static void free_all_the_stuff(cfgdata_t *cd)
{
int i, j;
if (cd->psection)
{
/* free all the memory allocated for this conf file */
for (i = 0; i < cd->section_count; i++)
{
/* delete all the keys in this section... */
for (j = 0; j < cd->psection[i].key_count; j++)
{
free(cd->psection[i].keys[j].key_value);
free(cd->psection[i].keys[j].key_name);
}
free(cd->psection[i].keys);
free(cd->psection[i].section_name);
}
free(cd->psection);
cd->psection = NULL;
}
#ifdef WITH_HASH
/* null out the hash table */
memset(cd->m_Hash, 0, sizeof(conf_section_t *) * CONF_HASH_SECTION_SIZE);
#endif
}
static int valid(cfgdata_t *cd)
{
return (cd->psection != NULL);
}
// min=max=0 表示大小没有限制。notfound也是默认值
int getconfint(void *cfgdata,const char *section, const char *key, int isnull, int notfound, int min, int max)
{
const char *cp;
int v;
cp = get_key_value(cfgdata,section, key, NULL);
if (!cp && !isnull)
{
printf("Config Item: [%s] %s: not set\n", section, key);
exit(1);
}
else if (!cp && isnull)
return notfound;
else
{
v = atoi(cp);
if (max && (v < min || v > max))
{
printf("Config Item: [%s] %s: set error\n", section, key);
exit(1);
}
return v;
}
}
double getconfloat(void *cfgdata,const char *section, const char *key, int isnull, double notfound, double min, double max)
{
const char *cp;
double v;
cp = get_key_value(cfgdata,section, key, NULL);
if (!cp && !isnull)
{
printf("Config Item: [%s] %s: not set\n", section, key);
exit(1);
}
else if (!cp && isnull)
return notfound;
else
{
v = atof(cp);
if (max && (v < min || v > max))
{
printf("Config Item: [%s] %s: set error\n", section, key);
exit(1);
}
return v;
}
}
// @return: need free.
char *getconfstring(void *cfgdata,const char *section, const char *key, int isnull, char *notfound,
char **values, int *ivalues, int valnum, int *retival)
{
const char *cp;
int i;
cp = get_key_value(cfgdata,section, key, NULL);
if (!cp && !isnull)
{
printf("Config Item: [%s] %s: not set\n", section, key);
exit(1);
}
else if (!cp && isnull)
{
if(notfound) {
cp = notfound;
goto process;
} else
return notfound;
}
else
{
process:
if (valnum == 0)
return xstrdup(cp);
for (i = 0;i < valnum;++i)
{
if (strcasecmp(cp, values[i]) == 0)
break;
}
if (i == valnum)
{
printf("Config Item: [%s] %s: set error\n", section, key);
exit(1);
}
if (ivalues && retival)
{
*retival = ivalues[i];
return NULL;
}
else if (ivalues && !retival)
{
printf("return value variable pointer isn't set.\n");
exit(1);
}
else
return xstrdup(cp);
}
}
// @return: need free.
char *getconfmulti(void *cfgdata,const char *section, const char *key, int isnull, char *notfound,
const char *sepstr, char ***values, int *valnum)
{
const char *cp;
char *v, **vv;
int i, n, len;
cp = get_key_value(cfgdata,section, key, NULL);
if (!cp && !isnull)
{
printf("Config Item: [%s] %s: not set\n", section, key);
exit(1);
}
else if (!cp && isnull)
{
if(notfound) {
cp = notfound;
goto process;
}
else {
*values = NULL;
*valnum = 0;
return notfound;
}
}
else
{
process:
len = strlen(cp);
v = xmalloc(len + 1);
strcpy(v,cp);
strsqueeze(v);
//trimid(cp, v, 1); 注意: 只能用于ascii和gbk
n = strfragnum(v, sepstr);
vv = (char **)xcalloc(n, sizeof(char *));
split(v, sepstr, vv, n);
for (i = 0;i < n;++i)
{
trim(vv[i]);
if (*vv[i] == 0)
{
printf("Config Item: [%s] %s: set error\n", section, key);
xfree(v);
xfree(vv);
exit(1);
}
}
*values = vv;
*valnum = n;
return v;
}
}
// @return: need free.
char *getconfdir(void *cfgdata,const char *section, const char *key, int isnull, char *notfound)
{
const char *cp;
char *v;
cp = get_key_value(cfgdata,section, key, NULL);
if (!cp && !isnull)
{
printf("Config Item: [%s] %s: not set\n", section, key);
exit(1);
}
else if (!cp && isnull)
{
if(notfound) {
cp = notfound;
goto process;
}
else
return notfound;
}
else
{
process:
v = xstrdup(cp);
if(v[strlen(v)-1] == '/')
v[strlen(v)-1] = 0;
return v;
}
}
// @return: need free.
char *getconfpath(void *cfgdata,const char *section, const char *key, int isnull, char *notfound,char *home,char *dir)
{
const char *cp;
char *v,path[1024];
cp = get_key_value(cfgdata,section, key, NULL);
if (!cp && !isnull)
{
printf("Config Item: [%s] %s: not set\n", section, key);
exit(1);
}
else if (!cp && isnull) {
if(notfound) {
cp = notfound;
goto process;
}
else
return notfound;
}
else
{
process:
if(cp[0] != '/')
{
path[0] = 0;
if (home) { strcpy(path, home); strcat(path,"/");}
if (dir) { strcat(path, dir); strcat(path,"/");}
strcat(path, cp);
v = xstrdup(path);
}
else
{
v = xstrdup(cp);
}
return v;
}
}
void get_conf(void *cfgdata,conf_t *conf,int confnum)
{
int i;
for(i=0;i<confnum;i++)
{
switch(conf[i].type)
{
case CONF_INT:
*(int *)conf[i].r = getconfint(cfgdata,conf[i].section,conf[i].key,conf[i].isnull,conf[i].defval?atoi(conf[i].defval):0,
(int)conf[i].p1,(int)conf[i].p2);
break;
case CONF_STR:
if((int)conf[i].p3) // 返回整数
getconfstring(cfgdata,conf[i].section,conf[i].key,conf[i].isnull,conf[i].defval,(char **)conf[i].p1,(int *)conf[i].p3,(int)conf[i].p2,(int *)conf[i].r);
else // 返回字符串
*(char **)conf[i].r = getconfstring(cfgdata,conf[i].section,conf[i].key,conf[i].isnull,conf[i].defval,(char **)conf[i].p1,NULL,(int)conf[i].p2,NULL);
break;
case CONF_FLOAT:
*(double *)conf[i].r = getconfloat(cfgdata,conf[i].section,conf[i].key,conf[i].isnull,conf[i].defval?atof(conf[i].defval):0.0,
(double)(int)conf[i].p1,(double)(int)conf[i].p2);
break;
case CONF_MULTI:
*(char **)conf[i].r = getconfmulti(cfgdata,conf[i].section,conf[i].key,conf[i].isnull,conf[i].defval,
(char *)(conf[i].p2),(char ***)(conf[i].p1),(int *)(conf[i].p3));
break;
case CONF_DIR:
*(char **)conf[i].r = getconfdir(cfgdata,conf[i].section,conf[i].key,conf[i].isnull,conf[i].defval);
break;
case CONF_PATH:
*(char **)conf[i].r = getconfpath(cfgdata,conf[i].section,conf[i].key,conf[i].isnull,conf[i].defval,
(char *)conf[i].p1,(char *)conf[i].p2);
break;
}
}
}
#ifdef TEST_CONF
int main()
{
char **value;
int valnum;
int i;
void *cfgdata;
cfgdata = init_conf();
if(load_conf(cfgdata,"../../etc/dp.conf") < 0)
{
printf("load configuration file failure.\n");
return -1;
}
const char *weekday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
printf("[test] int: %d\n",getconfint(cfgdata,"test","int",0,1,2,6));
printf("[test] float: %.2f\n",getconfloat(cfgdata,"test","float",0,1.0,1.0,999.99));
printf("[test] string: %s\n",getconfstring(cfgdata,"test","string",0,"not fount",weekday,NULL,7,NULL));
printf("[test] multi-value: \n");
getconfmulti(cfgdata,"test","multi",0,"not found"," ",&value,&valnum);
for(i=0;i<valnum;i++)
printf("%s ",value[i]);
printf("\n");
free_conf(cfgdata);
}
#endif