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 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/encode/inifiles.h"
#include "gate/comparers.h"
#include "gate/results.h"
static gate_flatmap_iterator_t gate_inifile_store_use_section(gate_inifile_store_t* store, gate_string_t const* section)
{
gate_flatmap_iterator_t iter = gate_flatmap_get(&store->sections, section);
gate_flatmap_t content = GATE_INIT_EMPTY;
if (!gate_flatmap_iterator_valid(&store->sections, iter))
{
if (NULL != gate_flatmap_create(&content, &gate_compare_string,
sizeof(gate_string_t), &gate_string_copy_constructor, &gate_string_destructor,
sizeof(gate_string_t), &gate_string_copy_constructor, &gate_string_destructor))
{
iter = gate_flatmap_add(&store->sections, section, &content);
if (!iter)
{
gate_flatmap_destroy(&content);
}
}
}
return iter;
}
gate_result_t gate_inifile_store_create(gate_inifile_store_t* store)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_flatmap_iterator_t global_section_entry;
static gate_string_t const empty_string = GATE_STRING_INIT_EMPTY;
do
{
gate_mem_clear(store, sizeof(gate_inifile_store_t));
if (NULL == gate_flatmap_create(&store->sections, &gate_compare_string,
sizeof(gate_string_t), &gate_string_copy_constructor, &gate_string_destructor,
sizeof(gate_flatmap_t), &gate_flatmap_copy_constructor, &gate_flatmap_destructor))
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
global_section_entry = gate_inifile_store_use_section(store, &empty_string);
if (NULL == global_section_entry)
{
gate_flatmap_destroy(&store->sections);
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
ret = GATE_RESULT_OK;
} while (0);
return ret;
}
gate_result_t gate_inifile_store_copy(gate_inifile_store_t* store, gate_inifile_store_t const* source)
{
if (NULL == gate_flatmap_copy(&store->sections, &source->sections))
{
return GATE_RESULT_OUTOFMEMORY;
}
return GATE_RESULT_OK;
}
gate_result_t gate_inifile_store_destroy(gate_inifile_store_t* store)
{
gate_result_t ret = GATE_RESULT_FAILED;
if (store)
{
gate_flatmap_destroy(&store->sections);
ret = GATE_RESULT_OK;
}
return ret;
}
gate_result_t gate_inifile_store_add_section(gate_inifile_store_t* store, gate_string_t const* section)
{
gate_flatmap_iterator_t section_entry = gate_inifile_store_use_section(store, section);
if (section_entry == NULL)
{
return GATE_RESULT_OUTOFMEMORY;
}
else
{
return GATE_RESULT_OK;
}
}
gate_result_t gate_inifile_store_remove_section(gate_inifile_store_t* store, gate_string_t const* section)
{
if (gate_flatmap_remove(&store->sections, section))
{
return GATE_RESULT_OK;
}
else
{
return GATE_RESULT_OK_UNCHANGED;
}
}
gate_result_t gate_inifile_store_set(gate_inifile_store_t* store, gate_string_t const* section,
gate_string_t const* key, gate_string_t const* value)
{
gate_result_t ret = GATE_RESULT_OK;
gate_flatmap_iterator_t section_entry;
gate_flatmap_t* entry_map;<--- The scope of the variable 'entry_map' can be reduced. [+]The scope of the variable 'entry_map' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
do
{
if (gate_string_is_empty(key))
{
/* a key MUST NOT be empty */
ret = GATE_RESULT_INVALIDARG;
break;
}
section_entry = gate_inifile_store_use_section(store, section);
if (NULL == section_entry)
{
/* failed to access or create section */
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
entry_map = (gate_flatmap_t*)gate_flatmap_iterator_value(section_entry);
if (NULL == entry_map)
{
/* section entry is invalid */
ret = GATE_RESULT_INVALIDCONTENT;
break;
}
if (NULL == gate_flatmap_add(entry_map, key, value))
{
/* failed to add */
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
/* new value successfully attached */
ret = GATE_RESULT_OK;
} while (0);
return ret;
}
gate_result_t gate_inifile_store_get(gate_inifile_store_t const* store, gate_string_t const* section,
gate_string_t const* key, gate_string_t* return_value)
{
gate_result_t ret = GATE_RESULT_OK;
gate_flatmap_iterator_t section_entry;
gate_flatmap_t* entry_map;<--- The scope of the variable 'entry_map' can be reduced. [+]The scope of the variable 'entry_map' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_flatmap_iterator_t entry_iter;
do
{
section_entry = gate_flatmap_get(&store->sections, section);
if (NULL == section_entry)
{
/* failed to access or create section */
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
entry_map = (gate_flatmap_t*)gate_flatmap_iterator_value(section_entry);
if (NULL == entry_map)
{
/* section entry is invalid */
ret = GATE_RESULT_INVALIDCONTENT;
break;
}
entry_iter = gate_flatmap_get(entry_map, key);
if (NULL == entry_iter)
{
/* key does not exist in section */
ret = GATE_RESULT_NOMATCH;
break;
}
if (NULL != return_value)
{
if (NULL == gate_string_duplicate(return_value, (gate_string_t const*)gate_flatmap_iterator_value(entry_iter)))
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
}
/* value successfully retrieved */
ret = GATE_RESULT_OK;
} while (0);
return ret;
}
gate_result_t gate_inifile_store_remove(gate_inifile_store_t* store, gate_string_t const* section, gate_string_t const* key)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_flatmap_iterator_t section_entry;
gate_flatmap_t* entry_map;<--- The scope of the variable 'entry_map' can be reduced. [+]The scope of the variable 'entry_map' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
do
{
section_entry = gate_flatmap_get(&store->sections, section);
if (NULL == section_entry)
{
/* failed to access or create section */
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
entry_map = (gate_flatmap_t*)gate_flatmap_iterator_value(section_entry);
if (NULL == entry_map)
{
/* section entry is invalid */
ret = GATE_RESULT_INVALIDCONTENT;
break;
}
if (!gate_flatmap_remove(entry_map, key))
{
/* entry key does not exist */
ret = GATE_RESULT_NOMATCH;
break;
}
/* entry successfully removed */
ret = GATE_RESULT_OK;
} while (0);
return ret;
}
gate_result_t gate_inifile_store_load(gate_inifile_store_t* inistore, gate_stream_t* input)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_string_t current_section = GATE_STRING_INIT_EMPTY;
char buffer[8192];
gate_uint64_t comment_counter = 0;
char comment_id[128] = ";;;;;;;;;;;;;;;;";
gate_size_t linelen;
gate_string_t comment_key = GATE_STRING_INIT_EMPTY;
gate_string_t current_line = GATE_STRING_INIT_EMPTY;
gate_string_t current_key = GATE_STRING_INIT_EMPTY;
gate_string_t current_value = GATE_STRING_INIT_EMPTY;
gate_size_t pos;
gate_char8_t chr;
gate_char8_t chr_last;
do
{
ret = GATE_RESULT_OK;
while (GATE_SUCCEEDED(ret))
{
ret = gate_stream_read_line(input, buffer, sizeof(buffer), &linelen);
GATE_BREAK_IF_FAILED(ret);
if (linelen == 0)
{
/* end of stream reached */
break;
}
gate_string_create_static_len(¤t_line, buffer, linelen);
gate_string_rtrim(¤t_line, ¤t_line);
gate_string_ltrim(¤t_line, ¤t_line);
if (gate_string_is_empty(¤t_line))
{
continue;
}
chr = gate_string_char_at(¤t_line, 0);
switch (chr)
{
case '[':
{
/* new section */
chr_last = gate_string_char_at(¤t_line, gate_string_length(¤t_line) - 1);
if (chr_last != ']')
{
ret = GATE_RESULT_INVALIDINPUT;
break;
}
gate_string_release(¤t_section);
if (NULL == gate_string_create(¤t_section, gate_string_ptr(¤t_line, 1), gate_string_length(¤t_line) - 2))
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
ret = gate_inifile_store_add_section(inistore, ¤t_section);
break;
}
case '#':
case ';':
{
/* comment line */
gate_str_print_uint64(&comment_id[1], sizeof(comment_id) - 1, ++comment_counter);
gate_string_create_static(&comment_key, comment_id);
gate_inifile_store_set(inistore, ¤t_section, &comment_key, ¤t_line);
gate_string_release(&comment_key);
break;
}
default:
{
/* key-value entry */
pos = gate_string_char_pos(¤t_line, '=', 0);
if (pos == GATE_STR_NPOS)
{
gate_string_duplicate(¤t_key, ¤t_line);
gate_string_create_empty(¤t_value);
}
else
{
gate_string_substr(¤t_key, ¤t_line, 0, pos);
gate_string_rtrim(¤t_key, ¤t_key);
gate_string_substr(¤t_value, ¤t_line, pos + 1, GATE_STR_NPOS);
gate_string_ltrim(¤t_value, ¤t_value);
}
ret = gate_inifile_store_set(inistore, ¤t_section, ¤t_key, ¤t_value);
gate_string_release(¤t_value);
gate_string_release(¤t_key);
break;
}
}
gate_string_release(¤t_line);
}
} while (0);
gate_string_release(¤t_section);
return ret;
}
gate_result_t gate_inifile_store_save(gate_inifile_store_t const* inistore, gate_stream_t* output)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_flatmap_iterator_t section_iter;
gate_flatmap_iterator_t entry_iter;
gate_string_t const* ptr_section_name;
gate_flatmap_t const* ptr_section_content;
gate_string_t const* ptr_key;
gate_string_t const* ptr_value;
gate_size_t entry_count;
do
{
section_iter = gate_flatmap_first(&inistore->sections);
while (gate_flatmap_iterator_valid(&inistore->sections, section_iter))
{
ptr_section_name = (gate_string_t const*)gate_flatmap_iterator_key(section_iter);
ptr_section_content = (gate_flatmap_t const*)gate_flatmap_iterator_value(section_iter);
if (ptr_section_name && ptr_section_content)
{
if (!gate_string_is_empty(ptr_section_name))
{
gate_stream_print(output,
GATE_PRINT_CHAR, '[',
GATE_PRINT_STRING, ptr_section_name,
GATE_PRINT_CHAR, ']',
GATE_PRINT_NEWLINE,
GATE_PRINT_END);
}
entry_count = gate_flatmap_count(ptr_section_content);
if (entry_count > 0)
{
entry_iter = gate_flatmap_first(ptr_section_content);
while (gate_flatmap_iterator_valid(ptr_section_content, entry_iter))
{
ptr_key = (gate_string_t const*)gate_flatmap_iterator_key(entry_iter);
ptr_value = (gate_string_t const*)gate_flatmap_iterator_value(entry_iter);
if (gate_string_starts_with_char(ptr_key, ';'))
{
gate_stream_print(output,
GATE_PRINT_STRING, ptr_value,
GATE_PRINT_NEWLINE,
GATE_PRINT_END);
}
else
{
gate_stream_print(output,
GATE_PRINT_STRING, ptr_key,
GATE_PRINT_CSTR, " = ",
GATE_PRINT_STRING, ptr_value,
GATE_PRINT_NEWLINE,
GATE_PRINT_END);
}
entry_iter = gate_flatmap_iterator_next(entry_iter);
}
gate_stream_print(output,
GATE_PRINT_NEWLINE,
GATE_PRINT_END);
}
}
section_iter = gate_flatmap_iterator_next(section_iter);
}
ret = GATE_RESULT_OK;
} while (0);
return ret;
}
static gate_result_t gate_inifile_store_import_tree(gate_inifile_store_t* inistore, gate_property_t const* property_tree, gate_string_t const* section)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_size_t count, ndx;
gate_array_t names = GATE_INIT_EMPTY;
gate_string_t const* ptr_name;
gate_property_t const* ptr_prop;
gate_property_typeid_t prop_type;
gate_string_t prop_value;
gate_strbuilder_t builder;
gate_string_t sub_section;
do
{
if (NULL == gate_property_member_names(property_tree, &names))
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
count = gate_array_length(&names);
ret = GATE_RESULT_OK;
for (ndx = 0; GATE_SUCCEEDED(ret) && (ndx != count); ++ndx)
{
ptr_name = (gate_string_t const*)gate_array_get(&names, ndx);
if (!ptr_name)
{
continue;
}
ptr_prop = gate_property_member_get(property_tree, ptr_name);
if (!ptr_prop)
{
continue;
}
prop_type = gate_property_get_type(ptr_prop);
switch (prop_type)
{
case GATE_PROPERTY_TYPE_BOOL:
case GATE_PROPERTY_TYPE_INT:
case GATE_PROPERTY_TYPE_REAL:
case GATE_PROPERTY_TYPE_STRING:
{
ret = gate_property_get_string(ptr_prop, &prop_value);
GATE_BREAK_IF_FAILED(ret);
ret = gate_inifile_store_set(inistore, section, ptr_name, &prop_value);
gate_string_release(&prop_value);
break;
}
case GATE_PROPERTY_TYPE_ARRAY:
{
break;
}
case GATE_PROPERTY_TYPE_OBJECT:
{
gate_strbuilder_create(&builder, 0);
gate_strbuilder_append_string(&builder, section);
if (gate_strbuilder_length(&builder) != 0)
{
gate_strbuilder_append_cstr(&builder, "/");
}
gate_strbuilder_append_string(&builder, ptr_name);
if (NULL == gate_strbuilder_to_string(&builder, &sub_section))
{
ret = GATE_RESULT_OUTOFMEMORY;
}
gate_strbuilder_release(&builder);
GATE_BREAK_IF_FAILED(ret);
ret = gate_inifile_store_import_tree(inistore, ptr_prop, &sub_section);
gate_string_release(&sub_section);
break;
}
}
}
} while (0);
gate_array_release(&names);
return ret;
}
gate_result_t gate_inifile_store_import(gate_inifile_store_t* inistore, gate_property_t const* property_tree)
{
gate_string_t section = GATE_STRING_INIT_EMPTY;
if (gate_property_get_type(property_tree) != GATE_PROPERTY_TYPE_OBJECT)
{
return GATE_RESULT_INVALIDINPUT;
}
else
{
return gate_inifile_store_import_tree(inistore, property_tree, §ion);
}
}
gate_result_t gate_inifile_store_export(gate_inifile_store_t* inistore, gate_property_t* property_tree)
{
/* TODO */
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_inifile_parse_string(gate_string_t const* source, gate_property_t* property_tree)
{
gate_memstream_impl_t mem_stream = GATE_INIT_EMPTY;
gate_memstream_create_static_unmanaged_readonly(&mem_stream, gate_string_ptr(source, 0), gate_string_length(source), 0);
return gate_inifile_parse((gate_stream_t*)&mem_stream, property_tree);
}
gate_result_t gate_inifile_build_string(gate_property_t const* property_tree, gate_strbuilder_t* dest_builder)
{
gate_result_t ret;
gate_stringstream_t* stream = gate_stringstream_create_builder(dest_builder);
if (NULL == stream)
{
ret = GATE_RESULT_OUTOFMEMORY;
}
else
{
ret = gate_inifile_build(property_tree, (gate_stream_t*)stream);
gate_object_release(stream);
}
return ret;
}
gate_result_t gate_inifile_parse(gate_stream_t* source, gate_property_t* property_tree)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_inifile_store_t store = GATE_INIT_EMPTY;
do
{
ret = gate_inifile_store_create(&store);
GATE_BREAK_IF_FAILED(ret);
ret = gate_inifile_store_load(&store, source);
GATE_BREAK_IF_FAILED(ret);
ret = gate_inifile_store_export(&store, property_tree);
} while (0);
gate_inifile_store_destroy(&store);
return ret;
}
gate_result_t gate_inifile_build(gate_property_t const* property_tree, gate_stream_t* output)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_inifile_store_t store = GATE_INIT_EMPTY;
do
{
ret = gate_inifile_store_create(&store);
GATE_BREAK_IF_FAILED(ret);
ret = gate_inifile_store_import(&store, property_tree);
GATE_BREAK_IF_FAILED(ret);
ret = gate_inifile_store_save(&store, output);
} while (0);
gate_inifile_store_destroy(&store);
return ret;
}
|