summaryrefslogtreecommitdiff
path: root/tools/audio/xml.c
blob: 41f07a64545b543b6d9645d256542aa4033af511 (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
/**
 * SPDX-FileCopyrightText: Copyright (C) 2024 ZeldaRET
 * SPDX-License-Identifier: MPL-2.0
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
#include <assert.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "xml.h"
#include "util.h"

#define copy_out(out, v) memcpy((out), &(v), sizeof(v));

/**
 * Parse a string as an integer.
 *
 * The expected value matches case-insensitive regex `^\s*[-+]?(0x[0-9A-F]+|[0-9]+)\s*$`.
 * The value may be base 10, or base 16 with a (case-insensitive) 0x prefix.
 * Leading and trailing whitespace is ignored.
 */
static int
xml_str_to_int(const char *value)
{
    if (value == NULL || value[0] == '\0')
        goto err;

    bool neg = false;
    int res;

    size_t value_len = strlen(value);
    size_t start;

    // consume initial whitespace
    for (start = 0; start < value_len; start++) {
        if (!isspace(value[start]))
            break;
    }
    // if we consumed the whole string, it was bad
    if (start == value_len)
        goto err;

    // handle sign character if present
    if (value[start] == '+' || value[start] == '-') {
        neg = value[start] == '-';
        start++;
    }

    int base;

    // get absolute value in either base 10 or 16
    if (start + 2 < value_len && value[start + 0] == '0' && tolower(value[start + 1]) == 'x') {
        start += 2;
        base = 16;
    } else {
        base = 10;
    }

    char *str_end;
    res = strtol(&value[start], &str_end, base);
    size_t end = str_end - value;
    if (start == end)
        goto err;

    // consume trailing whitespace
    while (value[end] != '\0') {
        if (!isspace(value[end]))
            goto err;
        end++;
    }

    assert(end == value_len);

    // apply sign
    return neg ? -res : res;
err:
    error("bad int value %s", value);
}

void
xml_parse_int(const char *value, void *out)
{
    int v = xml_str_to_int(value);

    copy_out(out, v);
}

void
xml_parse_uint(const char *value, void *out)
{
    int v = xml_str_to_int(value);
    if (v < 0)
        error("Value should be unsigned");

    copy_out(out, v);
}

void
xml_parse_s16(const char *value, void *out)
{
    int v = xml_str_to_int(value);
    if (v < INT16_MIN || v > INT16_MAX)
        error("Value %d out of range for s16", v);
    int16_t vs16 = v;

    copy_out(out, vs16);
}

void
xml_parse_u8(const char *value, void *out)
{
    int v = xml_str_to_int(value);
    if (v < 0 || v > UINT8_MAX)
        error("Value %d out of range for u8", v);
    uint8_t vu8 = v;

    copy_out(out, vu8);
}

void
xml_parse_s8(const char *value, void *out)
{
    int v = xml_str_to_int(value);
    if (v < INT8_MIN || v > INT8_MAX)
        error("Value %d out of range for s8", v);
    int8_t vs8 = v;

    copy_out(out, vs8);
}

/**
 * Parse a note number name to its s8 [0;127] value.
 * For example "PITCH_EF4" -> 42.
 */
void
xml_parse_note_number(const char *value, void *out)
{
    size_t value_len = strlen(value);
    int8_t vs8;
    char c;

    if (value_len == 0)
        goto err;

    // consume initial whitespace
    size_t start;
    for (start = 0; start < value_len; start++) {
        if (!isspace(value[start]))
            break;
    }
    // if we consumed the whole string, it was bad
    if (start == value_len)
        goto err;

    c = toupper(value[start]);

    if (c >= 'A' && c <= 'G') {
        start++;

        // got a note number
        static const int8_t notes_lut[] = {
            /* A */ 12,
            /* B */ 14,
            /* C */ 3,
            /* D */ 5,
            /* E */ 7,
            /* F */ 8,
            /* G */ 10,
        };
        char cm = toupper(value[start]);
        int mod = 0;
        int v;

        if (value_len > start && (cm == 'F' || cm == 'S')) {
            // handle flat/sharp modifier
            mod = (cm == 'S') ? 1 : -1;
            start++;
        }

        if (start == value_len)
            goto err;

        bool neg = false;
        int res;

        // if value starts with NEG (ignoring case)
        if (start + 3 <= value_len && toupper(value[start + 0]) == 'N' && toupper(value[start + 1]) == 'E' &&
            toupper(value[start + 2]) == 'G') {
            neg = true;
            start += 3;
        }

        int base = 10;

        char *str_end;
        res = strtol(&value[start], &str_end, base);
        size_t end = str_end - value;
        if (start == end)
            goto err;

        // consume trailing whitespace
        while (value[end] != '\0') {
            if (!isspace(value[end]))
                goto err;
            end++;
        }

        assert(end == value_len);

        // apply sign
        v = neg ? -res : res;

        if (v < -1 || v > 10)
            error("Value %d out of range for note number", v);

        vs8 = (v - 1) * 12 + notes_lut[c - 'A'] + mod;
        if (vs8 < 0)
            vs8 += 128;
    } else { // got a raw value
        vs8 = xml_str_to_int(&value[start]);
    }

    if (vs8 < 0)
        goto err;

    copy_out(out, vs8);
    return;
err:
    error("Invalid note %s", value);
}

void
xml_parse_string(const char *value, void *out)
{
    // copies only the pointer to the string
    copy_out(out, value);
}

void
xml_parse_c_identifier(const char *value, void *out)
{
    if (!str_is_c_identifier(value))
        error("Input %s is not a valid C Language identifier", value);

    // copies only the pointer to the string
    copy_out(out, value);
}

void
xml_parse_bool(const char *value, void *out)
{
    bool v;

    // TODO make case-insensitive
    if (strequ(value, "true"))
        v = true;
    else if (strequ(value, "false"))
        v = false;
    else
        error("Invalid value %s for bool", value);

    copy_out(out, v);
}

void
xml_parse_float(const char *value, void *out)
{
    char *end;
    float v = strtof(value, &end);

    if (value == end)
        error("Invalid value %s for float", value);
    if (v < 0.0f)
        error("Only positive floats are allowed");

    copy_out(out, v);
}

void
xml_parse_double(const char *value, void *out)
{
    char *end;
    double v = strtod(value, &end);

    if (value == end)
        error("Invalid value %s for double", value);
    if (v < 0.0)
        error("Only positive doubles are allowed");

    copy_out(out, v);
}

void
xml_get_single_property(void *out, const xmlNodePtr node, const char *name, xml_parser_func parser)
{
    xmlAttrPtr attr = node->properties;

    if (attr == NULL || attr->next != NULL)
        error("Expected only property %s on line %d", name, node->line);

    const char *prop_name = XMLSTR_TO_STR(attr->name);

    if (!strequ(prop_name, name))
        error("Unexpected attribute on line %d: got: \"%s\", expected: \"%s\"", node->line, prop_name, name);

    xmlChar *xvalue = xmlNodeListGetString(node->doc, attr->children, 1);
    const char *value = XMLSTR_TO_STR(xvalue);

    parser(value, (uint8_t *)out);

    if (parser != xml_parse_string && parser != xml_parse_c_identifier)
        free(xvalue);
}

void
xml_parse_node_by_spec(void *out, const xmlNodePtr node, const xml_attr_spec spec, size_t spec_length)
{
    bool *got = alloca(spec_length * sizeof(bool));
    memset(got, false, spec_length * sizeof(bool));

    LL_FOREACH(xmlAttrPtr, attr, node->properties) {
        const char *name = XMLSTR_TO_STR(attr->name);
        bool found = false;

        for (size_t i = 0; i < spec_length; i++) {
            if (strequ(name, spec[i].name)) {
                // strictly speaking this pointer needs to be freed but we may want to save the string itself
                // so we just don't free it and let it clean up when the program terminates
                xmlChar *xvalue = xmlNodeListGetString(node->doc, attr->children, 1);
                const char *value = XMLSTR_TO_STR(xvalue);

                spec[i].parser_func(value, (uint8_t *)out + spec[i].offset);

                if (spec[i].parser_func != xml_parse_string && spec[i].parser_func != xml_parse_c_identifier)
                    free(xvalue); // free when we don't need the string in the future, TODO strdup when we do need it?

                got[i] = true;
                found = true;
                break;
            }
        }

        if (!found)
            error("Unrecognized attribute %s on line %d", name, node->line);
    }

    for (size_t i = 0; i < spec_length; i++) {
        if (!spec[i].optional && !got[i])
            error("Expected a %s attribute on line %d", spec[i].name, node->line);
    }
}

static void
xml_print_rec(xmlNodePtr base, int *pIndent)
{
    LL_FOREACH(xmlNodePtr, node, base) {
        if (node->type != XML_ELEMENT_NODE)
            continue;

        fprintf(stdout, "%*cChild is <%s> (%i)\n", *pIndent, ' ', node->name, node->type);
        *pIndent += 4;

        LL_FOREACH(xmlAttrPtr, attr, node->properties) {
            xmlChar *value = xmlNodeListGetString(node->doc, attr->children, 1);
            fprintf(stdout, "%*c- Property <%s> \"%s\"\n", *pIndent, ' ', attr->name, XMLSTR_TO_STR(value));
            free(value);
        }

        xml_print_rec(node->children, pIndent);
        *pIndent -= 4;
    }
}

void
xml_print_tree(xmlDocPtr document)
{
    xmlNodePtr root = xmlDocGetRootElement(document);
    int indent = 4;
    fprintf(stdout, "Root is <%s> (%i)\n", root->name, root->type);

    LL_FOREACH(xmlAttrPtr, attr, root->properties) {
        xmlChar *value = xmlNodeListGetString(root->doc, attr->children, 1);
        fprintf(stdout, "%*c- Property <%s> \"%s\"\n", indent, ' ', attr->name, XMLSTR_TO_STR(value));
        free(value);
    }

    xml_print_rec(root->children, &indent);
}