GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_json_reader.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2/*
3 * Copyright (C) 2021-2022 Cyril Hrubis <metan@ucw.cz>
4 */
5
17#ifndef GP_JSON_READER_H
18#define GP_JSON_READER_H
19
20#include <stdio.h>
21#include <core/gp_compiler.h>
23
32#define GP_JSON_READER_INIT(buf, buf_len) { \
33 .max_depth = GP_JSON_RECURSION_MAX, \
34 .err_print = GP_JSON_ERR_PRINT, \
35 .err_print_priv = GP_JSON_ERR_PRINT_PRIV, \
36 .json = buf, \
37 .len = buf_len, \
38}
39
45 const char *json;
47 size_t len;
49 size_t off;
51 size_t sub_off;
53 unsigned int depth;
55 unsigned int max_depth;
56
58 void (*err_print)(void *err_print_priv, const char *line);
59 void *err_print_priv;
60
61 char err[GP_JSON_ERR_MAX];
62 char buf[];
63};
64
75
77 char *buf;
78 size_t buf_size;
79
86 size_t idx;
87
89 union {
93 long long val_int;
95 const char *val_str;
96 };
97
104 double val_float;
105
108
109 char buf__[];
110};
111
119
126
133static inline int gp_json_val_valid(struct gp_json_val *res)
134{
135 return !!res->type;
136}
137
148void gp_json_err(gp_json_reader *self, const char *fmt, ...)
149 GP_FMT_PRINTF(2, 3);
150
162
173void gp_json_warn(gp_json_reader *self, const char *fmt, ...)
174 GP_FMT_PRINTF(2, 3);
175
182static inline int gp_json_reader_err(gp_json_reader *self)
183{
184 return !!self->err[0];
185}
186
194
202
212
225
239#define GP_JSON_OBJ_FOREACH(self, res) \
240 for (gp_json_obj_first(self, res); gp_json_val_valid(res); gp_json_obj_next(self, res))
241
250size_t gp_json_lookup(const void *arr, size_t memb_size, size_t list_len,
251 const char *key);
252
267
279
280static inline size_t gp_json_obj_lookup(const gp_json_obj *obj, const char *key)
281{
282 return gp_json_lookup(obj->attrs, sizeof(*obj->attrs), obj->attr_cnt, key);
283}
284
286#define GP_JSON_OBJ_ATTR(keyv, typev) \
287 {.key = keyv, .type = typev}
288
290#define GP_JSON_OBJ_ATTR_IDX(key_idx, keyv, typev) \
291 [key_idx] = {.key = keyv, .type = typev}
292
304 const struct gp_json_obj *obj, const struct gp_json_obj *ign);
305
320 const struct gp_json_obj *obj, const struct gp_json_obj *ign);
321
348#define GP_JSON_OBJ_FOREACH_FILTER(self, res, obj, ign) \
349 for (gp_json_obj_first_filter(self, res, obj, ign); \
350 gp_json_val_valid(res); \
351 gp_json_obj_next_filter(self, res, obj, ign))
352
361
371
384
398#define GP_JSON_ARR_FOREACH(self, res) \
399 for (gp_json_arr_first(self, res); gp_json_val_valid(res); gp_json_arr_next(self, res))
400
409
413typedef struct gp_json_reader_state {
414 size_t off;
415 unsigned int depth;
417
428{
429 struct gp_json_reader_state ret = {
430 .off = self->sub_off,
431 .depth = self->depth,
432 };
433
434 return ret;
435}
436
447{
448 self->off = state.off;
449 self->sub_off = state.off;
450 self->depth = state.depth;
451}
452
458static inline void gp_json_reader_reset(gp_json_reader *self)
459{
460 self->off = 0;
461 self->sub_off = 0;
462 self->depth = 0;
463 self->err[0] = 0;
464}
465
475
482
495
503{
504 return self->off >= self->len;
505}
506
507#endif /* GP_JSON_H */
A compiler dependent macros.
#define GP_FMT_PRINTF(fmt, list)
Expands to format printf attribute when supported by the compiler.
Definition gp_compiler.h:31
Common JSON reader/writer definitions.
#define GP_JSON_ID_MAX
Maximal id string lenght including terminating null element.
gp_json_type
A JSON data type.
#define GP_JSON_ERR_MAX
Maximal error message length.
size_t gp_json_lookup(const void *arr, size_t memb_size, size_t list_len, const char *key)
Utility function for log(n) lookup in a sorted array.
void gp_json_err_print(gp_json_reader *self)
Prints error stored in the buffer.
enum gp_json_type gp_json_reader_start(gp_json_reader *self)
Returns if first element in JSON is object or array.
void gp_json_reader_finish(gp_json_reader *self)
Prints errors and warnings at the end of parsing.
void gp_json_err(gp_json_reader *self, const char *fmt,...)
Fills the reader error.
static gp_json_reader_state gp_json_reader_state_save(gp_json_reader *self)
Returns a parser state at the start of current object/array.
int gp_json_arr_next(gp_json_reader *self, struct gp_json_val *res)
Parses next value from a JSON array.
gp_json_val * gp_json_val_alloc(size_t buf_size)
Allocates a JSON value.
static int gp_json_reader_consumed(gp_json_reader *self)
Returns non-zero if whole buffer has been consumed.
int gp_json_arr_first(gp_json_reader *self, struct gp_json_val *res)
Starts parsing of a JSON array.
static void gp_json_reader_reset(gp_json_reader *self)
Resets the parser to a start.
int gp_json_arr_skip(gp_json_reader *self)
Skips parsing of a JSON array.
static void gp_json_reader_state_load(gp_json_reader *self, gp_json_reader_state state)
Returns the parser to a saved state.
int gp_json_obj_next(gp_json_reader *self, struct gp_json_val *res)
Parses next value from a JSON object.
int gp_json_obj_first(gp_json_reader *self, struct gp_json_val *res)
Starts parsing of a JSON object.
void gp_json_warn(gp_json_reader *self, const char *fmt,...)
Prints a warning.
int gp_json_obj_next_filter(gp_json_reader *self, struct gp_json_val *res, const struct gp_json_obj *obj, const struct gp_json_obj *ign)
Parses next value from a JSON object with attribute lists.
int gp_json_obj_first_filter(gp_json_reader *self, struct gp_json_val *res, const struct gp_json_obj *obj, const struct gp_json_obj *ign)
Starts parsing of a JSON object with attribute lists.
void gp_json_reader_free(gp_json_reader *self)
Frees an gp_json_reader buffer.
enum gp_json_type gp_json_next_type(gp_json_reader *self)
Returns the type of next element in buffer.
gp_json_reader * gp_json_reader_load(const char *path)
Loads a file into an gp_json_reader buffer.
void gp_json_val_free(gp_json_val *self)
Frees a JSON value.
int gp_json_obj_skip(gp_json_reader *self)
Skips parsing of a JSON object.
static int gp_json_reader_err(gp_json_reader *self)
Returns true if error was encountered.
static int gp_json_val_valid(struct gp_json_val *res)
Checks is result has valid type.
A JSON object attribute description i.e. key and type.
const char * key
A JSON object key name.
enum gp_json_type type
A JSON object value type.
A JSON object description.
const gp_json_obj_attr * attrs
A list of object attributes.
size_t attr_cnt
A size of attrs array.
A JSON parser state.
A JSON parser internal state.
const char * json
unsigned int max_depth
unsigned int depth
void(* err_print)(void *err_print_priv, const char *line)
A parsed JSON key value pair.
int val_bool
A boolean value.
long long val_int
An integer value.
enum gp_json_type type
A value type.
double val_float
A floating point value.
const char * val_str
A string value.