aboutsummaryrefslogtreecommitdiff
path: root/scripts/write_config/symlist.c
blob: 71d9470bd2ddca92b16b174a40ea64e3ef4af509 (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
#include "symlist.h"

struct symlist *symlist_read(FILE * f) {
    struct symlist *ret;
    ret = malloc(sizeof(struct symlist));
    ret->size = 1;
    ret->array = malloc(ret->size * sizeof(struct symlist_el));

    unsigned int id;
    char *w;
    size_t w_pos = 0, w_size = 2;
    w = malloc((w_size + 1) * sizeof(char));

    int c;
    do {
        c = fgetc(f);
        if (c == EOF || c == '\n') {
            w[w_pos] = '\0';
            if ((size_t) id > ret->size) {
                ret->size *= 2;
                ret->array =
                    realloc(ret->array,
                            ret->size * sizeof(struct symlist_el));
            }
            ret->array[(size_t) id - 1].id = id;
            ret->array[(size_t) id - 1].sym = sym_lookup(w, 0);
            w_pos = 0;
        } else if (c == ':') {
            w[w_pos] = '\0';
            id = atoi(w);
            w_pos = 0;
        } else {
            if (w_pos >= w_size) {
                w_size *= 2;
                w = realloc(w, (w_size + 1) * sizeof(char));
            }
            w[w_pos++] = (char) c;
        }
    } while (c != EOF);

    return ret;
}

struct symbol *symlist_get(struct symlist *sl, unsigned int id) {
    return sl->array[id].sym;
}