-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha_basic_tests.c
executable file
·162 lines (147 loc) · 4.07 KB
/
a_basic_tests.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
#include "./ekon.h"
#include "./lsp/ekon_schema.h"
#include "./tests/c_api/utils.h"
#include "hashmap.h"
#include <stdio.h>
#include <string.h>
// Get the EKON and Set some EKON
void getAndSet(EkonValue *src, EkonValue *des) {
// Set EKON Type
const EkonType t = ekonValueType(src);
if (t == 0)
return;
switch (t) {
case EKON_TYPE_ARRAY: {
ekonValueSetArray(des);
EkonValue *next = ekonValueBegin(src);
while (next != 0) {
EkonValue *v = ekonValueNew(des->a);
getAndSet(next, v);
if (ekonValueArrayAddFast(des, v) != true) {
return;
}
next = ekonValueNext(next);
}
break;
}
case EKON_TYPE_OBJECT: {
ekonValueSetObj(des); // init
EkonValue *next = ekonValueBegin(src);
while (next != 0) {
EkonValue *v = ekonValueNew(des->a);
uint32_t keyLen = 0;
EkonOption option;
const char *key = ekonValueGetKey(next, &option, &keyLen);
if (src->n->keymap != 0 &&
ekonHashmapGet(src->n->keymap, key, keyLen) != NULL) {
// key is already present
}
ekonValueSetKeyFast(v, key);
getAndSet(next, v);
if (ekonValueObjAddFast(des, v) != true)
return;
next = ekonValueNext(next);
}
break;
}
case EKON_TYPE_BOOL: {
bool outBool;
const bool b = ekonValueGetBool(src, &outBool);
if (b == 0)
return;
ekonValueSetBool(des, outBool);
break;
}
case EKON_TYPE_NUMBER: {
const char *str = ekonValueGetNumStr(src);
if (str == 0)
return;
if (ekonValueSetNumStr(des, str) == false)
return;
break;
}
case EKON_TYPE_NULL: {
if (ekonValueIsNull(src) == false)
return;
ekonValueSetNull(des);
break;
}
case EKON_TYPE_STRING: {
EkonOption option;
const char *str = ekonValueGetStr(src, &option);
if (str == 0)
return;
if (!ekonValueSetStrFast(des, str)) {
return;
}
break;
}
}
}
bool EKON_Parse(EkonValue *srcV, const char *srcEkon) {
char *errMessage = NULL;
char *schema = NULL;
bool success = ekonValueParseFast(srcV, srcEkon, &errMessage, &schema);
ekonParseSchema(schema, strlen(schema));
if (success == false)
printf("Error Message: %s", errMessage);
free(schema);
return success;
}
int main() {
printf("Size of EkonNode: %ld\n--", sizeof(EkonNode) + sizeof(EkonHashmap));
const char *str = "specs1.ekon";
const char *srcEkon = readFromFile(str);
/* printf("%s\n", srcEkon); */
// Allocate new chunk of memory
EkonAllocator *a = ekonAllocatorNew();
// Add a new value
EkonValue *srcV = ekonValueNew(a);
EkonValue *desV = ekonValueNew(a);
// ... EKON ..
printf("----- Parsing... ------\n");
bool ret = EKON_Parse(srcV, srcEkon);
if (!ret) {
return 1;
}
printf("----- getAndSet... ----\n");
/* printf("%s\n", src); */
// Get and Set EKON
getAndSet(srcV, desV);
// DesEkon
const char *des = ekonValueStringify(desV, true);
printf("~>stringified:\n%s\n", (des));
/* printf("------beauty--------\n"); */
char *err2;
const char *beautifiedDes = ekonBeautify(des, &err2,
(EkonBeautifyOptions){
false,
false,
false,
});
if (beautifiedDes == 0) {
printf("Beautification Error: %s\n", err2);
return 1;
}
printf("~~>Beautified:\n%s\n", beautifiedDes);
/* const char *strUnes = ekonValueStringifyUnEscaped(desV); */
/* if (strUnes == 0) { */
/* printf("Unescape Str Error\n"); */
/* return 1; */
/* } */
/* printf("~>Unescaped:%s\n", (strUnes)); */
/* printf("------json------\n"); */
/* const char *json = ekonValueStringifyToJSON(desV); */
/* if (json == 0) { */
/* fprintf(stderr, "Error stringifying JSON"); */
/* return 1; */
/* } */
/* printf("~~JSON->%s\n", json); */
// ReleaseAllocator
ekonAllocatorRelease(a);
free((void *)srcEkon);
/* free((void *)des); */
/* free((void *)src); */
/* free(err2); */
return 0;
}