-
Notifications
You must be signed in to change notification settings - Fork 1
/
bentypes.c
155 lines (146 loc) · 2.79 KB
/
bentypes.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "bdecode.h"
bd_list* bd_list_create()
{
bd_list* list = malloc(sizeof(bd_list));
// TODO: Nomem check hurr.
list->entries = malloc(sizeof(bd_entry) * 2);
list->allocated = 2;
list->used = 0;
return list;
}
void bd_list_add(bd_list* list, enum bd_type type, void* data)
{
bd_entry* e;
if(list->used == list->allocated)
{
list->allocated = list->allocated + (list->allocated / 2);
e = realloc(list->entries, sizeof(bd_entry) * (list->allocated));
list->entries = e;
}
list->entries[list->used].type = type;
list->entries[list->used].data = data;
list->used++;
}
void bd_list_destroy(bd_list* list)
{
int i;
for(i = 0; i < list->used; i++)
{
switch(list->entries[i].type)
{
case DICTIONARY:
bd_dict_destroy(list->entries[i].dict);
break;
case LIST:
bd_list_destroy(list->entries[i].list);
break;
case STRING:
free(list->entries[i].str);
break;
default:
break;
}
}
free(list->entries);
free(list);
}
void bd_dict_add(bd_dict** dict, char* key, enum bd_type type, void* data)
{
bd_dict* kvp = malloc(sizeof(bd_dict));
// TODO: Nomem check hurr.
kvp->key = key;
kvp->data = data;
kvp->type = type;
kvp->next = *dict;
*dict = kvp;
}
bd_entry* bd_dict_find(bd_dict* dict, char* key)
{
bd_dict* iter = dict;
while(iter)
{
if(strcmp(iter->key, key) == 0)
{
return iter;
}
iter = iter->next;
}
return 0;
}
void bd_dict_destroy(bd_dict* dict)
{
bd_dict* dt;
bd_dict* d = dict;
while(d)
{
dt = d->next;
switch(d->type)
{
case DICTIONARY:
bd_dict_destroy(d->dict);
break;
case LIST:
bd_list_destroy(d->list);
break;
case STRING:
free(d->str);
break;
default:
break;
}
free(d);
d = dt;
}
}
void bd_dict_print(bd_dict* dict, int indent)
{
indent = (indent > 32) ? 0 : indent;
bd_dict* itr = dict;
while(itr)
{
printf("%*s" "%s::\n", indent, " ", itr->key);
switch(itr->type)
{
case DICTIONARY:
bd_dict_print(itr->data, indent + 1);
break;
case STRING:
printf("%*s" "%s\n", indent + 1, " ", itr->str);
break;
case NUMBER:
printf("%*s" "%lli\n", indent + 1, " ", (long long)itr->data);
break;
case LIST:
bd_list_print(itr->data, indent + 1);
break;
default:
break;
}
itr = itr->next;
}
}
void bd_list_print(bd_list* list, int indent)
{
indent = (indent > 32) ? 0 : indent;
int i;
for(i = 0; i < list->used; i++)
{
switch(list->entries[i].type)
{
case DICTIONARY:
bd_dict_print(list->entries[i].dict, indent);
break;
case NUMBER:
printf("%*s" "%lli\n", indent, " ", (long long)list->entries[i].data);
break;
case LIST:
bd_list_print(list->entries[i].list, indent + 1);
break;
case STRING:
printf("%*s" "%s\n", indent, " ", list->entries[i].str);
break;
default:
break;
}
}
}