-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.c
312 lines (268 loc) · 8.65 KB
/
object.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
Copyright (c) 2011, Coleman Stavish
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Coleman Stavish nor the
names of contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL COLEMAN STAVISH BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "eurysta.h"
// global "null" object
cs_json_obj null_ = { OBJ_TYPE_NULL, NULL };
static inline void spec_destructor_(const char *k, void *v) {
cs_json_obj *obj = (cs_json_obj *)v;
switch (obj->type) {
case OBJ_TYPE_OBJECT:
cs_hash_destroy(obj->data);
break;
case OBJ_TYPE_ARRAY:
cs_dll_destroy(obj->data);
break;
case OBJ_TYPE_STRING:
free(obj->data);
break;
case OBJ_TYPE_NUMBER:
free(obj->data);
break;
case OBJ_TYPE_NULL:
return;
}
free(obj);
}
static void generic_destructor_(void *v) {
spec_destructor_(NULL, v);
}
static inline void print_string_(const char *s, FILE *f) {
putc('"', f);
while (*s) {
if (*s == '"')
putc('\\', f);
putc(*s++, f);
}
putc('"', f);
}
static inline void print_hash_(cs_hash_tab *t, FILE *f) {
putc('{', f);
for (size_t i = 0, c = 0; i < t->size, c < t->count; i++) {
for (cs_knode *n = t->buckets[i]; n != NULL; n = n->next) {
print_string_(n->key, f);
putc(':', f);
cs_object_print(n->val, f);
if (t->count - c++ > 1)
putc(',', f);
}
}
putc('}', f);
}
static inline void print_array_(cs_dll *list, FILE *f) {
putc('[', f);
cs_dll_node *n = list->start;
for (int i = 0; n != NULL; i++, n = n->next) {
cs_object_print((cs_json_obj *)n->data, f);
if (i < list->size - 1)
putc(',', f);
}
putc(']', f);
}
void cs_object_destroy(cs_json_obj *o) {
generic_destructor_(o);
}
void cs_object_print(cs_json_obj *obj, FILE *f) {
switch (obj->type) {
case OBJ_TYPE_STRING:
print_string_((const char *)obj->data, f);
break;
case OBJ_TYPE_NUMBER:
fprintf(f, "%g", *(double *)obj->data);
break;
case OBJ_TYPE_OBJECT:
print_hash_((cs_hash_tab *)obj->data, f);
break;
case OBJ_TYPE_ARRAY:
print_array_((cs_dll *)obj->data, f);
break;
case OBJ_TYPE_NULL:
fprintf(f, "null");
break;
case OBJ_TYPE_BOOL:
fprintf(f, "%s", ((uintptr_t)obj->data & 1) ? "true" : "false");
}
}
cs_json_obj *cs_object_create(void) {
cs_json_obj *obj = malloc(sizeof(cs_json_obj));
if (obj == NULL)
return NULL;
obj->type = OBJ_TYPE_OBJECT;
if ((obj->data = cs_hash_create_opt(32, 0.75f, 0.25f)) == NULL) {
free(obj);
return NULL;
}
((cs_hash_tab *)(obj->data))->cleanup = spec_destructor_;
((cs_hash_tab *)(obj->data))->copy_keys = 0;
return obj;
}
cs_json_obj *cs_array_create(void) {
cs_json_obj *obj = malloc(sizeof(cs_json_obj));
if (obj == NULL)
return NULL;
obj->type = OBJ_TYPE_ARRAY;
if ((obj->data = cs_dll_create(generic_destructor_, NULL)) == NULL) {
free(obj);
return NULL;
}
return obj;
}
cs_json_obj *cs_bool_create(uint8_t val) {
cs_json_obj *obj = malloc(sizeof(cs_json_obj));
if (obj == NULL)
return NULL;
obj->type = OBJ_TYPE_BOOL;
obj->data = (void *)(uintptr_t)(val & 1);
return obj;
}
cs_json_obj *cs_string_create(char *val, uint8_t assign) {
if (val == NULL)
return NULL;
cs_json_obj *str = malloc(sizeof(cs_json_obj));
if (str == NULL)
return NULL;
str->type = OBJ_TYPE_STRING;
str->data = (assign) ? val : strdup(val);
return str;
}
cs_json_obj *cs_number_create(double val) {
cs_json_obj *num = malloc(sizeof(cs_json_obj));
if (num == NULL)
return NULL;
num->type = OBJ_TYPE_NUMBER;
if ((num->data = malloc(sizeof(double))) == NULL) {
free(num);
return NULL;
}
*(double *)(num->data) = val;
return num;
}
char *cs_string_get_val(cs_json_obj *string) {
if (string != NULL && string->type == OBJ_TYPE_STRING) {
return string->data;
}
return NULL;
}
uint8_t cs_string_set_val(cs_json_obj *string, const char *value) {
if (string != NULL && string->type == OBJ_TYPE_STRING) {
// FIXME: it's unsafe to assume the previous value was dynamically allocated
free(string->data);
string->data = strdup(value);
return 1;
}
return 0;
}
inline size_t cs_string_get_len(cs_json_obj *string) {
return strlen((char *)string->data);
}
double cs_number_get_val(cs_json_obj *number, uint8_t *success) {
uint8_t s = 0;
double v = 0;
if (number != NULL && number->type == OBJ_TYPE_NUMBER) {
s = 1;
v = *(double *)number->data;
}
if (success != NULL)
*success = s;
return v;
}
uint8_t cs_number_set_val(cs_json_obj *number, double value) {
if (number != NULL && number->type == OBJ_TYPE_NUMBER) {
free(number->data);
if ((number->data = malloc(sizeof(double))) != NULL) {
*(double *)number->data = value;
return 1;
}
}
return 0;
}
uint8_t cs_bool_get_val(cs_json_obj *boolean, uint8_t *success) {
uint8_t s = 0, v = 0;
if (boolean != NULL) {
s = 1;
v = (uintptr_t)boolean->data & 1;
}
if (success != NULL)
*success = s;
return v;
}
uint8_t cs_bool_set_val(cs_json_obj *boolean, uint8_t value) {
if (boolean != NULL && boolean->type == OBJ_TYPE_BOOL) {
boolean->data = (void *)((uintptr_t)value & 1);
return 1;
}
return 0;
}
cs_json_obj *cs_object_get_val(cs_json_obj *object, const char *key) {
if (object != NULL && key != NULL && object->type == OBJ_TYPE_OBJECT) {
return cs_hash_get((cs_hash_tab *)object->data, key);
}
return NULL;
}
uint8_t cs_object_set_val(cs_json_obj *object, const char *key, cs_json_obj *value) {
if (object != NULL && object->type == OBJ_TYPE_OBJECT) {
if (object->data != NULL) {
cs_hash_set((cs_hash_tab *)object->data, key, value);
return 1;
}
}
return 0;
}
inline size_t cs_object_get_size(cs_json_obj *object) {
return ((cs_hash_tab *)object)->count;
}
void cs_object_del_val(cs_json_obj *object, const char *key) {
if (object && object->type == OBJ_TYPE_OBJECT) {
cs_json_obj *o = cs_hash_del((cs_hash_tab *)object->data, key);
if (o)
cs_object_destroy(o);
}
}
cs_json_obj *cs_array_get_val(cs_json_obj *array, uint32_t index) {
if (array != NULL && array->type == OBJ_TYPE_ARRAY) {
return cs_dll_get((cs_dll *)array->data, index);
}
return NULL;
}
uint8_t cs_array_set_val(cs_json_obj *array, uint32_t index, cs_json_obj *value) {
if (array != NULL && array->type == OBJ_TYPE_ARRAY) {
if (array->data) {
return cs_dll_set((cs_dll *)array->data, value, index);
}
}
return 0;
}
void cs_array_del_val(cs_json_obj *array, uint32_t index) {
if (array && array->type == OBJ_TYPE_ARRAY) {
cs_json_obj *o = cs_dll_del((cs_dll *)array->data, index);
if (o)
cs_object_destroy(o);
}
}
inline size_t cs_array_get_len(cs_json_obj *array) {
return ((cs_dll *)array->data)->size;
}