-
Notifications
You must be signed in to change notification settings - Fork 1
/
ftmap.c
115 lines (101 loc) · 2.83 KB
/
ftmap.c
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
#include <stdlib.h>
#include <string.h>
#include "plumber.h"
int plumber_ftmap_init(plumber_data *plumb)
{
int pos;
for(pos = 0; pos < 256; pos++) {
plumb->ftmap[pos].nftbl = 0;
plumb->ftmap[pos].root.to_delete = plumb->delete_ft;
plumb->ftmap[pos].last= &plumb->ftmap[pos].root;
}
return PLUMBER_OK;
}
int plumber_ftmap_add(plumber_data *plumb, const char *str, sp_ftbl *ft)
{
plumber_ftbl *new;
plumber_add(plumb, str, &new);
new->ud = (void *)ft;
new->type = PTYPE_TABLE;
return PLUMBER_OK;
}
int plumber_ftmap_add_userdata(plumber_data *plumb, const char *str, void *ud)
{
plumber_ftbl *new;
plumber_add(plumb, str, &new);
new->ud = ud;
new->type = PTYPE_USERDATA;
return PLUMBER_OK;
}
int plumber_ftmap_add_function(plumber_data *plumb,
const char *str, plumber_dyn_func f, void *ud)
{
sporth_fload_d *fd = malloc(sizeof(sporth_fload_d));
fd->fun = f;
fd->ud = ud;
return plumber_ftmap_add_userdata(plumb, str, (void *)fd);
}
int plumber_ftmap_search(plumber_data *plumb, const char *str, sp_ftbl **ft)
{
plumber_ftbl *ftbl;
if(plumber_search(plumb, str, &ftbl) != PLUMBER_OK) {
return PLUMBER_NOTOK;
} else if(ftbl->type != PTYPE_TABLE) {
plumber_print(plumb, "Error: value '%s' is not of type ftable\n", str);
return PLUMBER_NOTOK;
} else {
*ft = (sp_ftbl *)ftbl->ud;
return PLUMBER_OK;
}
}
int plumber_ftmap_search_userdata(plumber_data *plumb, const char *str, void **ud)
{
plumber_ftbl *ftbl;
if(plumber_search(plumb, str, &ftbl) != PLUMBER_OK) {
return PLUMBER_NOTOK;
} else if(ftbl->type != PTYPE_USERDATA) {
plumber_print(plumb, "Error: value '%s' is not of type userdata\n", str);
return PLUMBER_NOTOK;
} else {
*ud = ftbl->ud;
return PLUMBER_OK;
}
}
int plumber_ftmap_delete(plumber_data *plumb, char mode)
{
plumb->delete_ft = mode;
return PLUMBER_OK;
}
void plumber_ftmap_dump(plumber_ftentry *ft)
{
uint32_t i, k;
plumber_ftbl *cur, *next;
for(i = 0; i < 256; i ++) {
cur = ft[i].root.next;
for(k = 0; k < ft[i].nftbl; k++) {
next = cur->next;
printf("%s\n", cur->name);
cur = next;
}
}
}
int plumber_ftmap_destroy(plumber_data *plumb)
{
int pos, n;
plumber_ftbl *ftbl, *next;
for(pos = 0; pos < 256; pos++) {
ftbl = plumb->ftmap[pos].root.next;
for(n = 0; n < plumb->ftmap[pos].nftbl; n++) {
next = ftbl->next;
free(ftbl->name);
if(ftbl->to_delete) {
if(ftbl->type == PTYPE_TABLE)
sp_ftbl_destroy((sp_ftbl **)&ftbl->ud);
else free(ftbl->ud);
}
free(ftbl);
ftbl = next;
}
}
return PLUMBER_OK;
}