GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
Data Structures | Typedefs | Functions
gp_json_reader.h File Reference

A JSON parser. More...

#include <stdio.h>
#include <core/gp_compiler.h>
#include <utils/gp_json_common.h>

Go to the source code of this file.

Data Structures

struct  gp_json_reader
 A JSON parser internal state. More...
 
struct  gp_json_val
 A parsed JSON key value pair. More...
 
struct  gp_json_obj_attr
 A JSON object attribute description i.e. key and type. More...
 
struct  gp_json_obj
 A JSON object description. More...
 
struct  gp_json_state
 A JSON parser state. More...
 

Typedefs

typedef struct gp_json_val gp_json_val
 A parsed JSON key value pair.
 
typedef struct gp_json_obj_attr gp_json_obj_attr
 A JSON object attribute description i.e. key and type.
 
typedef struct gp_json_obj gp_json_obj
 A JSON object description.
 
typedef struct gp_json_state gp_json_state
 A JSON parser state.
 

Functions

gp_json_valgp_json_val_alloc (size_t buf_size)
 Allocates a JSON value.
 
void gp_json_val_free (gp_json_val *self)
 Frees a JSON value.
 
const char * gp_json_type_name (enum gp_json_type type)
 Returns type name.
 
void gp_json_err (gp_json_reader *self, const char *fmt,...)
 Fills the reader error.
 
void gp_json_err_print (gp_json_reader *self)
 Prints error stored in the buffer.
 
void gp_json_warn (gp_json_reader *self, const char *fmt,...)
 Prints a warning.
 
static int gp_json_reader_err (gp_json_reader *self)
 Returns true if error was encountered.
 
static int gp_json_valid (struct gp_json_val *res)
 Checks is result has valid type.
 
enum gp_json_type gp_json_next_type (gp_json_reader *self)
 Returns the type of next element in buffer.
 
enum gp_json_type gp_json_start (gp_json_reader *self)
 Returns if first element in JSON is object or array.
 
int gp_json_obj_first (gp_json_reader *self, struct gp_json_val *res)
 Starts parsing of an JSON object.
 
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.
 
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.
 
int gp_json_obj_skip (gp_json_reader *self)
 Skips parsing of an JSON object.
 
int gp_json_arr_skip (gp_json_reader *self)
 Skips parsing of an JSON array.
 
static struct gp_json_state gp_json_state_start (gp_json_reader *self)
 Returns a parser state at the start of current object/array.
 
static void gp_json_state_load (gp_json_reader *self, struct gp_json_state state)
 Returns the parser to a saved state.
 
static void gp_json_reset (gp_json_reader *self)
 Resets the parser to a start.
 
gp_json_readergp_json_reader_load (const char *path)
 Loads a file into an gp_json_reader buffer.
 
void gp_json_reader_free (gp_json_reader *self)
 Frees an gp_json_reader buffer.
 
void gp_json_reader_finish (gp_json_reader *self)
 Prints errors and warnings at the end of parsing.
 
static int gp_json_empty (gp_json_reader *self)
 Returns non-zero if whole buffer has been consumed.
 

Detailed Description

A JSON parser.

Definition in file gp_json_reader.h.

Function Documentation

◆ gp_json_arr_skip()

int gp_json_arr_skip ( gp_json_reader self)

Skips parsing of an JSON array.

Parameters
selfA gp_json_reader
Returns
Zero on success, non-zero otherwise.

◆ gp_json_empty()

static int gp_json_empty ( gp_json_reader self)
inlinestatic

Returns non-zero if whole buffer has been consumed.

Parameters
selfA gp_json_reader.
Returns
Non-zero if whole buffer was consumed.

Definition at line 354 of file gp_json_reader.h.

References gp_json_reader::len, and gp_json_reader::off.

◆ gp_json_err()

void gp_json_err ( gp_json_reader self,
const char *  fmt,
  ... 
)

Fills the reader error.

Once buffer error is set all parsing functions return immediatelly with type set to GP_JSON_VOID.

Parameters
selfAn gp_json_reader
fmtA printf like format string
...A printf like parameters

◆ gp_json_err_print()

void gp_json_err_print ( gp_json_reader self)

Prints error stored in the buffer.

The error takes into consideration the current offset in the buffer and prints a few preceding lines along with the exact position of the error.

The error is passed to the err_print() handler.

Parameters
selfA gp_json_reader

◆ gp_json_lookup()

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.

Parameters
listAnalphabetically sorted array.
list_lenArray length.
Returns
An array index or (size_t)-1 if key wasn't found.

◆ gp_json_next_type()

enum gp_json_type gp_json_next_type ( gp_json_reader self)

Returns the type of next element in buffer.

Parameters
selfA gp_json_reader
Returns
A type of next element in the buffer.

◆ gp_json_obj_first()

int gp_json_obj_first ( gp_json_reader self,
struct gp_json_val res 
)

Starts parsing of an JSON object.

Parameters
selfA gp_json_reader @res An gp_json result.

◆ gp_json_obj_first_filter()

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.

These functions allows you to efficiently filter a set of keys for a given object passed in gp_json_obj.

Parameters
selfA gp_json_reader
resAn gp_json result.
objAn gp_json_obj object description.
fA file to print warnings to.

◆ gp_json_obj_skip()

int gp_json_obj_skip ( gp_json_reader self)

Skips parsing of an JSON object.

Parameters
selfA gp_json_reader
Returns
Zero on success, non-zero otherwise.

◆ gp_json_reader_err()

static int gp_json_reader_err ( gp_json_reader self)
inlinestatic

Returns true if error was encountered.

Parameters
selfA gp_json_reader
Returns
True if error was encountered false otherwise.

Definition at line 146 of file gp_json_reader.h.

◆ gp_json_reader_finish()

void gp_json_reader_finish ( gp_json_reader self)

Prints errors and warnings at the end of parsing.

Checks if self->err is set and prints the error with gp_json_reader_err()

Checks if there is any text left after the parser has finished and prints a warning if so.

Parameters
selfA gp_json_reader

◆ gp_json_reader_free()

void gp_json_reader_free ( gp_json_reader self)

Frees an gp_json_reader buffer.

Parameters
selfA gp_json_reader allocated by gp_json_load() function.

◆ gp_json_reader_load()

gp_json_reader * gp_json_reader_load ( const char *  path)

Loads a file into an gp_json_reader buffer.

The reader has to be later freed by gp_json_reader_free().

Parameters
pathA path to a file.
Returns
A gp_json_reader or NULL in a case of a failure.

◆ gp_json_reset()

static void gp_json_reset ( gp_json_reader self)
inlinestatic

Resets the parser to a start.

Parameters
selfA gp_json_reader

Definition at line 311 of file gp_json_reader.h.

References gp_json_reader::depth, gp_json_reader::off, and gp_json_reader::sub_off.

◆ gp_json_start()

enum gp_json_type gp_json_start ( gp_json_reader self)

Returns if first element in JSON is object or array.

Parameters
selfA gp_json_reader
Returns
On success returns GP_JSON_OBJ or GP_JSON_ARR. On failure GP_JSON_VOID.

◆ gp_json_state_load()

static void gp_json_state_load ( gp_json_reader self,
struct gp_json_state  state 
)
inlinestatic

Returns the parser to a saved state.

This function could be used for the parser to return to the start of object or array saved by t the gp_json_state_get() function.

Parameters
selfA gp_json_reader
stateAn parser state as returned by the gp_json_state_get().

Definition at line 299 of file gp_json_reader.h.

References gp_json_reader::depth, gp_json_reader::off, and gp_json_reader::sub_off.

◆ gp_json_state_start()

static struct gp_json_state gp_json_state_start ( gp_json_reader self)
inlinestatic

Returns a parser state at the start of current object/array.

This function could be used for the parser to return to the start of the currently parsed object or array.

Parameters
selfA gp_json_reader
Returns
A state that points to a start of the last object or array.

Definition at line 280 of file gp_json_reader.h.

◆ gp_json_type_name()

const char * gp_json_type_name ( enum gp_json_type  type)

Returns type name.

Parameters
typeA json type.
Returns
A type name.

◆ gp_json_val_alloc()

gp_json_val * gp_json_val_alloc ( size_t  buf_size)

Allocates a JSON value.

Parameters
buf_sizeA maximal buffer size for a string value, pass 0 for default.
Returns
A newly allocated JSON value.

◆ gp_json_val_free()

void gp_json_val_free ( gp_json_val self)

Frees a JSON value.

Parameters
selfA JSON value previously allocated by gp_json_val_alloc().

◆ gp_json_valid()

static int gp_json_valid ( struct gp_json_val res)
inlinestatic

Checks is result has valid type.

Parameters
resAn gp_json value.
Returns
Zero if result is not valid, non-zero otherwise.

Definition at line 157 of file gp_json_reader.h.

◆ gp_json_warn()

void gp_json_warn ( gp_json_reader self,
const char *  fmt,
  ... 
)

Prints a warning.

Uses the print handler in the buffer to print a warning along with a few lines of context from the JSON at the current position.

Parameters
selfA gp_json_reader
fmtA printf-like error string.
...A printf-like parameters.