-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathini.h
More file actions
711 lines (548 loc) · 22.4 KB
/
ini.h
File metadata and controls
711 lines (548 loc) · 22.4 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
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
#ifndef INI_H
#define INI_H
/*
* MIT License
*
* Copyright (c) 2025 Carter Dugan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
//////////////////////////
// Forward Declarations //
//////////////////////////
/* Structs */
typedef struct INIPair_t INIPair_t;
typedef struct INISection_t INISection_t;
typedef struct INIData_t INIData_t;
typedef struct INIError_t INIError_t;
/* Functions */
// Internals
void ini_disable_heap (void);
void ini_set_allocator (void*(*)(size_t));
void ini_set_free (void(*)(void*));
void ini_set_reallocator (void*(*)(void*, size_t));
// File I/O
INIData_t *ini_read_file_path (const char*, INIData_t*, INIError_t*, uint64_t);
INIData_t *ini_read_file_pointer (FILE*, INIData_t*, INIError_t*, uint64_t);
void ini_write_file_path (const char*, const INIData_t*);
void ini_write_file_pointer (FILE*, const INIData_t*);
// Database insertion
INISection_t *ini_add_section (INIData_t*, const char*);
INIPair_t *ini_add_pair (const INIData_t*, const char*, INIPair_t);
INIPair_t *ini_add_pair_to_section (INISection_t *, INIPair_t);
// Database query
INISection_t *ini_has_section (const INIData_t*, const char*);
const char *ini_get_value (const INIData_t*, const char*, const char*);
const char *ini_get_string (const INIData_t*, const char*, const char*, const char*);
unsigned long long ini_get_unsigned (const INIData_t*, const char*, const char*, unsigned long long);
long long ini_get_signed (const INIData_t*, const char*, const char*, long long);
unsigned long long ini_get_hex (const INIData_t*, const char*, const char*, unsigned long long);
long double ini_get_float (const INIData_t*, const char*, const char*, long double);
bool ini_get_bool (const INIData_t*, const char*, const char*, bool);
// Parsing
bool ini_is_blank_line (const char*);
bool ini_parse_section (const char*, INISection_t*, ptrdiff_t*);
bool ini_parse_pair (const char*, INIPair_t*, ptrdiff_t*);
bool ini_parse_key (const char*, char*, unsigned, ptrdiff_t*);
bool ini_parse_value (const char*, char*, unsigned, ptrdiff_t*);
// Heap
INIData_t *ini_create_data (void);
void ini_free_data (INIData_t*);
// Stack
void ini_init_data (INIData_t*, INISection_t*, INIPair_t**, unsigned, unsigned);
//////////////
// MACROS //
//////////////
// You can redefine these without issue.
#ifndef INI_MAX_STRING_SIZE
#define INI_MAX_STRING_SIZE 256
#endif
#ifndef INI_MAX_LINE_SIZE
#define INI_MAX_LINE_SIZE 1024
#endif
#ifndef INI_DEFAULT_ALLOC
#define INI_DEFAULT_ALLOC malloc
#endif
#ifndef INI_DEFAULT_FREE
#define INI_DEFAULT_FREE free
#endif
#ifndef INI_DEFAULT_REALLOC
#define INI_DEFAULT_REALLOC realloc
#endif
#ifndef INI_INITIAL_ALLOCATED_PAIRS
#define INI_INITIAL_ALLOCATED_PAIRS 32
#endif
#ifndef INI_INITIAL_ALLOCATED_SECTIONS
#define INI_INITIAL_ALLOCATED_SECTIONS 8
#endif
// I strongly advise against changing these
#define ini_read_file(T,data,error,flags) _Generic((T), \
const char*: ini_read_file_path, \
char*: ini_read_file_path, \
FILE*: ini_read_file_pointer \
)(T,data,error,flags)
#define ini_write_file(T,data) _Generic((T), \
const char*: ini_write_file_path, \
char*: ini_write_file_path, \
FILE*: ini_write_file_pointer \
)(T,data)
// File parsing flags
#define INI_CONTINUE_PAST_ERROR (1ull << 0)
#define INI_ALLOW_DUPLICATE_SECTIONS (1ull << 1)
#define INI_DUPLICATE_KEYS_OVERWRITE (1ull << 2)
////////////////////////
// Struct Definitions //
////////////////////////
/**
* Key=value pair
*/
struct INIPair_t
{
char key[INI_MAX_STRING_SIZE];
char value[INI_MAX_STRING_SIZE];
};
/**
* [Section]
*
* Keeps track of encapsulated pairs, the number of pairs,
* and the number of allocated pairs.
*/
struct INISection_t
{
// Name of the section, excluding the encapsulating
// [ ] characters
char name[INI_MAX_STRING_SIZE];
// Pointer to pairs
INIPair_t *pairs;
unsigned pair_count;
// Number of allocated pairs
// >= pair_count
unsigned pair_allocation;
};
/**
* Data structure for INI contents. Keeps track of
* sections and the number of sections.
*/
struct INIData_t
{
// Pointer to sections
INISection_t *sections;
unsigned section_count;
// Number of allocated sections
// >= section_count
unsigned section_allocation;
};
/**
* A container for the parsing error information.
*/
struct INIError_t
{
// Set if an error is encountered during parsing
bool encountered;
// A message describing the error
char msg[INI_MAX_LINE_SIZE];
// The culprit line
char line[INI_MAX_LINE_SIZE];
// The offset of the invalid character if encountered.
ptrdiff_t offset;
};
///////////////////////////
// Function Declarations //
///////////////////////////
/**
* @brief
* Disable internal calls to heap allocation functions,
* which by default are malloc, realloc, and free. If
* you use this, then all attempted heap calls are
* treated as failures and must be handled accordingly.
*
* See examples/stack/ to see an example of using the library
* without the heap.
*/
void ini_disable_heap(void);
/**
* Set the allocator to be used internally by ini. If you
* set this, you almost *certainly* want to also set the
* reallocator and deallocator.
*
* @param allocator malloc like allocator
*/
void ini_set_allocator(void *(*allocator) (size_t));
/**
* Set the deallocator to be used internally by ini. If you
* set this, you almost *certainly* want to also set the
* reallocator and allocator.
*
* @param deallocator free like deallocator
*/
void ini_set_free(void (*deallocator) (void*));
/**
* Set the reallocator to be used internally by ini. If you
* set this, you almost *certainly* want to also set the
* allocator and deallocator.
*
* @param reallocator realloc like reallocator
*/
void ini_set_reallocator(void *(*reallocator) (void*,size_t));
/**
* @brief Parse an ini file and populate a data structure with contents. User will need to free the returned object on their own later on with a call to ini_free()
*
* @param file File path to parse
*
* @param data The database object to be filled with ini contents.
*
* @param buffer Line buffer to store erroneous line. Must be
*
* @param flags Bit-aligned flags to control behavior of the file parser. See the flag macros
*
* @return A pointer to data on success, or NULL on failure.
*
*/
INIData_t *ini_read_file_path(const char *path, INIData_t *data, INIError_t *error, uint64_t flags);
/**
* Parse an ini file and populate a data structure
* with contents. User will need to free the returned
* object on their own later on with a call to ini_free()
*
* @param file File pointer to parse
* @param data The database object to be filled with ini contents.
* Must be a valid pointer.
* @param error Pointer to error object to keep track of
* erroneous character offset and store error message
* @param flags Bit-aligned flags to control behavior of the file parser.
* See the flag macros
*
* @return A pointer to data on success, or NULL on failure.
*/
INIData_t *ini_read_file_pointer(FILE *file, INIData_t *data, INIError_t *error, uint64_t flags);
/**
* Use the contents of an INIData_t object to generate an
* INI file (or overwrite an existing one)
*
* @param file Destination file path.
* @param data A pointer to the INIData_t object whose data
* you would like to write
*/
void ini_write_file_path(const char *path, const INIData_t *data);
/**
* Use the contents of an INIData_t object to generate an
* INI file (or overwrite an existing one)
*
* @param file Destination file pointer.
* @param data A pointer to the INIData_t object whose data
* you would like to write
*/
void ini_write_file_pointer(FILE *file, const INIData_t *data);
/**
* Add a section to an INIData_t object by providing the name
* of the new section. This internally will call ini_section_init()
* (see above).
* @see ini_section_init()
*
* @param data The INIData_t object that will acquire the new section.
* @param name The name of the section to be added.
*
* @return A pointer to the newly-added section, or NULL on error or
* if the section already exists.
*
* If you have opted to use heap allocations, then this
* allocates space for the new heap. Otherwise it will return
* a pointer to the newly initialized section.
*/
INISection_t *ini_add_section(INIData_t *data, const char *name);
/**
* Add a pair to an INIData_t object by providing the section
* name and indirectly adding it to the section.
*
* @param data The INIData_t object to add the pair to. Must be
* a valid pointer
* @param section The **name** of the section to add the pair
* to. Assumed to be null-terminated.
* @param pair The pair to be added. Assumed that both internal
* strings for key and value are null-terminated.
*
* @return
* A pointer to the pair after being added to the proper
* section within `data`, or NULL on failure (i.e., providing
* a name for a section that does not exist in `data`)
*/
INIPair_t *ini_add_pair(const INIData_t *data, const char *section, INIPair_t pair);
/**
* Add a pair directly to a section, agnostic to the parent
* INIData_t object.
*
* @param section The section to acquire the pair.
* @param pair A pair object whose data will be copied into
* a new pair in the section.
*
* @return A pointer to the newly-added pair within the section.
*/
INIPair_t *ini_add_pair_to_section(INISection_t *section, INIPair_t pair);
/**
* Query for a section object based on the section name.
*
* @param data The INIData_t object that represents an INI file.
* @param section The name of the section you are checking for.
*
* @return A pointer to the located INISection_t object, or NULL if
* the section is not found.
*/
INISection_t *ini_has_section(const INIData_t *data, const char *section);
/**
* Retrieve a value from an INIData_t object given a section
* name and a key value.
*
* @param data The INIData_t object to be searched.
* @param section The section string to search for.
* @param key The key string to search for.
*
* @return The value in the form of a null-terminated C-string, or
* NULL if not found.
*/
const char *ini_get_value(const INIData_t *data, const char *section, const char *key);
/**
* Attempt to fetch an string value from INI data given a
* section and key. If unfound, returns a provided default.
*
* @param data Pointer to the INIData_t object to search
* @param section The section title being searched for.
* @param key The key being searched for.
* @param default Default value to be used if searched
* value is not found or fails to be parsed.
*
* @return As said above, returns the searched value or the provided
* default if the searched value could not be found or parsing
* failed.
*/
const char *ini_get_string(const INIData_t *data, const char *section, const char *key, const char *default_value);
/**
* Attempt to fetch an unsigned integer value from INI data given a
* section and key. If unfound, returns a provided default.
*
* @param data Pointer to the INIData_t object to search
* @param section The section title being searched for.
* @param key The key being searched for.
* @param default Default value to be used if searched
* value is not found or fails to be parsed.
*
* @return As said above, returns the searched value or the provided
* default if the searched value could not be found or parsing
* failed.
*/
unsigned long long ini_get_unsigned(const INIData_t *data, const char *section, const char *key, unsigned long long default_value);
/**
* Attempt to fetch a signed integer value from INI data given a
* section and key. If unfound, returns a provided default.
*
* @param data Pointer to the INIData_t object to search
* @param section The section title being searched for.
* @param key The key being searched for.
* @param default Default value to be used if searched
* value is not found or fails to be parsed.
*
* @return As said above, returns the searched value or the provided
* default if the searched value could not be found or parsing
* failed.
*/
long long ini_get_signed(const INIData_t *data, const char *section, const char *key, long long default_value);
/**
* Attempt to fetch an unsigned base-16 hex value from INI data given a
* section and key. If unfound, returns a provided default.
*
* @param data Pointer to the INIData_t object to search
* @param section The section title being searched for.
* @param key The key being searched for.
* @param default Default value to be used if searched
* value is not found or fails to be parsed.
*
* @return As said above, returns the searched value or the provided
* default if the searched value could not be found or parsing
* failed.
*/
unsigned long long ini_get_hex(const INIData_t *data, const char *section, const char *key, unsigned long long default_value);
/**
* Attempt to fetch an floating point value from INI data given a
* section and key. If unfound, returns a provided default.
*
* @param data Pointer to the INIData_t object to search
* @param section The section title being searched for.
* @param key The key being searched for.
* @param default Default value to be used if searched
* value is not found or fails to be parsed.
*
* @return As said above, returns the searched value or the provided
* default if the searched value could not be found or parsing
* failed.
*/
long double ini_get_float(const INIData_t *data, const char *section, const char *key, long double default_value);
/**
* Attempt to fetch an boolean from INI data given a section
* and key. If unfound, returns a provided default.
*
* @param data Pointer to the INIData_t object to search
* @param section The section title being searched for.
* @param key The key being searched for.
* @param default Default value to be used if searched
* value is not found or fails to be parsed.
*
* @return As said above, returns the searched value or the provided
* default if the searched value could not be found or parsing
* failed.
*/
bool ini_get_bool(const INIData_t *data, const char *section, const char *key, bool default_value);
/**
*
* A helper function that parses a character array and
* determines if the array represents a blank line via
* INI syntax rules (contains only whitespace or comments)
*
* @param line The character array to be parsed. Assumed
* to be null-terminated.
*
* @return True if the line is considered blank, false
* otherwise.
*/
bool ini_is_blank_line(const char *line);
/**
* A helper function that parses a character array and
* attempts to parse a valid section.
*
* @param line The character array to be parsed. Assumed
* to be null-terminated.
* @param section A pointer to a destination section to
* store name strings. If NULL is provided,
* has no effect. If a string is not a valid
* section, then the name string is zero-length
* and null-terminated.
* @param discrepancy A pointer to an integer representing the
* offset of the erroneous character if
* present. If no error found, will be given
* 0. If NULL, has no effect.
*
* @return True if the line is considered a valid section,
* false otherwise or on failure.
*/
bool ini_parse_section(const char *line, INISection_t *section, ptrdiff_t *discrepancy);
/**
* A helper function that reads a character array and
* attempts to parse a valid pair.
*
* @param line The character array to be parsed. Assumed to be
* null-terminated.
* @param pair A pointer to a destination pair to store
* key and value strings. If NULL is provided,
* has no effect. If a string is not a valid
* pair, then the key and value strings are
* zero-length and null-terminated.
* @param discrepancy A pointer to an integer representing the
* offset of the erroneous character if
* present. If no error found, will be given
* 0. If NULL, has no effect.
*
* @return True if the line is considered a legal k=v pair,
* false otherwise or on failure.
*/
bool ini_parse_pair(const char *line, INIPair_t *pair, ptrdiff_t *discrepancy);
/**
* A helper function that parses a character array and
* attempts to parse a valid key.
*
* @param line The character array to be parsed. Assumed
* to be null-terminated.
* @param dest A pointer to a destination buffer to
* store key string. If NULL is provided,
* has no effect. If a string is not a valid
* section, then the string is invalid and
* not guaranteed to be null-terminated.
* @param n The size of the destination buffer. Assumed
* to be accurate.
* @param discrepancy A pointer to an integer representing the
* offset of the erroneous character if
* present. If no error found, will be given
* 0. If NULL, has no effect.
*
* @return True if the line is considered a valid key, false
* otherwise or on failure.
*/
bool ini_parse_key(const char *line, char *dest, unsigned n, ptrdiff_t *discrepancy);
/**
* A helper function that parses a character array and
* attempts to parse a valid value.
*
* @param line The character array to be parsed.
* @param dest A pointer to a destination buffer to
* store value string. If NULL is provided,
* has no effect. If a string is not a valid
* section, then the string is invalid and
* not guaranteed to be null-terminated.
* @param n The size of the destination buffer. Assumed
* to be accurate.
* @param discrepancy A pointer to an integer representing the
* offset of the erroneous character if
* present. If no error found, will be given
* 0. If NULL, has no effect.
*
* @return True if the line is considered a valid value, false
* otherwise.
*/
bool ini_parse_value(const char *line, char *dest, unsigned n, ptrdiff_t *discrepancy);
/**
* @brief Create a heap-allocated INIData_t database object.
*
* @return Pointer to INIData_t object that must be free'd later
* with a call to ini_free_data(). Initial allocations
* comply with INI_INITIAL_ALLOCATED_PAIRS and
* INI_INITIAL_ALLOCATED_SECTIONS
*/
INIData_t *ini_create_data();
/**
* Free the memory resources used by an INIData_t object.
* This should be called if you have created an INIData_t
* object with ini_create_data()
*
* @param data The INIData_t object to be free'd.
*/
void ini_free_data(INIData_t *data);
/**
* Initialize a data object with sections, initialize sections
* with pairs. Useful for saving some time when working with
* the stack. See examples.
*
* @param data The INIData_t object to be initialized. Its section
* count is set to zero, its section allocations set
* to num_sections.
* @param sections The sections to be initialized with pairs and given
* to the data object. Its pair count is set to zero
* and its pair allocations set to num_pair. Expected
* to be of size num_sections
* @param pairs The pairs given to the section objects. Expected to
* be a two-dimensional array of size
* [num_sections][num_pairs]
* @param num_sections The number of sections. Assumed to be the only dimension
* of the sections array and the first dimension of the
* pairs 2D array.
* @param num_pairs The number of pairs per section. Assumed to be the
* second dimension of the pairs 2D array.
*/
void ini_init_data(INIData_t* data, INISection_t* sections, INIPair_t** pairs, unsigned num_sections, unsigned num_pairs);
#endif //INI_H