-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmain.c
More file actions
75 lines (71 loc) · 1.52 KB
/
Copy pathmain.c
File metadata and controls
75 lines (71 loc) · 1.52 KB
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
#include "stable.h"
#include <inttypes.h>
#include <stdio.h>
#include <assert.h>
static void
print_string(void *ud, const char *string, size_t sz) {
printf("%s",string);
}
static void
dump(struct table *root, int depth) {
size_t size = stable_cap(root);
struct table_key keys[size];
size = stable_keys(root,keys,size);
int i;
for (i=0;i<depth;i++)
printf(" ");
for (i=0;i<size;i++) {
if (keys[i].key == NULL) {
printf("[%d] = ",keys[i].sz_idx);
} else {
printf("%s = ",keys[i].key);
}
switch(keys[i].type) {
case ST_NIL:
printf("nil");
break;
case ST_NUMBER: {
double d = stable_number(root, keys[i].key, keys[i].sz_idx);
printf("%lf",d);
break;
}
case ST_BOOLEAN: {
int b = stable_boolean(root, keys[i].key, keys[i].sz_idx);
printf("%s",b ? "true" : "false");
break;
}
case ST_ID: {
uint64_t id = stable_id(root, keys[i].key, keys[i].sz_idx);
printf("%" PRIu64,id);
break;
}
case ST_STRING:
stable_string(root,keys[i].key, keys[i].sz_idx, print_string, NULL);
break;
case ST_TABLE: {
struct table * sub = stable_table(root, keys[i].key, keys[i].sz_idx);
printf(":\n");
dump(sub,depth+1);
break;
}
default:
assert(0);
break;
}
printf("\n");
}
}
static void
test(struct table *root) {
struct table * sub = stable_settable(root,TKEY("hello"));
stable_setnumber(root,TINDEX(10),100);
stable_setstring(sub,TINDEX(0),TKEY("world"));
dump(root,0);
}
int
main() {
struct table * t = stable_create();
test(t);
stable_release(t);
return 0;
}