Skip to content

Commit e01b440

Browse files
committed
Don't return errors. Add two macros.
1 parent aac19c9 commit e01b440

File tree

3 files changed

+118
-46
lines changed

3 files changed

+118
-46
lines changed

include/libleaconfig/leaconfig.h

+9-5
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ typedef enum
2929

3030
typedef struct config *config_t;
3131

32+
#define CFG(NAME, TYPE) config_entry_get_##TYPE(NULL,NAME)
33+
#define CFGSET(NAME, VALUE, TYPE) config_entry_set_##TYPE(NULL, NAME, VALUE)
34+
3235
config_t config_init(void);
3336
config_error_t config_clear(config_t d);
3437
config_error_t config_dispose(config_t d);
35-
config_error_t config_set_filename(config_t d, const char *name);
38+
config_error_t config_set_config(config_t d);
39+
config_error_t config_set_filename(config_t d, const char *filename);
3640
config_error_t config_read_file(config_t d, const char *filename);
3741
config_error_t config_write_file(config_t d, const char *filename);
3842
config_error_t config_add_entry(config_t d, const char *name);
@@ -41,9 +45,9 @@ config_error_t config_entry_set_int(config_t d, const char *name, int i);
4145
config_error_t config_entry_set_double(config_t d, const char *name, double f);
4246
config_error_t config_entry_set_bool(config_t d, const char *name, int i);
4347
config_error_t config_entry_set_string(config_t d, const char *name, const char *s);
44-
config_error_t config_entry_get_int(config_t d, const char *name, int *i);
45-
config_error_t config_entry_get_double(config_t d, const char *name, double *f);
46-
config_error_t config_entry_get_bool(config_t d, const char *name, int *i);
47-
config_error_t config_entry_get_string(config_t d, const char *name, const char **s);
48+
int config_entry_get_int(config_t d, const char *name);
49+
double config_entry_get_double(config_t d, const char *name);
50+
int config_entry_get_bool(config_t d, const char *name);
51+
const char *config_entry_get_string(config_t d, const char *name);
4852

4953
#endif /* LIBLEACONFIG_LEACONFIG_H */

libleaconfig/leaconfig.c

+102-21
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ static config_entry_type_t config_determine_value_type(char *s);
4242
static config_error_t config_entry_create(struct config_entry **e, const char *name);
4343
static config_error_t config_entry_delete(struct config_entry *e);
4444

45+
static config_t default_conf;
46+
4547
config_t config_init(void)
4648
{
4749
struct config *d = malloc(sizeof *d);
@@ -52,22 +54,45 @@ config_t config_init(void)
5254

5355
config_error_t config_clear(config_t d)
5456
{
57+
if(!d && default_conf)
58+
d = default_conf;
59+
else if(!d && !default_conf)
60+
return CONFIG_ERROR_NEXISTS;
61+
5562
for(int i = 0; i < d->length; i++)
5663
config_remove_entry(d, d->data[i]->name);
5764
return CONFIG_SUCCESS;
5865
}
5966

6067
config_error_t config_dispose(config_t d)
6168
{
69+
if(!d && default_conf)
70+
d = default_conf;
71+
else if(!d && !default_conf)
72+
return CONFIG_ERROR_NEXISTS;
73+
6274
config_clear(d);
6375
free(d->data);
6476
free(d->filename);
6577
free(d);
6678
return CONFIG_SUCCESS;
6779
}
6880

81+
config_error_t config_set_config(config_t d)
82+
{
83+
if(!d)
84+
return CONFIG_ERROR_NEXISTS;
85+
default_conf = d;
86+
return CONFIG_SUCCESS;
87+
}
88+
6989
config_error_t config_set_filename(config_t d, const char *name)
7090
{
91+
if(!d && default_conf)
92+
d = default_conf;
93+
else if(!d && !default_conf)
94+
return CONFIG_ERROR_NEXISTS;
95+
7196
d->filename = malloc(sizeof *d->filename * strlen(name) + 1);
7297
if(!d->filename)
7398
return CONFIG_ERROR_MALLOC;
@@ -77,6 +102,11 @@ config_error_t config_set_filename(config_t d, const char *name)
77102

78103
config_error_t config_read_file(config_t d, const char *filename) //NOTDONE
79104
{
105+
if(!d && default_conf)
106+
d = default_conf;
107+
else if(!d && !default_conf)
108+
return CONFIG_ERROR_NEXISTS;
109+
80110
char line[MAX_LINE];
81111
char *lhs, *rhs, *p;
82112
FILE *file = fopen((filename ? filename : d->filename), "r");
@@ -127,6 +157,11 @@ config_error_t config_read_file(config_t d, const char *filename) //NOTDONE
127157

128158
config_error_t config_write_file(config_t d, const char* filename)
129159
{
160+
if(!d && default_conf)
161+
d = default_conf;
162+
else if(!d && !default_conf)
163+
return CONFIG_ERROR_NEXISTS;
164+
130165
FILE *file = fopen((filename ? filename : d->filename), "w+");
131166
if(!file)
132167
return CONFIG_ERROR_IO;
@@ -155,6 +190,11 @@ config_error_t config_write_file(config_t d, const char* filename)
155190

156191
config_error_t config_add_entry(config_t d, const char *name)
157192
{
193+
if(!d && default_conf)
194+
d = default_conf;
195+
else if(!d && !default_conf)
196+
return CONFIG_ERROR_NEXISTS;
197+
158198
if( d->length > 0 && d->length % CHUNK_SIZE == 0){
159199
struct config_entry **new = realloc(d->data, (d->length + CHUNK_SIZE) * sizeof *d->data);
160200
if(!new)
@@ -171,6 +211,11 @@ config_error_t config_add_entry(config_t d, const char *name)
171211

172212
config_error_t config_remove_entry(config_t d, const char *name)
173213
{
214+
if(!d && default_conf)
215+
d = default_conf;
216+
else if(!d && !default_conf)
217+
return CONFIG_ERROR_NEXISTS;
218+
174219
size_t index = 0;
175220
struct config_entry *e = config_lookup_entry(d, name, &index);
176221
if(!e)
@@ -185,6 +230,11 @@ config_error_t config_remove_entry(config_t d, const char *name)
185230

186231
config_error_t config_entry_set_int(config_t d, const char *name, int i)
187232
{
233+
if(!d && default_conf)
234+
d = default_conf;
235+
else if(!d && !default_conf)
236+
return CONFIG_ERROR_NEXISTS;
237+
188238
struct config_entry *e = config_lookup_entry(d, name, NULL);
189239
if(!e)
190240
return CONFIG_ERROR_NEXISTS;
@@ -199,6 +249,11 @@ config_error_t config_entry_set_int(config_t d, const char *name, int i)
199249

200250
config_error_t config_entry_set_double(config_t d, const char *name, double f)
201251
{
252+
if(!d && default_conf)
253+
d = default_conf;
254+
else if(!d && !default_conf)
255+
return CONFIG_ERROR_NEXISTS;
256+
202257
struct config_entry *e = config_lookup_entry(d, name, NULL);
203258
if(!e)
204259
return CONFIG_ERROR_NEXISTS;
@@ -213,6 +268,11 @@ config_error_t config_entry_set_double(config_t d, const char *name, double f)
213268

214269
config_error_t config_entry_set_bool(config_t d, const char *name, int i)
215270
{
271+
if(!d && default_conf)
272+
d = default_conf;
273+
else if(!d && !default_conf)
274+
return CONFIG_ERROR_NEXISTS;
275+
216276
struct config_entry *e = config_lookup_entry(d, name, NULL);
217277
if(!e)
218278
return CONFIG_ERROR_NEXISTS;
@@ -227,6 +287,11 @@ config_error_t config_entry_set_bool(config_t d, const char *name, int i)
227287

228288
config_error_t config_entry_set_string(config_t d, const char *name, const char *s)
229289
{
290+
if(!d && default_conf)
291+
d = default_conf;
292+
else if(!d && !default_conf)
293+
return CONFIG_ERROR_NEXISTS;
294+
230295
struct config_entry *e = config_lookup_entry(d, name, NULL);
231296
if(!e)
232297
return CONFIG_ERROR_NEXISTS;
@@ -247,48 +312,64 @@ config_error_t config_entry_set_string(config_t d, const char *name, const char
247312
return CONFIG_SUCCESS;
248313
}
249314

250-
config_error_t config_entry_get_int(config_t d, const char *name, int *i)
315+
int config_entry_get_int(config_t d, const char *name)
251316
{
317+
if(!d && default_conf)
318+
d = default_conf;
319+
else if(!d && !default_conf)
320+
return 0;
321+
252322
struct config_entry *e = config_lookup_entry(d, name, NULL);
253323
if(!e)
254-
return CONFIG_ERROR_NEXISTS;
324+
return 0;
255325
if(e->type != CONFIG_TYPE_INT)
256-
return CONFIG_ERROR_MISMATCH;
257-
*i = e->data.ival;
258-
return CONFIG_SUCCESS;
326+
return 0;
327+
return e->data.ival;
259328
}
260329

261-
config_error_t config_entry_get_double(config_t d, const char *name, double *f)
330+
double config_entry_get_double(config_t d, const char *name)
262331
{
332+
if(!d && default_conf)
333+
d = default_conf;
334+
else if(!d && !default_conf)
335+
return CONFIG_ERROR_NEXISTS;
336+
263337
struct config_entry *e = config_lookup_entry(d, name, NULL);
264338
if(!e)
265-
return CONFIG_ERROR_NEXISTS;
266-
if(e->type != CONFIG_TYPE_FLOAT)
267-
return CONFIG_ERROR_MISMATCH;
268-
*f = e->data.fval;
269-
return CONFIG_SUCCESS;
339+
return 0;
340+
if(e->type !=CONFIG_TYPE_FLOAT)
341+
return 0;
342+
return e->data.fval;
270343
}
271344

272-
config_error_t config_entry_get_bool(config_t d, const char *name, int *i)
345+
int config_entry_get_bool(config_t d, const char *name)
273346
{
347+
if(!d && default_conf)
348+
d = default_conf;
349+
else if(!d && !default_conf)
350+
return CONFIG_ERROR_NEXISTS;
351+
274352
struct config_entry *e = config_lookup_entry(d, name, NULL);
275353
if(!e)
276-
return CONFIG_ERROR_NEXISTS;
354+
return 0;
277355
if(e->type != CONFIG_TYPE_BOOL)
278-
return CONFIG_ERROR_MISMATCH;
279-
*i = e->data.ival;
280-
return CONFIG_SUCCESS;
356+
return 0;
357+
return e->data.ival;
281358
}
282359

283-
config_error_t config_entry_get_string(config_t d, const char *name, const char **s)
360+
const char *config_entry_get_string(config_t d, const char *name)
284361
{
362+
if(!d && default_conf)
363+
d = default_conf;
364+
else if(!d && !default_conf)
365+
return CONFIG_ERROR_NEXISTS;
366+
285367
struct config_entry *e = config_lookup_entry(d, name, NULL);
286368
if(!e)
287-
return CONFIG_ERROR_NEXISTS;
369+
return NULL;
288370
if(e->type != CONFIG_TYPE_STRING)
289-
return CONFIG_ERROR_MISMATCH;
290-
*s = e->data.sval;
291-
return CONFIG_SUCCESS;
371+
return NULL;
372+
return e->data.sval;
292373
}
293374

294375
static config_error_t config_entry_delete(struct config_entry *e)

src/conftest.c

+7-20
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,27 @@
99
int main(void)
1010
{
1111
config_t conf = config_init();
12-
int i = 0;
13-
double d = 0;
14-
const char *s;
1512
config_add_entry(conf, "test");
1613
config_entry_set_int(conf, "test", 155);
17-
config_entry_get_int(conf, "test", &i);
18-
printf("test=%d\n", i);
19-
i = 0;
14+
printf("test=%d\n", config_entry_get_int(conf, "test"));
2015
config_add_entry(conf, "kek");
2116
config_entry_set_string(conf, "kek", "toast");
22-
config_entry_get_string(conf, "kek", &s);
23-
printf("kek=%s\n", s);
24-
s = NULL;
17+
printf("kek=%s\n", config_entry_get_string(conf, "kek"));
2518
config_remove_entry(conf, "test");
2619
config_add_entry(conf, "test");
2720
config_entry_set_double(conf, "test", 0.1f);
28-
config_entry_get_double(conf, "test", &d);
29-
printf("test=%f\n", d);
30-
d = 0;
21+
printf("test=%f\n", config_entry_get_double(conf, "test", &d));
3122
config_write_file(conf, "tmp.conf");
3223
config_remove_entry(conf, "test");
3324
config_remove_entry(conf, "kek");
3425
config_set_filename(conf, "testconf.conf");
3526
config_read_file(conf, NULL);
36-
config_entry_get_int(conf, "ur_an_fagit", &i);
37-
printf("ur_an_fagit=%d\n", i);
38-
config_entry_get_string(conf, "potatoes_are_nice", &s);
39-
printf("potatoes_are_nice=%s\n", s);
27+
printf("ur_an_fagit=%d\n", config_entry_get_int(conf, "ur_an_fagit"));
28+
printf("potatoes_are_nice=%s\n", config_entry_get_string(conf, "potatoes_are_nice"));
4029
config_clear(conf);
4130
config_set_filename(conf, "tmp.conf");
4231
config_read_file(conf, NULL);
43-
config_entry_get_double(conf, "test", &d);
44-
printf("test=%f\n", d);
45-
config_entry_get_string(conf, "kek", &s);
46-
printf("kek=%s\n", s);
32+
printf("test=%f\n", config_entry_get_double(conf, "test"));
33+
printf("kek=%s\n", config_entry_get_string(conf, "kek"));
4734
config_dispose(conf);
4835
}

0 commit comments

Comments
 (0)