1 #include <lib/jsmn_helper.h> 9 int load_and_parse_json_file(
const char *file_name,
char **file_str_p,
jsmntok_t **tokens_p) {
21 if(file_name == NULL || file_str_p == NULL || tokens_p == NULL)
25 t_file = fopen(file_name,
"r");
31 if(fseek(t_file, 0L, SEEK_END))
34 file_len = ftell(t_file);
38 if(fseek(t_file, 0L, SEEK_SET))
42 buffer = rsalloc(file_len + 1);
44 if(!fread(buffer, (
size_t) file_len, 1, t_file))
51 buffer[file_len] =
'\0';
56 tokens_cnt = jsmn_parse(&parser, buffer, file_len, NULL, 0);
60 tokens = rsalloc(
sizeof(
jsmntok_t) * (
size_t) tokens_cnt);
65 if(jsmn_parse(&parser, buffer, file_len, tokens, tokens_cnt) != tokens_cnt)
92 if((t->type != JSMN_OBJECT && t->type != JSMN_ARRAY) || t->size <= closure->a)
98 while ((t + closure->b)->parent != (t - base_t))
101 return t + closure->b;
109 if(t->type != JSMN_OBJECT)
114 while ((t_aux = get_next_token(base_t, t, &closure)) != NULL) {
120 if(t_aux->type == JSMN_STRING && t_aux->size == 1) {
121 if(strcmp_token(base, t_aux, key) == 0)
133 while (get_next_token(base_t, t, &closure))
143 if(!(get_next_token(base_t, t, &closure)))
146 return get_next_token(base_t, t, &closure);
149 int parse_double_token(
const char *base,
c_jsmntok_t *t,
double *result) {
155 if(t->type != JSMN_PRIMITIVE)
158 size = (size_t) (t->end - t->start);
159 size = size < (64 - 1) ? size : (64 - 1);
161 memcpy(buff, &base[t->start], size);
164 tmp = strtod(buff, &check);
172 int parse_unsigned_token(
const char *base,
c_jsmntok_t *t,
unsigned *result) {
175 if(parse_double_token(base, t, &tmp) < 0 ||
178 tmp > (
unsigned) tmp ||
179 tmp < (
unsigned) tmp)
182 *result = (unsigned) tmp;
186 int strcmp_token(
const char *base,
c_jsmntok_t *t,
const char* str) {
187 int t_len = t->end - t->start;
188 int res = strncmp(&base[t->start], str, t_len);
189 return res == 0 ? str[t_len] !=
'\0' : res;
192 int parse_double_by_key(
c_jsmntok_t *base_t,
const char *base,
c_jsmntok_t *t_obj,
const char *key,
double *result) {
194 if(!(base_t && base && t_obj && key && result))
198 c_jsmntok_t *t = get_value_token_by_key(base_t, base, t_obj, key);
199 if(t == NULL || parse_double_token(base, t, result) < 0) {
206 int parse_unsigned_by_key(
c_jsmntok_t *base_t,
const char *base,
c_jsmntok_t *t_obj,
const char *key,
unsigned *result) {
208 if(!(base_t && base && t_obj && key && result))
212 c_jsmntok_t *t = get_value_token_by_key(base_t, base, t_obj, key);
213 if(t == NULL || parse_unsigned_token(base, t, result) < 0) {
220 int parse_boolean_by_key(
c_jsmntok_t *base_t,
const char *base,
c_jsmntok_t *t_obj,
const char *key,
bool *result) {
222 if(!(base_t && base && t_obj && key && result))
226 c_jsmntok_t *t = get_value_token_by_key(base_t, base, t_obj, key);
227 if(t == NULL || t->type != JSMN_PRIMITIVE) {
232 if(!strcmp_token(base, t,
"false")) {
234 }
else if(!strcmp_token(base, t,
"true")) {
243 int parse_double_array(
c_jsmntok_t *base_t,
const char *base,
c_jsmntok_t *t_arr,
unsigned expected_cnt,
double *result) {
245 if(!(base_t && base && t_arr && result))
249 if(t_arr->type != JSMN_ARRAY || children_count_token(base_t, t_arr) != expected_cnt) {
255 for (i = 0; i < expected_cnt; ++i) {
256 if(parse_double_token(base, get_next_token(base_t, t_arr, &closure), &result[i]) < 0)
263 unsigned parse_string_choice(
c_jsmntok_t *base_t,
const char *base,
c_jsmntok_t *t_str,
unsigned cnt,
const char *choices[cnt]) {
265 if(!(base_t && base && t_str && choices))
269 if(t_str->type != JSMN_STRING) {
274 for (i = 0; i < cnt; ++i) {
275 if(!strcmp_token(base, t_str, choices[i]))
Dynamic Memory Logger and Restorer (DyMeLoR)