-
Notifications
You must be signed in to change notification settings - Fork 42
/
config.c
100 lines (87 loc) · 3.02 KB
/
config.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
// Helper functions for iniparser that combine section:key
// April 2022, Phil Karn, KA9Q
// Copyright 2022-2023, Phil Karn, KA9Q
#include <iniparser/iniparser.h>
#include <stdlib.h>
#include "config.h"
int config_getint(dictionary const *d,char const *section,char const *key,int def){
if(section == NULL || key == NULL)
return def;
char buf[1024];
snprintf(buf,sizeof(buf),"%s:%s",section,key);
return iniparser_getint(d,buf,def);
}
float config_getfloat(dictionary const *d,char const *section,char const *key,float def){
if(section == NULL || key == NULL)
return def;
char buf[1024];
snprintf(buf,sizeof(buf),"%s:%s",section,key);
return (float)iniparser_getdouble(d,buf,def);
}
double config_getdouble(dictionary const *d,char const *section,char const *key,double def){
if(section == NULL || key == NULL)
return def;
char buf[1024];
snprintf(buf,sizeof(buf),"%s:%s",section,key);
return iniparser_getdouble(d,buf,def);
}
int config_getboolean(dictionary const *d,char const *section,char const *key,int def){
if(section == NULL || key == NULL)
return def;
char buf[1024];
snprintf(buf,sizeof(buf),"%s:%s",section,key);
return iniparser_getboolean(d,buf,def);
}
char const *config_getstring(dictionary const *d,char const *section,char const *key,char const *def){
if(section == NULL || key == NULL)
return def;
char buf[1024];
snprintf(buf,sizeof(buf),"%s:%s",section,key);
return iniparser_getstring(d,buf,def);
}
// Look in dictionary d2 first, fall back to d1 if not found
char const *config2_getstring(dictionary const *d1,dictionary const *d2,char const *sec1,char const *sec2,char const *key,char const *def){
char const *cp = config_getstring(d2,sec2,key,NULL);
if(cp)
return cp;
cp = config_getstring(d1,sec1,key,NULL);
if(cp)
return cp;
return def;
}
int config2_getint(dictionary const *d1,dictionary const *d2,char const *sec1,char const *sec2,char const *key,int def){
char const *cp = config_getstring(d2,sec2,key,NULL);
if(cp)
return strtol(cp,NULL,0);
cp = config_getstring(d1,sec1,key,NULL);
if(cp)
return strtol(cp,NULL,0);
return def;
}
float config2_getfloat(dictionary const *d1,dictionary const *d2,char const *sec1,char const *sec2,char const *key,float def){
char const *cp = config_getstring(d2,sec2,key,NULL);
if(cp)
return strtof(cp,0);
cp = config_getstring(d1,sec1,key,NULL);
if(cp)
return strtof(cp,0);
return def;
}
double config2_getdouble(dictionary const *d1,dictionary const *d2,char const *sec1,char const *sec2,char const *key,double def){
char const *cp = config_getstring(d2,sec2,key,NULL);
if(cp)
return strtof(cp,0);
cp = config_getstring(d1,sec1,key,NULL);
if(cp)
return strtof(cp,0);
return def;
}
int config2_getboolean(dictionary const *d1,dictionary const *d2,char const *sec1,char const *sec2,char const *key,int def){
int x = config_getboolean(d2,sec2,key,-1);
if(x != -1)
return x;
x = config_getboolean(d1,sec1,key,-1);
if(x != -1)
return x;
return def;
}