Repository navigation
Expand file tree
/
Copy pathjson_conf.c
More file actions
190 lines (166 loc) · 3.33 KB
/
Copy pathjson_conf.c
File metadata and controls
190 lines (166 loc) · 3.33 KB
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
/** \cond 0 */
#include <stdio.h>
#include <stdint.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>
#include "cJSON.h"
#include <stdlib.h>
/** \endcond */
#include "json_conf.h"
static cJSON *process_json(cJSON *conf, int level);
static cJSON *conf_load_simple(const char *filename)
{
FILE *fp;
cJSON *conf;
fp = fopen(filename, "r");
if (fp == NULL) {
//log error
fprintf(stderr, "fopen(): %m");
return NULL;
}
conf = cJSON_fParse(fp);
fclose(fp);
return conf;
}
static cJSON *process_array(cJSON *conf, int level)
{
cJSON *after, *curr;
for (curr=conf->child; curr!=NULL; curr=curr->next) {
after = process_json(curr, level);
if (after!=curr) {
if (curr->prev==NULL) {
conf->child = after;
}
curr = after;
}
}
return conf;
}
static cJSON *process_json(cJSON *conf, int level)
{
cJSON *load, *fname, *result;
if (conf==NULL) {
return NULL;
}
if (level>=MAX_LOAD_DEPTH) {
fprintf(stderr, "Followed \""LOAD_KEYWORD"\" more than %d levels, there might be a loop.\n", MAX_LOAD_DEPTH);
return conf;
}
switch (conf->type) {
case cJSON_Array:
return process_array(conf, level);
break;
case cJSON_Object:
fname = cJSON_GetObjectItem(conf, LOAD_KEYWORD);
if (fname==NULL || fname->type != cJSON_String) {
return process_array(conf, level);
}
load = conf_load_simple(fname->valuestring);
if (load==NULL) {
fprintf(stderr, "Load %s failed.\n", fname->valuestring);
abort();
}
load->next = conf->next;
if (conf->next) {
conf->next->prev = load;
}
load->prev = conf->prev;
if (conf->prev) {
conf->prev->next = load;
}
if (conf->string) {
load->string = strdup(conf->string); // FIXME: Should alloc memory by cJSON_malloc().
} else {
load->string = NULL;
}
result = process_json(load, level+1);
//cJSON_Delete(conf);
return result;
break;
case cJSON_String:
case cJSON_Number:
case cJSON_NULL:
case cJSON_True:
case cJSON_False:
return conf;
break;
default:
fprintf(stderr, "Unknown type: %d\n", conf->type);
abort();
return NULL;
break;
}
}
cJSON *conf_load(const char *filename)
{
cJSON *conf ;
conf = conf_load_simple(filename);
if (conf==NULL) {
return NULL;
}
return process_json(conf, 1);
}
cJSON *conf_get(const char *name, cJSON *c)
{
return cJSON_GetObjectItem(c, name);
}
int *conf_get_int(const char *name, cJSON *c)
{
cJSON *tmp;
tmp = cJSON_GetObjectItem(c, name);
if (tmp) {
return &(tmp->valueint);
}
return NULL;
}
double *conf_get_double(const char *name, cJSON *c)
{
cJSON *tmp;
tmp = cJSON_GetObjectItem(c, name);
if (tmp) {
return &(tmp->valuedouble);
}
return NULL;
}
const int *conf_get_bool(const char *name, cJSON *c)
{
cJSON *tmp;
static const int true=1, false=0;
tmp = cJSON_GetObjectItem(c, name);
if (tmp==NULL) {
return NULL;
}
if (tmp->type == cJSON_True) {
return &true;
}
if (tmp->type == cJSON_False) {
return &false;
}
return NULL;
}
char *conf_get_str(const char *name, cJSON *c)
{
cJSON *tmp;
tmp = cJSON_GetObjectItem(c, name);
if (tmp) {
return tmp->valuestring;
}
return NULL;
}
int conf_delete(cJSON *conf)
{
cJSON_Delete(conf);
return 0;
}
#if UTEST
int main(int argc, char **argv)
{
cJSON *conf;
conf = conf_load(argv[1]);
printf("Conf Loaded:\n");
cJSON_fPrint(stderr, conf);
conf_delete(conf);
return 0;
}
#endif