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
11#ifndef GP_JSON_READER_H
12#define GP_JSON_READER_H
13
14#include <stdio.h>
15#include <core/gp_compiler.h>
17
18#define GP_JSON_READER_INIT(buf, buf_len) { \
19 .max_depth = GP_JSON_RECURSION_MAX, \
20 .err_print = GP_JSON_ERR_PRINT, \
21 .err_print_priv = GP_JSON_ERR_PRINT_PRIV, \
22 .json = buf, \
23 .len = buf_len, \
24}
25
31 const char *json;
33 size_t len;
35 size_t off;
37 size_t sub_off;
39 unsigned int depth;
41 unsigned int max_depth;
42
44 void (*err_print)(void *err_print_priv, const char *line);
45 void *err_print_priv;
46
47 char err[GP_JSON_ERR_MAX];
48 char buf[];
49};
50
54typedef struct gp_json_val {
55 enum gp_json_type type;
56
58 char *buf;
59 size_t buf_size;
60
62 size_t idx;
63
65 union {
66 int val_bool;
67 long long val_int;
68 const char *val_str;
69 };
70
71 double val_float;
72
74 char id[GP_JSON_ID_MAX];
75
76 char buf__[];
78
86
93
100const char *gp_json_type_name(enum gp_json_type type);
101
112void gp_json_err(gp_json_reader *self, const char *fmt, ...)
113 GP_FMT_PRINTF(2, 3);
114
126
137void gp_json_warn(gp_json_reader *self, const char *fmt, ...)
138 GP_FMT_PRINTF(2, 3);
139
146static inline int gp_json_reader_err(gp_json_reader *self)
147{
148 return !!self->err[0];
149}
150
157static inline int gp_json_valid(struct gp_json_val *res)
158{
159 return !!res->type;
160}
161
169
177
185int gp_json_obj_next(gp_json_reader *self, struct gp_json_val *res);
186
187#define GP_JSON_OBJ_FOREACH(buf, res) \
188 for (gp_json_obj_first(buf, res); gp_json_valid(res); gp_json_obj_next(buf, res))
189
198size_t gp_json_lookup(const void *arr, size_t memb_size, size_t list_len,
199 const char *key);
200
202typedef struct gp_json_obj_attr {
203 const char *key;
204 enum gp_json_type type;
206
208typedef struct gp_json_obj {
211 size_t attr_cnt;
213
214static inline size_t gp_json_obj_lookup(const gp_json_obj *obj, const char *key)
215{
216 return gp_json_lookup(obj->attrs, sizeof(*obj->attrs), obj->attr_cnt, key);
217}
218
219#define GP_JSON_OBJ_ATTR(keyv, typev) \
220 {.key = keyv, .type = typev}
221
234 const struct gp_json_obj *obj, const struct gp_json_obj *ign);
235int gp_json_obj_next_filter(gp_json_reader *self, struct gp_json_val *res,
236 const struct gp_json_obj *obj, const struct gp_json_obj *ign);
237
238#define GP_JSON_OBJ_FILTER(buf, res, obj, ign) \
239 for (gp_json_obj_first_filter(buf, res, obj, ign); gp_json_valid(res); gp_json_obj_next_filter(buf, res, obj, ign))
240
248
249int gp_json_arr_first(gp_json_reader *self, struct gp_json_val *res);
250int gp_json_arr_next(gp_json_reader *self, struct gp_json_val *res);
251
252#define GP_JSON_ARR_FOREACH(buf, res) \
253 for (gp_json_arr_first(buf, res); gp_json_valid(res); gp_json_arr_next(buf, res))
254
262
266typedef struct gp_json_state {
267 size_t off;
268 unsigned int depth;
270
281{
282 struct gp_json_state ret = {
283 .off = self->sub_off,
284 .depth = self->depth,
285 };
286
287 return ret;
288}
289
299static inline void gp_json_state_load(gp_json_reader *self, struct gp_json_state state)
300{
301 self->off = state.off;
302 self->sub_off = state.off;
303 self->depth = state.depth;
304}
305
311static inline void gp_json_reset(gp_json_reader *self)
312{
313 self->off = 0;
314 self->sub_off = 0;
315 self->depth = 0;
316 self->err[0] = 0;
317}
318
328
335
347
354static inline int gp_json_empty(gp_json_reader *self)
355{
356 return self->off >= self->len;
357}
358
359#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:30
Common JSON reader/writer definitions.
gp_json_type
A JSON data type.
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_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 void gp_json_reset(gp_json_reader *self)
Resets the parser to a start.
static int gp_json_empty(gp_json_reader *self)
Returns non-zero if whole buffer has been consumed.
gp_json_val * gp_json_val_alloc(size_t buf_size)
Allocates a JSON value.
static void gp_json_state_load(gp_json_reader *self, struct gp_json_state state)
Returns the parser to a saved state.
const char * gp_json_type_name(enum gp_json_type type)
Returns type name.
int gp_json_arr_skip(gp_json_reader *self)
Skips parsing of an JSON array.
static int gp_json_valid(struct gp_json_val *res)
Checks is result has valid type.
int gp_json_obj_first(gp_json_reader *self, struct gp_json_val *res)
Starts parsing of an JSON object.
void gp_json_warn(gp_json_reader *self, const char *fmt,...)
Prints a warning.
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)
Object parsing functions with with attribute list.
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 an JSON object.
static int gp_json_reader_err(gp_json_reader *self)
Returns true if error was encountered.
static struct gp_json_state gp_json_state_start(gp_json_reader *self)
Returns a parser state at the start of current object/array.
A JSON object attribute description i.e. key and type.
A JSON object description.
const gp_json_obj_attr * attrs
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 JSON parser state.
A parsed JSON key value pair.