aboutsummaryrefslogtreecommitdiff
path: root/any_ini.h
blob: ba89f996bed93310bbc418838a5e29a41c7fde90 (plain)
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
// any_ini
//
// A single-file library that provides a simple and somewhat opinionated
// interface for parsing ini files, either from a buffer or stream.
//
// To use this library you should choose a suitable file to put the
// implementation and define ANY_INI_IMPLEMENT. For example
//
//    #define ANY_INI_IMPLEMENT
//    #include "any_ini.h"
//
// Additionally, you can customize the library behavior by defining certain
// macros in the file where you put the implementation. You can see which are
// supported by reading the code guarded by ANY_INI_IMPLEMENT.
//
// This library is licensed under the terms of the MIT license.
// A copy of the license is included at the end of this file.
//

#ifndef ANY_INI_INCLUDE
#define ANY_INI_INCLUDE

#include <stddef.h>
#include <stdbool.h>

// String parser (provided by any_ini_t)

typedef struct {
    const char *source;
    size_t length;
    size_t cursor;
    size_t line;
} any_ini_t;

// Initialize the parser with a string.
//
void any_ini_init(any_ini_t *ini, const char *source, size_t length);

// Check if the parser has reached the end of the string.
//
bool any_ini_eof(any_ini_t *ini);

// Get the current line reached by the parser.
//
size_t any_ini_line(any_ini_t *ini);

// Get the next section.
// This function will return NULL if the parser has reached the end.
//
char *any_ini_next_section(any_ini_t *ini);

// Get the next pair key.
// This function will return NULL if the section has ended.
//
char *any_ini_next_key(any_ini_t *ini);

// Get the value for the current pair.
// This function will return NULL if nothing is found.
//
// NOTE: You should always call any_ini_next_key before this function.
//
char *any_ini_next_value(any_ini_t *ini);

// Stream parser (provided by any_ini_stream_t)
//
// Can be disabled by defining ANY_INI_NO_STREAM.

#ifndef ANY_INI_NO_STREAM

// Specify the size of the temporary line buffer used by the stream parser.
//
#ifndef ANY_INI_BUFFER_SIZE
#define ANY_INI_BUFFER_SIZE 512
#endif

// The stream parser can be initialized with a function similar to fgets.
// This function should read up to size chars from stream and stop at newlines.
//
typedef char *(*any_ini_stream_read_t)(char *string, int size, void *stream);

typedef struct {
    char buffer[ANY_INI_BUFFER_SIZE];
    size_t cursor;
    size_t line;
    any_ini_stream_read_t read;
    void *stream;
    bool eof;
} any_ini_stream_t;

// Initialize the parser with a read function and a stream.
//
void any_ini_stream_init(any_ini_stream_t *ini, any_ini_stream_read_t read, void *stream);

// Initialize the parser with a file stream.
//
// NOTE: This is just a shorthand function equivalent to
//
//       any_ini_stream_init(ini, fgets, file);
//
void any_ini_file_init(any_ini_stream_t *ini, FILE *file);

// Check if the parser has reached the end of the stream.
//
bool any_ini_stream_eof(any_ini_stream_t *ini);

// Get the current line reached by the stream parser.
//
size_t any_ini_stream_line(any_ini_stream_t *ini);

// Get the next section from the stream.
// This function will return NULL if the parser has reached the end.
//
char *any_ini_stream_next_section(any_ini_stream_t *ini);

// Get the next pair key from the stream.
// This function will return NULL if the section has ended.
//
char *any_ini_stream_next_key(any_ini_stream_t *ini);

// Get the value for the current pair from the stream.
// This function will return NULL if nothing is found.
//
// NOTE: You should always call any_ini_next_key before this function.
//
char *any_ini_stream_next_value(any_ini_stream_t *ini);

#endif

#endif

#ifdef ANY_INI_IMPLEMENT

#include <string.h>
#include <ctype.h>

// The strings returned from the parser are allocated with ANY_INI_MALLOC.
// You can change allocation strategy by defining this macro in the
// implementation file like so
//
//    #define ANY_INI_IMPLEMENT
//    #define ANY_INI_MALLOC my_malloc
//    #define ANY_INI_REALLOC my_realloc
//    #include "any_ini.h"
//
// ANY_INI_MALLOC is used by the string parser and ANY_INI_REALLOC is used
// by the stream parser. If you defined ANY_INI_NO_STREAM you can ignore
// the former function.
//
#ifndef ANY_INI_MALLOC
#include <stdlib.h>
#define ANY_INI_MALLOC malloc
#define ANY_INI_REALLOC realloc
#endif

// You can define ANY_INI_DELIM_COMMENT to specify which char starts a comment.
// By default it is ';'.
// Additionally you can specify a second character with ANY_INI_DELIM_COMMENT2.
// For example
//
//    #define ANY_INI_DELIM_COMMENT2 '#'
//    #include "any_ini.h"
//
#ifndef ANY_INI_DELIM_COMMENT
#define ANY_INI_DELIM_COMMENT ';'
#endif

// You can define ANY_INI_DELIM_PAIR to specify which char divides a key from
// the value in a pair.
// By default it is '='.
//
#ifndef ANY_INI_DELIM_PAIR
#define ANY_INI_DELIM_PAIR '='
#endif

// You can define ANY_INI_SECTION_START to specify which char starts a section.
// By default it is '['.
//
#ifndef ANY_INI_SECTION_START
#define ANY_INI_SECTION_START '['
#endif

// You can define ANY_INI_SECTION_END to specify which char ends a section.
// By default it is ']'.
//
#ifndef ANY_INI_SECTION_END
#define ANY_INI_SECTION_END ']'
#endif

// The parsers allow values to stretch multiple lines if a ANY_INI_LINE_ESCAPE
// is found just before the newline character.
// By default ANY_INI_LINE_ESCAPE is '\'.
//
// You can define the macro ANY_INI_NO_MULTILINE if you want to disable
// multiline handling.
//
#ifndef ANY_INI_LINE_ESCAPE
#define ANY_INI_LINE_ESCAPE '\\'
#endif

static size_t any_ini_trim(const char *source, size_t start, size_t end)
{
    while (isspace(source[end - 1])) end--;
    return end - start;
}

// If ANY_INI_NO_MULTILINE is defined it copies verbatim the input, otherwise
// it will remove the sequences '\\\r\n' and '\\\n' while copying
static size_t any_ini_copy(char *dest, const char *source, size_t length)
{
#ifdef ANY_INI_NO_MULTILINE
    memcpy(dest, source, length);
    return length;
#else
    size_t i = 0, j = 0;
    while (i < length) {
        if (source[i] == ANY_INI_LINE_ESCAPE) {
            if (i + 1 < length && source[i + 1] == '\n')
                i += 2;
            else if (i + 2 < length && source[i + 1] == '\r' && source[i + 2] == '\n')
                i += 3;
            continue;
        }
        dest[j++] = source[i++];
    }
    return j;
#endif
}

static char *any_ini_slice(const char *start, size_t length)
{
    char *string = ANY_INI_MALLOC(length + 1);
    if (string) {
        length = any_ini_copy(string, start, length);
        string[length] = '\0';
    }
    return string;
}

static void any_ini_skip(any_ini_t *ini)
{
    while (!any_ini_eof(ini)) {
        switch (ini->source[ini->cursor]) {
            case '\n':
                ini->line++;
                break;

            case ANY_INI_DELIM_COMMENT:
#ifdef ANY_INI_DELIM_COMMENT2
            case ANY_INI_DELIM_COMMENT2:
#endif
                while (!any_ini_eof(ini) && ini->source[ini->cursor] != '\n')
                    ini->cursor++;
                continue;

            default:
                if (isspace(ini->source[ini->cursor])) break;
                return;
        }
        ini->cursor++;
    }
}

static bool any_ini_skip_pair(any_ini_t *ini, bool key)
{
    switch (ini->source[ini->cursor]) {
        case '\n':
#ifndef ANY_INI_NO_MULTILINE
            if ((ini->cursor > 2 && ini->source[ini->cursor - 1] == '\r' &&
                 ini->source[ini->cursor - 2] == ANY_INI_LINE_ESCAPE) ||
                (ini->cursor > 1 && ini->source[ini->cursor - 1] == ANY_INI_LINE_ESCAPE)) {
                ini->line++;
                return true;
            }
#endif
            return false;

#ifndef ANY_INI_NO_INLINE_COMMENT
        case ANY_INI_DELIM_COMMENT:
#ifdef ANY_INI_DELIM_COMMENT2
        case ANY_INI_DELIM_COMMENT2:
#endif
            if (ini->cursor != 0 && isspace(ini->source[ini->cursor - 1]))
                return false;
            // fallthrough
#endif

        default:
            return !key || ini->source[ini->cursor] != ANY_INI_DELIM_PAIR;
    }
}

void any_ini_init(any_ini_t *ini, const char *source, size_t length)
{
    ini->source = source;
    ini->length = length;
    ini->cursor = 0;
    ini->line = 1;
}

bool any_ini_eof(any_ini_t *ini)
{
    return ini->cursor >= ini->length;
}

size_t any_ini_line(any_ini_t *ini)
{
    return ini->line;
}

char *any_ini_next_section(any_ini_t *ini)
{
    any_ini_skip(ini);

    if (any_ini_eof(ini) || ini->source[ini->cursor] != ANY_INI_SECTION_START)
        return NULL;

    ++ini->cursor;
    while (!any_ini_eof(ini) && isspace(ini->source[ini->cursor]))
        ini->cursor++;
    size_t start = ini->cursor;

    while (!any_ini_eof(ini) && ini->source[ini->cursor] != '\n' && ini->source[ini->cursor] != ANY_INI_SECTION_END)
        ini->cursor++;

    size_t end = ini->cursor;
    while (!any_ini_eof(ini) && ini->source[ini->cursor] != '\n')
        ini->cursor++;

    size_t length = any_ini_trim(ini->source, start, end);
    return any_ini_slice(ini->source + start, length);
}

char *any_ini_next_key(any_ini_t *ini)
{
    any_ini_skip(ini);

    if (any_ini_eof(ini) || ini->source[ini->cursor] == ANY_INI_SECTION_START)
        return NULL;

    size_t start = ini->cursor;
    while (!any_ini_eof(ini) && any_ini_skip_pair(ini, true))
        ini->cursor++;

    size_t length = any_ini_trim(ini->source, start, ini->cursor);
    return any_ini_slice(ini->source + start, length);
}

char *any_ini_next_value(any_ini_t *ini)
{
    if (any_ini_eof(ini) || ini->source[ini->cursor] != ANY_INI_DELIM_PAIR)
        return NULL;

    ++ini->cursor;
    any_ini_skip(ini);

    size_t start = ini->cursor;
    while (!any_ini_eof(ini) && any_ini_skip_pair(ini, false))
        ini->cursor++;

    size_t length = any_ini_trim(ini->source, start, ini->cursor);
    return any_ini_slice(ini->source + start, length);
}

#ifndef ANY_INI_NO_STREAM

static void any_ini_stream_read(any_ini_stream_t *ini)
{
    ini->eof = ini->read(ini->buffer, ANY_INI_BUFFER_SIZE, ini->stream) == NULL;
    ini->cursor = 0;
}

static void any_ini_stream_skip_line(any_ini_stream_t *ini)
{
    while (!ini->eof && ini->buffer[ini->cursor] != '\n') {
        if (ini->buffer[ini->cursor] == '\0')
            any_ini_stream_read(ini);
        else
            ini->cursor++;
    }
}

static void any_ini_stream_skip(any_ini_stream_t *ini, bool comment)
{
    while (!ini->eof) {
        switch (ini->buffer[ini->cursor]) {
            case '\0':
                any_ini_stream_read(ini);
                continue;

            case ANY_INI_DELIM_COMMENT:
#ifdef ANY_INI_DELIM_COMMENT2
            case ANY_INI_DELIM_COMMENT2:
#endif
                if (!comment)
                    return;

                any_ini_stream_skip_line(ini);
                // fallthrough

            // Discard the current line
            case '\n':
                ini->line++;
                any_ini_stream_read(ini);
                continue;

            default:
                if (isspace(ini->buffer[ini->cursor])) break;
                return;
        }
        ini->cursor++;
    }
}


static char *any_ini_stream_until(any_ini_stream_t *ini, size_t start, char c)
{
    char *tmp, *value = NULL;
    size_t size = 0;
    char prev[2] = { 0 };

    bool done = false;
    while (!ini->eof && !done) {
        switch (ini->buffer[ini->cursor]) {
            // Copy current buffer and refill
            case '\0':
                tmp = ANY_INI_REALLOC(value, size + ini->cursor - start);
                if (!tmp) return value;

                size += any_ini_copy(tmp + size, ini->buffer + start, ini->cursor - start);
                value = tmp;

                any_ini_stream_read(ini);
                start = 0;
                break;

            // Stop at line boundaries
            case '\n':
#ifndef ANY_INI_NO_MULTILINE
                if ((prev[0] == '\r' && prev[1] == '\\') || prev[0] == '\\') {
                    ini->cursor++;
                    continue;
                }
#endif
                done = true;
                break;

#ifndef ANY_INI_NO_INLINE_COMMENT
            case ANY_INI_DELIM_COMMENT:
#ifdef ANY_INI_DELIM_COMMENT2
            case ANY_INI_DELIM_COMMENT2:
#endif
                if (isspace(prev[0])) {
                    done = true;
                    break;
                }
                // fallthrough
#endif
            default:
                if (ini->buffer[ini->cursor] == c) {
                    done = true;
                    break;
                }

                prev[1] = prev[0];
                prev[0] = ini->buffer[ini->cursor];
                ini->cursor++;
                break;

        }
    }

    tmp = ANY_INI_REALLOC(value, size + ini->cursor - start);
    if (!tmp) return value;

    size += any_ini_copy(tmp + size, ini->buffer + start, ini->cursor - start);
    size = any_ini_trim(tmp, 0, size);
    tmp[size] = '\0';
    return tmp;
}

void any_ini_stream_init(any_ini_stream_t *ini, any_ini_stream_read_t read, void *stream)
{
    ini->line = 1;
    ini->read = read;
    ini->stream = stream;

    // Init buffer
    memset(ini->buffer, 0, ANY_INI_BUFFER_SIZE);
    any_ini_stream_read(ini);
}

void any_ini_file_init(any_ini_stream_t *ini, FILE *file)
{
    any_ini_stream_init(ini, (any_ini_stream_read_t)fgets, file);
}

bool any_ini_stream_eof(any_ini_stream_t *ini)
{
    return ini->eof;
}

size_t any_ini_stream_line(any_ini_stream_t *ini)
{
    return ini->line;
}

char *any_ini_stream_next_section(any_ini_stream_t *ini)
{
    any_ini_stream_skip(ini, true);

    if (ini->eof || ini->buffer[ini->cursor] != ANY_INI_SECTION_START)
        return NULL;

    // Skip padding
    ini->cursor++;
    any_ini_stream_skip(ini, false);

    char *section = any_ini_stream_until(ini, ini->cursor, ANY_INI_SECTION_END);
    any_ini_stream_skip_line(ini);
    return section;
}

char *any_ini_stream_next_key(any_ini_stream_t *ini)
{
    any_ini_stream_skip(ini, true);

    if (ini->eof || ini->buffer[ini->cursor] == ANY_INI_SECTION_START
                 || ini->buffer[ini->cursor] == ANY_INI_DELIM_PAIR)
        return NULL;

    return any_ini_stream_until(ini, ini->cursor, ANY_INI_DELIM_PAIR);
}

char *any_ini_stream_next_value(any_ini_stream_t *ini)
{
    if (ini->eof || ini->buffer[ini->cursor] != ANY_INI_DELIM_PAIR)
        return NULL;

    // Skip padding
    ini->cursor++;
    any_ini_stream_skip(ini, false);
    return any_ini_stream_until(ini, ini->cursor, '\n');
}

#endif

#endif

// MIT License
//
// Copyright (c) 2024 Federico Angelilli
//
// 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.
//