@@ -42,6 +42,8 @@ static config_entry_type_t config_determine_value_type(char *s);
42
42
static config_error_t config_entry_create (struct config_entry * * e , const char * name );
43
43
static config_error_t config_entry_delete (struct config_entry * e );
44
44
45
+ static config_t default_conf ;
46
+
45
47
config_t config_init (void )
46
48
{
47
49
struct config * d = malloc (sizeof * d );
@@ -52,22 +54,45 @@ config_t config_init(void)
52
54
53
55
config_error_t config_clear (config_t d )
54
56
{
57
+ if (!d && default_conf )
58
+ d = default_conf ;
59
+ else if (!d && !default_conf )
60
+ return CONFIG_ERROR_NEXISTS ;
61
+
55
62
for (int i = 0 ; i < d -> length ; i ++ )
56
63
config_remove_entry (d , d -> data [i ]-> name );
57
64
return CONFIG_SUCCESS ;
58
65
}
59
66
60
67
config_error_t config_dispose (config_t d )
61
68
{
69
+ if (!d && default_conf )
70
+ d = default_conf ;
71
+ else if (!d && !default_conf )
72
+ return CONFIG_ERROR_NEXISTS ;
73
+
62
74
config_clear (d );
63
75
free (d -> data );
64
76
free (d -> filename );
65
77
free (d );
66
78
return CONFIG_SUCCESS ;
67
79
}
68
80
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
+
69
89
config_error_t config_set_filename (config_t d , const char * name )
70
90
{
91
+ if (!d && default_conf )
92
+ d = default_conf ;
93
+ else if (!d && !default_conf )
94
+ return CONFIG_ERROR_NEXISTS ;
95
+
71
96
d -> filename = malloc (sizeof * d -> filename * strlen (name ) + 1 );
72
97
if (!d -> filename )
73
98
return CONFIG_ERROR_MALLOC ;
@@ -77,6 +102,11 @@ config_error_t config_set_filename(config_t d, const char *name)
77
102
78
103
config_error_t config_read_file (config_t d , const char * filename ) //NOTDONE
79
104
{
105
+ if (!d && default_conf )
106
+ d = default_conf ;
107
+ else if (!d && !default_conf )
108
+ return CONFIG_ERROR_NEXISTS ;
109
+
80
110
char line [MAX_LINE ];
81
111
char * lhs , * rhs , * p ;
82
112
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
127
157
128
158
config_error_t config_write_file (config_t d , const char * filename )
129
159
{
160
+ if (!d && default_conf )
161
+ d = default_conf ;
162
+ else if (!d && !default_conf )
163
+ return CONFIG_ERROR_NEXISTS ;
164
+
130
165
FILE * file = fopen ((filename ? filename : d -> filename ), "w+" );
131
166
if (!file )
132
167
return CONFIG_ERROR_IO ;
@@ -155,6 +190,11 @@ config_error_t config_write_file(config_t d, const char* filename)
155
190
156
191
config_error_t config_add_entry (config_t d , const char * name )
157
192
{
193
+ if (!d && default_conf )
194
+ d = default_conf ;
195
+ else if (!d && !default_conf )
196
+ return CONFIG_ERROR_NEXISTS ;
197
+
158
198
if ( d -> length > 0 && d -> length % CHUNK_SIZE == 0 ){
159
199
struct config_entry * * new = realloc (d -> data , (d -> length + CHUNK_SIZE ) * sizeof * d -> data );
160
200
if (!new )
@@ -171,6 +211,11 @@ config_error_t config_add_entry(config_t d, const char *name)
171
211
172
212
config_error_t config_remove_entry (config_t d , const char * name )
173
213
{
214
+ if (!d && default_conf )
215
+ d = default_conf ;
216
+ else if (!d && !default_conf )
217
+ return CONFIG_ERROR_NEXISTS ;
218
+
174
219
size_t index = 0 ;
175
220
struct config_entry * e = config_lookup_entry (d , name , & index );
176
221
if (!e )
@@ -185,6 +230,11 @@ config_error_t config_remove_entry(config_t d, const char *name)
185
230
186
231
config_error_t config_entry_set_int (config_t d , const char * name , int i )
187
232
{
233
+ if (!d && default_conf )
234
+ d = default_conf ;
235
+ else if (!d && !default_conf )
236
+ return CONFIG_ERROR_NEXISTS ;
237
+
188
238
struct config_entry * e = config_lookup_entry (d , name , NULL );
189
239
if (!e )
190
240
return CONFIG_ERROR_NEXISTS ;
@@ -199,6 +249,11 @@ config_error_t config_entry_set_int(config_t d, const char *name, int i)
199
249
200
250
config_error_t config_entry_set_double (config_t d , const char * name , double f )
201
251
{
252
+ if (!d && default_conf )
253
+ d = default_conf ;
254
+ else if (!d && !default_conf )
255
+ return CONFIG_ERROR_NEXISTS ;
256
+
202
257
struct config_entry * e = config_lookup_entry (d , name , NULL );
203
258
if (!e )
204
259
return CONFIG_ERROR_NEXISTS ;
@@ -213,6 +268,11 @@ config_error_t config_entry_set_double(config_t d, const char *name, double f)
213
268
214
269
config_error_t config_entry_set_bool (config_t d , const char * name , int i )
215
270
{
271
+ if (!d && default_conf )
272
+ d = default_conf ;
273
+ else if (!d && !default_conf )
274
+ return CONFIG_ERROR_NEXISTS ;
275
+
216
276
struct config_entry * e = config_lookup_entry (d , name , NULL );
217
277
if (!e )
218
278
return CONFIG_ERROR_NEXISTS ;
@@ -227,6 +287,11 @@ config_error_t config_entry_set_bool(config_t d, const char *name, int i)
227
287
228
288
config_error_t config_entry_set_string (config_t d , const char * name , const char * s )
229
289
{
290
+ if (!d && default_conf )
291
+ d = default_conf ;
292
+ else if (!d && !default_conf )
293
+ return CONFIG_ERROR_NEXISTS ;
294
+
230
295
struct config_entry * e = config_lookup_entry (d , name , NULL );
231
296
if (!e )
232
297
return CONFIG_ERROR_NEXISTS ;
@@ -247,48 +312,64 @@ config_error_t config_entry_set_string(config_t d, const char *name, const char
247
312
return CONFIG_SUCCESS ;
248
313
}
249
314
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 )
251
316
{
317
+ if (!d && default_conf )
318
+ d = default_conf ;
319
+ else if (!d && !default_conf )
320
+ return 0 ;
321
+
252
322
struct config_entry * e = config_lookup_entry (d , name , NULL );
253
323
if (!e )
254
- return CONFIG_ERROR_NEXISTS ;
324
+ return 0 ;
255
325
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 ;
259
328
}
260
329
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 )
262
331
{
332
+ if (!d && default_conf )
333
+ d = default_conf ;
334
+ else if (!d && !default_conf )
335
+ return CONFIG_ERROR_NEXISTS ;
336
+
263
337
struct config_entry * e = config_lookup_entry (d , name , NULL );
264
338
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 ;
270
343
}
271
344
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 )
273
346
{
347
+ if (!d && default_conf )
348
+ d = default_conf ;
349
+ else if (!d && !default_conf )
350
+ return CONFIG_ERROR_NEXISTS ;
351
+
274
352
struct config_entry * e = config_lookup_entry (d , name , NULL );
275
353
if (!e )
276
- return CONFIG_ERROR_NEXISTS ;
354
+ return 0 ;
277
355
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 ;
281
358
}
282
359
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 )
284
361
{
362
+ if (!d && default_conf )
363
+ d = default_conf ;
364
+ else if (!d && !default_conf )
365
+ return CONFIG_ERROR_NEXISTS ;
366
+
285
367
struct config_entry * e = config_lookup_entry (d , name , NULL );
286
368
if (!e )
287
- return CONFIG_ERROR_NEXISTS ;
369
+ return NULL ;
288
370
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 ;
292
373
}
293
374
294
375
static config_error_t config_entry_delete (struct config_entry * e )
0 commit comments