-
Notifications
You must be signed in to change notification settings - Fork 2
/
kamby.h
196 lines (143 loc) · 3.79 KB
/
kamby.h
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#ifndef KAMBY_H
#define KAMBY_H
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
typedef enum {
KA_NONE, KA_NUMBER, KA_STRING, KA_SYMBOL, KA_LIST, KA_BLOCK
} KaType;
typedef struct KaNode {
KaType type;
char *key;
union {
long double *number;
char *string;
struct KaNode *children;
void *value;
};
int *refcount;
struct KaNode *next;
} KaNode;
static inline KaNode *ka_new(KaType type) {
KaNode *node = (KaNode *)calloc(1, sizeof(KaNode));
node->type = type;
node->refcount = (int *)calloc(1, sizeof(int));
return node;
}
static inline void ka_free(KaNode *node) {
while (node) {
KaNode *next = node->next;
if ((*node->refcount)-- <= 0) {
if (node->type >= KA_LIST) ka_free((KaNode *)node->value);
else free(node->value);
free(node->refcount);
}
free(node->key);
free(node);
node = next;
}
}
static inline KaNode *ka_number(long double value) {
KaNode *node = ka_new(KA_NUMBER);
node->number = (long double *)calloc(1, sizeof(long double));
*node->number = value;
return node;
}
static inline KaNode *ka_string(char *value) {
KaNode *node = ka_new(KA_STRING);
node->string = strdup(value);
return node;
}
static inline KaNode *ka_symbol(char *key) {
KaNode *node = ka_new(KA_SYMBOL);
node->key = strdup(key);
return node;
}
static inline KaNode *ka_copy(KaNode *node) {
if (!node) return NULL;
KaNode *copy =
(node->type == KA_NUMBER) ? ka_number(*node->number) :
(node->type == KA_STRING) ? ka_string(node->string) :
(node->type == KA_SYMBOL) ? ka_symbol(node->key) :
ka_new(node->type);
if (copy->type >= KA_LIST) {
copy->value = node->value;
free(copy->refcount);
copy->refcount = node->refcount;
(*node->refcount)++;
}
copy->key = node->key ? strdup(node->key) : NULL;
return copy;
}
static inline KaNode *ka_chain(KaNode *chain, ...) {
va_list args;
va_start(args, chain);
KaNode *last = chain;
while (last) {
while (last->next) last = last->next;
last = last->next = va_arg(args, KaNode *);
}
va_end(args);
return chain;
}
static inline KaNode *ka_list(KaNode *chain, ...) {
va_list args;
va_start(args, chain);
KaNode *last = chain;
while ((last->next = va_arg(args, KaNode *))) {
last = last->next->next ? last->next = ka_copy(last->next) : last->next;
}
KaNode *node = ka_new(KA_LIST);
node->children = chain;
va_end(args);
return node;
}
static inline KaNode *ka_block(KaNode *chain, ...) {
va_list args;
va_start(args, chain);
KaNode *last = chain;
while (last) last = last->next = va_arg(args, KaNode *);
KaNode *node = ka_new(KA_BLOCK);
node->children = chain;
va_end(args);
return node;
}
static inline KaNode *ka_get(char *key, KaNode **chain) {
KaNode *item = *chain;
while (item && strcmp(key, item->key ? item->key : "") != 0) {
item = item->next;
}
return item;
}
static inline KaNode *ka_def(char *key, KaNode *node, KaNode **chain) {
free(node->key);
node->key = strdup(key);
node->next = *chain;
*chain = node;
return node;
}
static inline KaNode *ka_set(char *key, KaNode *node, KaNode **chain) {
KaNode *item = ka_get(key, chain);
if (!item) return ka_def(key, node, chain);
item->type >= KA_LIST ? ka_free((KaNode *)item->value) : free(item->value);
item->type = node->type;
item->value = node->value;
free(node->key);
free(node->refcount);
free(node);
return item;
}
static inline void ka_del(char *key, KaNode **chain) {
KaNode *prev = *chain;
KaNode *item = *chain;
while (item && strcmp(key, item->key ? item->key : "") != 0) {
prev = item;
item = item->next;
}
if (!item) return;
if (item == *chain) *chain = item->next;
else prev->next = item->next;
item->next = NULL;
ka_free(item);
}
#endif