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
|
// (C) 2017 <>< Joshua Allen, Under MIT/x11 License.
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "json_helpers.h"
#include <jsmn.h>
#if !defined(__FreeBSD__) && !defined(__APPLE__)
#include <malloc.h>
#endif
#ifdef _MSC_VER
#include <stdarg.h>
// Windows doesn't provide asprintf, so we need to make it.
int asprintf(char **strp, const char *fmt, ...)
{
char* buff = NULL;
va_list listPointer;
va_start( listPointer, fmt );
size_t lenNeeded = _vscprintf(fmt, listPointer) + 1; // add one for a terminating null
if (lenNeeded > 1)
{
buff = (char*)malloc(lenNeeded);
if (buff)
{
int bytesWritten = _vsnprintf(buff, lenNeeded, fmt, listPointer);
if (bytesWritten < 0)
{
free(buff);
buff = NULL;
}
}
}
if (strp)
{
*strp = buff;
}
return (int)lenNeeded;
}
#endif
void json_write_float_array(FILE* f, const char* tag, float* v, uint8_t count) {
uint8_t i = 0;
char * str1 = NULL;
char * str2 = NULL;
asprintf(&str1,"\"%s\":[", tag);
for (i=0;i<count;++i) {
if ( (i+1) < count) {
asprintf(&str2, "%s\"%f\"", str1,v[i]);
} else {
asprintf(&str2, "%s\"%f\",", str1,v[i]);
}
free(str1);
str1=str2;
str2=NULL;
}
asprintf(&str2, "%s]", str1);
fputs(str2,f);
free(str1);
free(str2);
}
void json_write_double_array(FILE* f, const char* tag, double* v, uint8_t count) {
uint8_t i = 0;
char * str1 = NULL;
char * str2 = NULL;
asprintf(&str1,"\"%s\":[", tag);
for (i=0;i<count;++i) {
if (i<(count-1)) {
asprintf(&str2, "%s\"%f\",", str1,v[i]);
} else {
asprintf(&str2, "%s\"%f\"", str1,v[i]);
}
free(str1);
str1=str2;
str2=NULL;
}
asprintf(&str2, "%s]", str1);
fputs(str2,f);
free(str1);
free(str2);
}
void json_write_uint32(FILE* f, const char* tag, uint32_t v) {
fprintf(f, "\"%s\":\"%d\"", tag, v);
}
void json_write_float(FILE* f, const char* tag, float v) {
fprintf(f, "\"%s\":\"%f\"", tag, v);
}
void json_write_str(FILE* f, const char* tag, const char* v) {
fprintf(f, "\"%s\":\"%s\"", tag, v);
}
void (*json_begin_object)(char* tag) = NULL;
void (*json_end_object)() = NULL;
void (*json_tag_value)(char* tag, char** values, uint8_t count) = NULL;
uint32_t JSON_STRING_LEN;
char* load_file_to_mem(const char* path) {
FILE * f = fopen( path, "r" );
if (f==NULL) return NULL;
fseek( f, 0, SEEK_END );
int len = ftell( f );
fseek( f, 0, SEEK_SET );
char * JSON_STRING = malloc( len + 1);
memset(JSON_STRING,0,len+1);
fread( JSON_STRING, len, 1, f );
fclose( f );
return JSON_STRING;
}
static char* substr(const char* str, uint32_t start, uint32_t end, uint32_t npos) {
uint32_t l = end-start+1;
if (end<=start || end>=npos) return NULL;
char* x = malloc(l);
memcpy(x,str+start,l);
x[l-1] = '\0';
return x;
}
static uint16_t json_load_array(const char* JSON_STRING, jsmntok_t* tokens, uint16_t size, char* tag) {
jsmntok_t* t = tokens;
uint16_t i = 0;
char** values;
values = alloca(sizeof(char*) * size);
for (i=0;i<size;++i) {
t = tokens+i;
values[i] = substr(JSON_STRING, t->start, t->end, JSON_STRING_LEN);
}
if (json_tag_value != NULL) json_tag_value(tag, values, (uint8_t)i);
for (i=0;i<size;++i) free(values[i]);
return size;
}
void json_load_file(const char* path) {
uint32_t i = 0;
char* JSON_STRING = load_file_to_mem(path);
if (JSON_STRING==NULL) return;
JSON_STRING_LEN = (uint32_t)strlen(JSON_STRING);
jsmn_parser parser;
jsmn_init(&parser);
int32_t items = jsmn_parse(&parser, JSON_STRING, JSON_STRING_LEN, NULL, 0);
if (items < 0) return;
jsmntok_t* tokens = malloc(items * sizeof(jsmntok_t));
jsmn_init(&parser);
items = jsmn_parse(&parser, JSON_STRING, JSON_STRING_LEN, tokens, items);
int16_t children = -1;
for (i=0; i<(unsigned int)items; i+=2)
{
//increment i on each successful tag + values combination, not individual tokens
jsmntok_t* tag_t = tokens+i;
jsmntok_t* value_t = tokens+i+1;
char* tag = substr(JSON_STRING, tag_t->start, tag_t->end, JSON_STRING_LEN);
char* value = substr(JSON_STRING, value_t->start, value_t->end, JSON_STRING_LEN);
printf("%d %d c:%d %d %s \n", tag_t->start, tag_t->end, tag_t->size, tag_t->type, tag);
if (value_t->type == JSMN_ARRAY) {
i += json_load_array(JSON_STRING, tokens+i+2,value_t->size, tag); //look at array children
} else if (value_t->type == JSMN_OBJECT) {
printf("Begin Object\n");
if (json_begin_object != NULL) json_begin_object(tag);
children = value_t->size +1; //+1 to account for this loop where we are not yed parsing children
// i += decode_jsmn_object(JSON_STRING, tokens+i+2,value_t->size);
}
else {
if (json_tag_value != NULL) json_tag_value(tag, &value, 1);
}
if (children>=0) children--;
if (children == 0) {
children = -1;
printf("End Object\n");
if (json_end_object!=NULL) json_end_object();
}
// printf("%d %s \n", value_t->type, tag);
free(tag);
free(value);
}
free(JSON_STRING);
}
|