forked from cyring/CoreFreq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcorefreq-cli-extra.c
219 lines (194 loc) · 5.55 KB
/
corefreq-cli-extra.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
/*
* CoreFreq (C) 2015-2021 CYRIL INGENIERIE
* Contributors: Andrew Gurinovich ; CyrIng
* Licenses: GPL2
*
* Some ideas taken from https://github.com/cesanta/frozen/
* under Apache 2.0 license
*/
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#include "corefreq-cli-extra.h"
#define UNUSED(expr) do { (void)(expr); } while (0)
/* --[ JSON toolkit ]-- */
struct json_state;
enum JSON_STATE {
DEFAULT, IN_ARRAY, IN_ARRAY2, IN_OBJECT, IN_OBJECT2
};
int json_writer_stdout(struct json_state * state, const char *str, size_t len)
{
UNUSED(state);
return fwrite(str, len, 1, stdout);
}
size_t get_utf8_char_len(unsigned char ch)
{
if ((ch & 0x80) == 0)
return 1;
switch (ch & 0xf0) {
case 0xf0:
return 4;
case 0xe0:
return 3;
default:
return 2;
}
}
/* This routine does NOT handle unicode finely - everything above 0x7f should
* be \u0000 encoded, but this requires us to be utf8 capable. Check the
* following tiny libraries(for future improvement):
* https://github.com/git/git/blob/master/utf8.c
* https://github.com/JeffBezanson/cutef8/blob/master/utf8.c
* https://github.com/sheredom/utf8.h
* https://github.com/JuliaStrings/utf8proc
*/
int json_escape(struct json_state *state, const char *p, size_t len)
{
size_t i, cl;
const char *hex_digits = "0123456789abcdef";
const char *specials = "btnvfr";
int n = 0;
for (i = 0; i < len; i++) {
unsigned char ch = ((unsigned char *) p)[i];
if (ch == '"' || ch == '\\') {
n += state->write(state, "\\", 1);
n += state->write(state, p + i, 1);
} else if (ch >= '\b' && ch <= '\r') {
n += state->write(state, "\\", 1);
n += state->write(state, &specials[ch - '\b'], 1);
} else if (isprint(ch)) {
n += state->write(state, p + i, 1);
} else if ((cl = get_utf8_char_len(ch)) == 1) {
n += state->write(state, "\\u00", 4);
n += state->write(state, &hex_digits[(ch >> 4) % 0xf], 1);
n += state->write(state, &hex_digits[ch % 0xf], 1);
} else {
n += state->write(state, p + i, cl);
i += cl - 1;
}
}
return n;
}
void json_start_object(struct json_state *state)
{
assert(state->depth < JSON_MAX_DEPTH);
/* TODO: assert(state->nested_state[state->depth] != IN_OBJECT); */
if (state->nested_state[state->depth] == IN_ARRAY2 ) {
state->write(state, ", {", 3);
} else {
state->write(state, "{", 1);
}
if (state->nested_state[state->depth] == IN_ARRAY) {
state->nested_state[state->depth] = IN_ARRAY2;
}
if (state->nested_state[state->depth] == IN_OBJECT) {
state->nested_state[state->depth] = IN_OBJECT2;
}
state->nested_state[++state->depth] = IN_OBJECT;
}
void json_end_object(struct json_state *state)
{
assert(state->depth >= 0);
assert(state->nested_state[state->depth] == \
IN_OBJECT || state->nested_state[state->depth] == IN_OBJECT2);
state->write(state, "}", 1);
state->nested_state[state->depth--] = DEFAULT;
}
void json_start_arr(struct json_state *state)
{
assert(state->depth < JSON_MAX_DEPTH);
if (state->nested_state[state->depth] == IN_ARRAY2) {
state->write(state, ", [", 3);
} else {
state->write(state, "[", 1);
}
if (state->nested_state[state->depth] == IN_ARRAY) {
state->nested_state[state->depth] = IN_ARRAY2;
}
if (state->nested_state[state->depth] == IN_OBJECT) {
state->nested_state[state->depth] = IN_OBJECT2;
}
state->nested_state[++state->depth] = IN_ARRAY;
}
void json_end_arr(struct json_state *state)
{
assert(state->depth >= 0);
assert(state->nested_state[state->depth] == \
IN_ARRAY || state->nested_state[state->depth] == IN_ARRAY2);
state->write(state, "]", 1);
state->nested_state[state->depth--] = DEFAULT;
}
void json_key(struct json_state *state, char * key)
{
assert(state->nested_state[state->depth] == \
IN_OBJECT || state->nested_state[state->depth] == IN_OBJECT2);
if (state->nested_state[state->depth] == IN_OBJECT2) {
state->write(state, ", ", 1);
}
state->write(state, "\"", 1);
json_escape(state, key, strlen(key));
state->write(state, "\":", 2);
}
void json_string(struct json_state *state, char * value)
{
assert(state->nested_state[state->depth] != DEFAULT);
if (state->nested_state[state->depth] == IN_ARRAY2) {
state->write(state, ", ", 2);
}
if (state->nested_state[state->depth] == IN_ARRAY) {
state->nested_state[state->depth] = IN_ARRAY2;
}
if (state->nested_state[state->depth] == IN_OBJECT) {
state->nested_state[state->depth] = IN_OBJECT2;
}
state->write(state, "\"", 1);
json_escape(state, value, strlen(value));
state->write(state, "\"", 1);
}
void json_literal(struct json_state *state, char * format, ...)
{
assert(state->nested_state[state->depth] != DEFAULT);
if (state->nested_state[state->depth] == IN_ARRAY2) {
state->write(state, ", ", 2);
}
if (state->nested_state[state->depth] == IN_ARRAY) {
state->nested_state[state->depth] = IN_ARRAY2;
}
if (state->nested_state[state->depth] == IN_OBJECT) {
state->nested_state[state->depth] = IN_OBJECT2;
}
va_list args;
va_start(args, format);
char buf[JSON_MAX_VALUE];
const int rc = vsprintf(buf, format, args);
const size_t bufsz = rc > 0 ? (size_t) rc : 0;
state->write(state, buf, bufsz);
va_end(args);
}
/* --[ UTF toolkit ]-- */
void ISO_8859_To_Unicode(unsigned char *pIn, unsigned char *pOut)
{
while ( (*pIn) != '\0' ) {
if ( (*pIn) > 0x7f ) {
switch ( (*pIn) ) {
case 0xc0 ... 0xff: /* To UTF-8 Ctrl:C3 */
(*pOut) = (*pIn) ^ 0x40;
break;
case 0xa0 ... 0xbf: /* To UTF-8 Ctrl:C2 */
(*pOut) = (*pIn) ^ 0x60;
break;
default:
(*pOut) = 0x20;
break;
}
} else {
(*pOut) = (*pIn);
}
pIn++;
pOut++;
}
(*pOut) = '\0';
}