aboutsummaryrefslogtreecommitdiffstats
path: root/moon-abe/pbc-0.5.14/misc/symtab.c
blob: 9d62f590b255c1d1da60f25adba01cf1aaf48dfc (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
#include <stdlib.h>
#include <string.h>
#include "pbc_memory.h"

#include "symtab.h"

struct entry_s {
  char *key;
  void *data;
};
typedef struct entry_s *entry_ptr;
typedef struct entry_s entry_t[1];

void symtab_init(symtab_t t) {
  darray_init(t->list);
}

static void clear(void *data) {
  entry_ptr e = data;
  pbc_free(e->key);
  pbc_free(e);
}

void symtab_clear(symtab_t t) {
  darray_forall(t->list, clear);
  darray_clear(t->list);
}

void symtab_put(symtab_t t, void *data, const char *key) {
  int i, n = t->list->count;
  entry_ptr e;
  for (i=0; i<n; i++) {
    e = t->list->item[i];
    if (!strcmp(e->key, key)) goto doit;
  }
  e = pbc_malloc(sizeof(entry_t));
  e->key = pbc_strdup(key);
  darray_append(t->list, e);
doit:
  e->data = data;
}

int symtab_has(symtab_t t, const char *key) {
  int i, n = t->list->count;
  for (i = 0; i < n; i++) {
    entry_ptr e = t->list->item[i];
    if (!strcmp(e->key, key)) return 1;
  }
  return 0;
}

void *symtab_at(symtab_t t, const char *key) {
  int i, n = t->list->count;
  for (i=0; i<n; i++) {
    entry_ptr e = t->list->item[i];
    if (!strcmp(e->key, key)) return e->data;
  }
  return NULL;
}

void symtab_forall_data(symtab_t t, void (*func)(void *)) {
  int i, n = t->list->count;
  for (i=0; i<n; i++) {
    entry_ptr e = t->list->item[i];
    func(e->data);
  }
}