aboutsummaryrefslogtreecommitdiff
path: root/src/vdf/vdf.c
blob: fc43e8ca1b8012023205a7740d590eb490c49f2c (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
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>

#include "vdf.h"

#define CHAR_SPACE ' '
#define CHAR_TAB '\t'
#define CHAR_NEWLINE '\n'
#define CHAR_DOUBLE_QUOTE '"'
#define CHAR_OPEN_CURLY_BRACKET '{'
#define CHAR_CLOSED_CURLY_BRACKET '}'
#define CHAR_FRONTSLASH '/'
#define CHAR_BACKSLASH '\\'

#define FMT_UNKNOWN_CHAR "Encountered Unknown Character %c (%li)\n"

static char* local_strndup_escape(const char* s, size_t n)
{
    if (!s)
       return NULL;

    char* retval = malloc(n + 1);
    strncpy(retval, s, n);
    retval[n] = '\0';

    char* head = retval;
    char* tail = retval + n;

    while (*head)
    {
        if (*head == CHAR_BACKSLASH)
        {
            switch (head[1])
            {
                case 'n':
                    memmove(head, head+1, (size_t)(tail-head));
                    *head = CHAR_NEWLINE;
                    break;

                case 't':
                    memmove(head, head+1, (size_t)(tail-head));
                    *head = CHAR_TAB;
                    break;

                case CHAR_BACKSLASH:
                case CHAR_DOUBLE_QUOTE:
                    memmove(head, head+1, (size_t)(tail-head));
                    break;
            }
        }
        ++head;
    }

    return retval;
}

static void puts_escaped(const char* s)
{
    while (*s)
    {
        switch(*s)
        {
            case CHAR_DOUBLE_QUOTE:
                printf("\\\"");
                break;

            case CHAR_TAB:
                printf("\\t");
                break;

            case CHAR_NEWLINE:
                printf("\\n");
                break;

            case CHAR_BACKSLASH:
                printf("\\\\");
                break;

            default:
                printf("%c", *s);
                break;
        }

        ++s;
    }
}

struct vdf_object* vdf_parse_buffer(const char* buffer, size_t size)
{
    if (!buffer)
        return NULL;

    struct vdf_object* root_object = malloc(sizeof(struct vdf_object));
    root_object->key = NULL;
    root_object->parent = NULL;
    root_object->type = VDF_TYPE_NONE;

    struct vdf_object* o = root_object;

    const char* head = buffer;
    const char* tail = head;

    const char* end = buffer + size;

    const char* buf = NULL;

    while (end > tail)
    {
        switch (*tail)
        {
            case CHAR_DOUBLE_QUOTE:
                if (tail > buffer && *(tail-1) == CHAR_BACKSLASH)
                    break;

                if (!buf)
                {
                    buf = tail+1;
                }
                else if (o->key)
                {
                    size_t len = tail - buf;
                    size_t digits = 0;
                    size_t chars = 0;

                    for (size_t i = 0; i < len; ++i)
                    {
                        if (isdigit(buf[i]))
                            digits++;

                        if (isalpha(buf[i]))
                            chars++;
                    }

                    if (len && digits == len)
                    {
                        o->type = VDF_TYPE_INT;
                    }
                    else
                    {
                        o->type = VDF_TYPE_STRING;
                    }

                    switch (o->type)
                    {
                        case VDF_TYPE_INT:
                            o->data.data_int = strtol(buf, NULL, 10);
                            break;

                        case VDF_TYPE_STRING:
                            o->data.data_string.len = len;
                            o->data.data_string.str = local_strndup_escape(buf, len);
                            break;

                        default:
                            assert(0);
                            break;
                    }

                    buf = NULL;

                    if (o->parent && o->parent->type == VDF_TYPE_ARRAY)
                    {
                        o = o->parent;
                        assert(o->type == VDF_TYPE_ARRAY);

                        o->data.data_array.len++;
                        o->data.data_array.data_value = realloc(o->data.data_array.data_value, (sizeof(void*)) * (o->data.data_array.len + 1));
                        o->data.data_array.data_value[o->data.data_array.len] = malloc(sizeof(struct vdf_object)),
                        o->data.data_array.data_value[o->data.data_array.len]->parent = o;

                        o = o->data.data_array.data_value[o->data.data_array.len];
                        o->key = NULL;
                        o->type = VDF_TYPE_NONE;
                    }
                }
                else
                {
                    size_t len = tail - buf;
                    o->key = local_strndup_escape(buf, len);
                    buf = NULL;
                }
                break;

            case CHAR_OPEN_CURLY_BRACKET:
                assert(!buf);
                assert(o->type == VDF_TYPE_NONE);

                if (o->parent && o->parent->type == VDF_TYPE_ARRAY)
                    o->parent->data.data_array.len++;

                o->type = VDF_TYPE_ARRAY;
                o->data.data_array.len = 0;
                o->data.data_array.data_value = malloc((sizeof(void*)) * (o->data.data_array.len + 1));
                o->data.data_array.data_value[o->data.data_array.len] = malloc(sizeof(struct vdf_object)),
                o->data.data_array.data_value[o->data.data_array.len]->parent = o;

                o = o->data.data_array.data_value[o->data.data_array.len];
                o->key = NULL;
                o->type = VDF_TYPE_NONE;
                break;

            case CHAR_CLOSED_CURLY_BRACKET:
                assert(!buf);

                o = o->parent;
                assert(o);
                if (o->parent)
                {
                    o = o->parent;
                    assert(o->type == VDF_TYPE_ARRAY);

                    o->data.data_array.data_value = realloc(o->data.data_array.data_value, (sizeof(void*)) * (o->data.data_array.len + 1));
                    o->data.data_array.data_value[o->data.data_array.len] = malloc(sizeof(struct vdf_object)),
                    o->data.data_array.data_value[o->data.data_array.len]->parent = o;

                    o = o->data.data_array.data_value[o->data.data_array.len];
                    o->key = NULL;
                    o->type = VDF_TYPE_NONE;
                }

                break;

            case CHAR_FRONTSLASH:
                if (!buf)
                    while (*tail != '\0' && *tail != CHAR_NEWLINE)
                        ++tail;

                break;

            default:
            case CHAR_NEWLINE:
            case CHAR_SPACE:
            case CHAR_TAB:
                break;
        }
        ++tail;
    }
    return root_object;
}

struct vdf_object* vdf_parse_file(const char* path)
{
    struct vdf_object* o = NULL;
    if (!path)
        return o;

    FILE* fd = fopen(path, "r");
    if (!fd)
        return o;

    fseek(fd, 0L, SEEK_END);
    size_t file_size = ftell(fd);
    rewind(fd);

    if (file_size)
    {
        char* buffer = malloc(file_size);
        fread(buffer, sizeof(*buffer), file_size, fd);

        o = vdf_parse_buffer(buffer, file_size);
        free(buffer);
    }

    fclose(fd);

    return o;
}


size_t vdf_object_get_array_length(struct vdf_object* o)
{
    assert(o);
    assert(o->type == VDF_TYPE_ARRAY);

    return o->data.data_array.len;
}

struct vdf_object* vdf_object_index_array(struct vdf_object* o, size_t index)
{
    assert(o);
    assert(o->type == VDF_TYPE_ARRAY);
    assert(o->data.data_array.len > index);

    return o->data.data_array.data_value[index];
}

struct vdf_object* vdf_object_index_array_str(struct vdf_object* o, char* str)
{
    if (!o || !str || o->type != VDF_TYPE_ARRAY)
        return NULL;

    for (size_t i = 0; i < o->data.data_array.len; ++i)
    {
        struct vdf_object* k = o->data.data_array.data_value[i];
        if (!strcmp(k->key, str))
            return k;
    }
    return NULL;
}

const char* vdf_object_get_string(struct vdf_object* o)
{
    assert(o->type == VDF_TYPE_STRING);

    return o->data.data_string.str;
}

int vdf_object_get_int(struct vdf_object* o)
{
    assert(o->type == VDF_TYPE_INT);

    return o->data.data_int;
}

static void vdf_print_object_indent(struct vdf_object* o, int l)
{
    if (!o)
        return;

    char* spacing = "\t";

    for (int k = 0; k < l; ++k)
        printf("%s", spacing);

    printf("\"");
    puts_escaped(o->key);
    printf("\"");

    switch (o->type)
    {
        case VDF_TYPE_ARRAY:
            puts("");
            for (int k = 0; k < l; ++k)
                printf("%s", spacing);
            puts("{");
            for (size_t i = 0; i < o->data.data_array.len; ++i)
                vdf_print_object_indent(o->data.data_array.data_value[i], l+1);

            for (int k = 0; k < l; ++k)
                printf("%s", spacing);
            puts("}");
            break;

        case VDF_TYPE_INT:
            printf("\t\t\"%lli\"\n", o->data.data_int);
            break;

        case VDF_TYPE_STRING:
            printf("\t\t\"");
            puts_escaped(o->data.data_string.str);
            printf("\"\n");
            break;

        default:
        case VDF_TYPE_NONE:
            assert(0);
            break;
    }
}

void vdf_print_object(struct vdf_object* o)
{
    vdf_print_object_indent(o, 0);
}

void vdf_free_object(struct vdf_object* o)
{
    if (!o)
        return;

    switch (o->type)
    {
        case VDF_TYPE_ARRAY:
            for (size_t i = 0; i <= o->data.data_array.len; ++i)
            {
                vdf_free_object(o->data.data_array.data_value[i]);
            }
            free(o->data.data_array.data_value);
            break;


        case VDF_TYPE_STRING:
            if (o->data.data_string.str)
                free(o->data.data_string.str);
            break;

        default:
        case VDF_TYPE_NONE:
            break;

    }

    if (o->key)
        free(o->key);
    free(o);
}