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
|
#include <string.h>
#include <assert.h>
#include <string.h>
#include <malloc.h>
#include <stdio.h>
#include "namereg.h"
struct namereg_entry {
enum namereg_type type;
char *name;
void *data;
};
void namereg_free(struct core *c) {
assert(c);
if (!c->namereg)
return;
assert(hashset_ncontents(c->namereg) == 0);
hashset_free(c->namereg, NULL, NULL);
}
const char *namereg_register(struct core *c, const char *name, enum namereg_type type, void *data, int fail) {
struct namereg_entry *e;
char *n = NULL;
int r;
assert(c && name && data);
if (!c->namereg) {
c->namereg = hashset_new(idxset_string_hash_func, idxset_string_compare_func);
assert(c->namereg);
}
if ((e = hashset_get(c->namereg, name)) && fail)
return NULL;
if (!e)
n = strdup(name);
else {
unsigned i;
size_t l = strlen(name);
n = malloc(l+3);
assert(n);
for (i = 1; i <= 99; i++) {
snprintf(n, l+2, "%s%u", name, i);
if (!(e = hashset_get(c->namereg, n)))
break;
}
if (e) {
free(n);
return NULL;
}
}
assert(n);
e = malloc(sizeof(struct namereg_entry));
assert(e);
e->type = type;
e->name = n;
e->data = data;
r = hashset_put(c->namereg, e->name, e);
assert (r >= 0);
return e->name;
}
void namereg_unregister(struct core *c, const char *name) {
struct namereg_entry *e;
int r;
assert(c && name);
e = hashset_get(c->namereg, name);
assert(e);
r = hashset_remove(c->namereg, name);
assert(r >= 0);
free(e->name);
free(e);
}
void* namereg_get(struct core *c, const char *name, enum namereg_type type) {
struct namereg_entry *e;
assert(c && name);
if (!(e = hashset_get(c->namereg, name)))
if (e->type == e->type)
return e->data;
return NULL;
}
|