]> rtime.felk.cvut.cz Git - linux-conf-perf.git/blob - scripts/write_config/symlist.c
Rewrite configuration script
[linux-conf-perf.git] / scripts / write_config / symlist.c
1 #include "symlist.h"
2
3 #define NONAMEGEN "NONAMEGEN"
4
5 struct symlist *symlist_read(FILE * f) {
6     struct symlist *ret;
7     ret = malloc(sizeof(struct symlist));
8     ret->size = 1;
9     ret->maxid = 0;
10     ret->array = malloc(ret->size * sizeof(struct symlist_el));
11
12     unsigned int id;
13     char *w;
14     size_t w_pos = 0, w_size = 2;
15     w = malloc((w_size + 1) * sizeof(char));
16
17     int c;
18     do {
19         c = fgetc(f);
20         if (c == '\n') {
21             w[w_pos] = '\0';
22             if ((size_t) id > ret->size) {
23                 ret->size *= 2;
24                 ret->array =
25                     realloc(ret->array,
26                             ret->size * sizeof(struct symlist_el));
27             }
28             if (id > ret->maxid)
29                 ret->maxid = id;
30             ret->array[(size_t) id - 1].id = id;
31             if (!strncmp(w, NONAMEGEN, strlen(NONAMEGEN)))
32                 ret->array[(size_t) id - 1].sym = NULL;
33             else
34                 ret->array[(size_t) id - 1].sym = sym_lookup(w, 0);
35             w_pos = 0;
36         } else if (c == ':') {
37             w[w_pos] = '\0';
38             id = atoi(w);
39             w_pos = 0;
40         } else {
41             if (w_pos >= w_size) {
42                 w_size *= 2;
43                 w = realloc(w, (w_size + 1) * sizeof(char));
44             }
45             w[w_pos++] = (char) c;
46         }
47     } while (c != EOF);
48
49     return ret;
50 }
51
52 struct symbol *symlist_get(struct symlist *sl, unsigned int id) {
53     return sl->array[id].sym;
54 }