-
Notifications
You must be signed in to change notification settings - Fork 0
/
tini.c
570 lines (469 loc) · 16.1 KB
/
tini.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
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
/*=======================================================================================
TinyINI - small and simple open-source library for loading, saving and
managing INI file data structures in the memory.
TinyINI is distributed under following terms and conditions:
Copyright (c) 2015-2016, Ivan Pizhenko.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* 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.
* Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
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 BEN HOYT 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.
SPECIAL NOTICE
TinyINI library relies on the open-source INIH library
(https://github.com/benhoyt/inih) for parsing text of INI file.
Source code of INIH library and information about it, including
licensing conditions, is included in the subfolder inih.
=======================================================================================*/
#include "include/tini/tini.h"
#include <errno.h>
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include "inih/ini.h"
/* INI section data structure */
struct _ini_section {
char* name; /* section name */
char** keys; /* array of parameter names */
char** values; /* array of parameter values */
size_t parameter_count; /* current number of parameters */
size_t max_parameter_count; /* maximum number of parameters for which memory is currently allocated */
};
struct _ini_file {
ini_section** sections; /* array of INI file sections */
size_t section_count; /* number of sections */
size_t max_section_count; /* maximum number of parameters for which memory is currently allocated */
};
static size_t find_section_index(const ini_file* ini, const char* section) {
size_t i;
/* Enumerate all sections, compare section name to the given input,
* return section index + 1 if match found, otherwise return zero.
*/
for (i = 0; i < ini->section_count; ++i) {
if (strcmp(ini->sections[i]->name, section) == 0)
return i + 1;
}
return 0;
}
static size_t find_parameter_index_in_section(const ini_section* section, const char* key) {
size_t i;
/* Enumerate all parameter names, compare parameter name to the given input,
* return parameter index + 1 if match found, otherwise return zero.
*/
for (i = 0; i < section->parameter_count; ++i) {
if(strcmp(section->keys[i], key) == 0)
return i + 1;
}
return 0;
}
#ifdef TINI_FEATURE_EDIT_INI_FILE
static void remove_section_by_index(ini_file* ini, size_t index) {
/* Destroy section object */
tini_free_section(ini->sections[index]);
/* Pack array of section pointers if removed section was in the beginning or middle */
if(index < ini->section_count - 1) {
memmove(ini->sections + index, ini->sections + index + 1,
sizeof(ini_section*) * (ini->section_count - index - 1));
}
/* Decrease current number of sections */
--ini->section_count;
}
#endif
/* INI file parsing handler for included INIH library. */
static int ini_file_handler(void* user, const char* section, const char* name,
const char* value)
{
/* Attempt to parameter to the INI file object, replace duplicates */
return tini_add_parameter((ini_file*)user, section, name, value, 1) == 0
? 1 : 0;
}
static int grow_section_storage(ini_file* ini) {
/* Find new storage size */
size_t new_max_section_count = ini->max_section_count + TINI_SECTION_STORAGE_SIZE_INCREMENT;
/* Reallocate memory */
ini_section** new_sections = realloc(ini->sections, sizeof(ini_section*) * new_max_section_count);
/* Update INI file object or indicate failure */
if (new_sections) {
ini->sections = new_sections;
ini->max_section_count = new_max_section_count;
return 0;
}
else return -1;
}
static int grow_parameter_storage(ini_section* section) {
/* Find new storage size */
size_t new_max_parameter_count = section->max_parameter_count + TINI_PARAMETER_STORAGE_SIZE_INCREMENT;
/* Reallocate memory */
char** new_keys = realloc(section->keys, sizeof(char*) * (new_max_parameter_count + 1) * 2);
/* Update INI file section object or indicate failure */
if (new_keys) {
char** old_values = new_keys + section->max_parameter_count + 1;
char** new_values = new_keys + new_max_parameter_count + 1;
memmove(new_values, old_values, sizeof(char*) * (section->parameter_count + 1));
section->keys = new_keys;
section->values = new_values;
section->max_parameter_count = new_max_parameter_count;
return 0;
}
else return -1;
}
static void cleanup_section(ini_section* section) {
/* Free memory consumed by parameter names and values */
size_t i;
for (i = 0; i < section->parameter_count; ++i) {
free(section->values[i]);
free(section->keys[i]);
}
/* Free memory consumed by parameter names and values storage */
free(section->keys);
/* Free memory consumed by section name */
free(section->name);
}
static int initialize_section(ini_section* section, const char* name) {
/* Save previous errno */
int saved_errno = errno;
/* Create section name*/
section->name = strdup(name);
if (!section->name) {
saved_errno = errno;
goto exit_error;
}
/* Allocate initial storage for parameter names and values */
section->keys = malloc(sizeof(const char*) * (TINI_PARAMETER_STORAGE_INITIAL_SIZE + 1) * 2);
if (!section->keys) {
saved_errno = errno;
goto cleanup_name;
}
/* Initialize storage */
section->values = section->keys + TINI_PARAMETER_STORAGE_INITIAL_SIZE + 1;
*(section->keys) = NULL;
*(section->values) = NULL;
/* Initialize counts of parameters */
section->parameter_count = 0;
section->max_parameter_count = TINI_PARAMETER_STORAGE_INITIAL_SIZE;
return 0;
cleanup_name:
/* Free memory on error */
free(section->name);
exit_error:
/* restore saved errno */
errno = saved_errno;
return -1;
}
static int initialize_ini(ini_file* ini) {
/* Allocate storage for sections */
ini->sections = malloc(sizeof(ini_section*) * TINI_SECTION_STORAGE_INITIAL_SIZE);
/* Initialize storage of sections or indicate error */
if (ini->sections) {
ini->section_count = 0;
ini->max_section_count = TINI_SECTION_STORAGE_INITIAL_SIZE;
return 0;
}
else return -1;
}
static void cleanup_ini(ini_file* ini) {
/* Cleanup all section objects */
size_t i;
for (i = 0; i < ini->section_count; ++i) {
cleanup_section(ini->sections[i]);
free(ini->sections[i]);
}
/* Free sections storage */
free(ini->sections);
}
ini_file* tini_create_ini(void) {
/* Allocate memory for INI file object */
ini_file* ini = malloc(sizeof(ini_file));
/* Initialize object, check result, indicate error if necessary */
if (ini && initialize_ini(ini) != 0) {
int saved_errno = errno;
free(ini);
ini = NULL;
errno = saved_errno;
}
/* Returns pointer to object (or NULL) */
return ini;
}
void tini_free_ini(ini_file* ini) {
/* By convention, free()-like functions accept NULL input */
if (ini) {
/* Cleanup object */
cleanup_ini(ini);
/* free memory */
free(ini);
}
}
ini_file* tini_load_ini(const char* file_path) {
/* Create INI file object */
ini_file* ini = tini_create_ini();
if (ini) {
/* Parse INI file using INIH library into INI file object, check result,
* indicate error if necessary.
*/
if (ini_parse(file_path, &ini_file_handler, ini) < 0) {
int saved_errno = errno;
free(ini);
ini = NULL;
errno = saved_errno;
}
}
/* Returns resulting object */
return ini;
}
#ifdef TINI_FEATURE_SAVE_INI
int tini_save_ini(const ini_file* ini, const char* file_path) {
/* Initialize result variable */
int res = -1;
/* Open file */
FILE* f = fopen(file_path, "w");
if (f) {
/* Dump INI file object into that file */
int saved_errno;
res = tini_dump_ini(ini, f);
/* Close file */
saved_errno = errno;
if(fclose(f) != 0);
res = -1;
else
errno = saved_errno;
}
/* Return result */
return res;
}
#endif
#if defined(TINI_FEATURE_SAVE_INI) || defined(TINI_FEATURE_SAVE_INI)
int tini_dump_ini(const ini_file* ini, FILE* f) {
size_t i, j;
/* Enumerate all section */
for (i = 0; i < ini->section_count; ++i) {
const ini_section* s = ini->sections[i];
/* Write section header */
if(fprintf(f, "[%s]\n", s->name) < 0)
return -1;
/* Enumerate and write all parameters and values */
for (j = 0; j < s->parameter_count; ++j) {
if(fprintf(f, "%s=%s\n", s->keys[j], s->values[j]) < 0)
return -1;
}
/* Put empty line between sections */
if(fputc('\n', f) == EOF)
return -1;
}
/* Indicate success. */
return 0;
}
#endif
ini_section* tini_new_section(const char* name) {
/* Allocate memory for section object */
ini_section* section = malloc(sizeof(ini_section));
if (section) {
/* Initialize section object, check result, indicate error if necessary */
if (initialize_section(section, name) != 0) {
int saved_errno = errno;
free(section);
section = NULL;
errno = saved_errno;
}
}
/* Return resulting object or NULL */
return section;
}
void tini_free_section(ini_section* section) {
/* By convention, free()-like functions can accept NULL values */
if (section) {
/* Cleanup section object */
cleanup_section(section);
/* Free memory */
free(section);
}
}
int tini_add_parameter(ini_file* ini, const char* section, const char* key, const char* value, int replace) {
/* Attempt to find section with given name */
ini_section* sectionObj = (ini_section*) tini_find_section(ini, section);
if (sectionObj) {
/* Section found - attempt adding parameter into it */
return tini_add_parameter_to_section(sectionObj, key, value, replace);
} else {
/* Otherwise create new section */
sectionObj = tini_new_section(section);
if (sectionObj) {
/* Attempt adding parameter to it */
if(tini_add_parameter_to_section(sectionObj, key, value, replace) == 0) {
/* Attempt to add section to sections storage */
if(ini->section_count < ini->max_section_count
|| (ini->section_count == ini->max_section_count
&& grow_section_storage(ini) == 0)) {
ini->sections[ini->section_count++] = sectionObj;
return 0;
} else
return -1;
} else {
/* Indicate error */
int saved_errno = errno;
tini_free_section(sectionObj);
errno = saved_errno;
return -1;
}
} else {
/* Indicate error */
return -1;
}
}
}
int tini_add_parameter_to_section(ini_section* section, const char* key, const char* value, int replace) {
/* Check whether parameter with given name already exists */
size_t i = find_parameter_index_in_section(section, key);
if (i == 0) {
/* Create parameter name string */
char *new_key, *new_value;
new_key = strdup(key);
if (!new_key)
return -1;
/* Create parameter value string */
new_value = strdup(value);
if (!new_value) {
free(new_key);
return -1;
}
/* Attempt to add parameter to section, resize parameters storage if necessary */
if (section->parameter_count < section->max_parameter_count
|| (section->parameter_count == section->max_parameter_count
&& grow_parameter_storage(section) == 0)) {
section->keys[section->parameter_count] = new_key;
section->values[section->parameter_count] = new_value;
++section->parameter_count;
section->keys[section->parameter_count] = NULL;
section->values[section->parameter_count] = NULL;
return 0;
} else {
/* Free memory and indicate error */
free(new_value);
free(new_key);
return -1;
}
} else if (replace) { /* Parameter exists and we can replace it */
/* Create new parameter value string */
char* new_value = strdup(value);
if (new_value) {
--i;
/* Free old parameter value string */
free(section->values[i]);
/* Put new one in place */
section->values[i] = new_value;
return 0;
} else
return -1;
} else { /* Parameter exists but we can't replace it */
errno = EEXIST;
return -1;
}
}
#ifdef TINI_FEATURE_EDIT_INI
int tini_remove_section(ini_file* ini, const char* section) {
/* Search for section with given name */
size_t i = find_section_index(ini, section);
if (i == 0) {
/* Section not found, indicate error */
errno = ESRCH;
return -1;
} else {
/* Section found, remove it */
--i;
remove_section_by_index(ini, i);
return 0;
}
}
int tini_remove_parameter(ini_file* ini, const char* section, const char* key) {
size_t i, j;
/* Find section with given name */
i = find_section_index(ini, section);
if (i == 0) {
/* Section not found, indicate error */
errno = ESRCH;
return -1;
} else {
/* Section found, find parameter */
ini_section* sectionObj = ini->sections[--i];
j = find_parameter_index_in_section(s, key);
if(j == 0) {
/* Parameter not found, indicate error */
errno = ESRCH;
return -1;
} else {
/* Parameter found */
--j;
/* Free parameter name and value strings */
free(sectionObj->values[j]);
free(sectionObj->keys[j]);
/* Pack parameters array if parameter was in the beginning or middle */
if(i < s->parameter_count - 1) {
memmove(sectionObj->keys + i, sectionObj->keys + i + 1,
sizeof(char*) * (sectionObj->parameter_count - i - 1));
memmove(sectionObj->values + i, sectionObj->values + i + 1,
sizeof(char*) * (sectionObj->parameter_count - i - 1));
}
/* Clear last parameters storage entries, so storage always finishes with NULLs */
s->keys[sectionObj->parameter_count] = NULL;
s->values[sectionObj->parameter_count] = NULL;
/* Decreate parameter number */
--sectionObj->parameter_count;
/* Indicate success */
return 0;
}
}
}
#endif
const ini_section* tini_find_section(const ini_file* ini, const char* section) {
/* Find section, retrieve its index */
size_t i = find_section_index(ini, section);
/* If index is valid, return section object, otherwise return NULL */
return i == 0 ? NULL : ini->sections[i - 1];
}
const char* tini_find_parameter_in_section(const ini_section* section, const char* key, const char* default_value) {
/* Find parameter, retrieve its index */
size_t i = find_parameter_index_in_section(section, key);
/* If index is valid, return parameter value, otherwise return default value */
return i == 0 ? default_value : section->values[i - 1];
}
const char* tini_find_parameter(const ini_file* ini, const char* section, const char* key, const char* default_value) {
/* Find section*/
const ini_section* sectionObj = tini_find_section(ini, section);
/* If section found, attempt find parameter, otherwise return default value */
return sectionObj ? tini_find_parameter_in_section(sectionObj, key, default_value) : default_value;
}
#ifdef TINI_FEATURE_GET_ELEMENT_COUNT
size_t tini_get_parameter_count(const ini_section* section) {
return section->parameter_count;
}
size_t tini_get_section_count(const ini_file* ini) {
return ini->section_count;
}
#endif
#ifdef TINI_FEATURE_GET_PARAMETERS_STORAGE
const char* const* tini_get_keys(const ini_section* section) {
return (const char* const*)section->keys;
}
const char* const* tini_get_values(const ini_section* section) {
return (const char* const*)section->values;
}
#endif
#ifdef TINI_FEATURE_GET_SECTIONS_STORAGE
const ini_section* const* tini_get_sections(const ini_file* ini) {
return (const ini_section* const*)ini->sections;
}
#endif