diff --git a/include/bits/__generics.h b/include/bits/__generics.h new file mode 100644 index 0000000..ff0b9e0 --- /dev/null +++ b/include/bits/__generics.h @@ -0,0 +1,35 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "../../modules/libcdsb/include/bits/__generics.h" + +#ifndef LIBCJSONP_BITS_GENERICS_H +#define LIBCJSONP_BITS_GENERICS_H + +#define _LIBCJSONP_vtypeof_hack(x) (vtype)(_Generic((x),\ + default: VTYPE_POINTER,\ + const void*: VTYPE_POINTER, void*: VTYPE_POINTER,\ + const char**: VTYPE_STRING, char**: VTYPE_STRING,\ + const vtype_string*: VTYPE_STRING, vtype_string*: VTYPE_STRING,\ + const vtype_array*: VTYPE_ARRAY, vtype_array*: VTYPE_ARRAY,\ + const vtype_list*: VTYPE_LIST, vtype_list*: VTYPE_LIST,\ + const vtype_map*: VTYPE_MAP, vtype_map*: VTYPE_MAP,\ + const vtype_set*: VTYPE_SET, vtype_set*: VTYPE_SET,\ + const vtype_dict*: VTYPE_DICT, vtype_dict*: VTYPE_DICT,\ + vtype_bool: VTYPE_BOOLEAN,\ + vtype_uint8: VTYPE_UINT8,\ + vtype_uint16: VTYPE_UINT16,\ + vtype_uint32: VTYPE_UINT32,\ + vtype_uint64: VTYPE_UINT64,\ + vtype_int8: VTYPE_INT8,\ + vtype_int16: VTYPE_INT16,\ + vtype_int32: VTYPE_INT32,\ + vtype_int64: VTYPE_INT64,\ + vtype_float: VTYPE_FLOAT,\ + vtype_double: VTYPE_DOUBLE,\ + vtype_ldouble: VTYPE_LDOUBLE)) + +#define _LIBCJSONP_vtypeof(x) _Generic((x), default: _LIBCJSONP_vtypeof_hack(_Generic((x), default: (x),const char*: &(x),char*:&(x))), const json_t*:json_type(x),json_t*:json_type(x)) +#define _LIBCJSONP_value_pointer(x) _Generic((x), default: _LIBCDSB_value_pointer(x), const json_t*: json_value(x), json_t*: json_value(x)) + +#endif /* LIBCJSONP_BITS_GENERICS_H */ diff --git a/include/bits/string.h b/include/bits/string.h new file mode 100644 index 0000000..e7ec63b --- /dev/null +++ b/include/bits/string.h @@ -0,0 +1,13 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include +#include "../../modules/libcdsb/include/vtype.h" + +#ifndef LIBCJSONP_BITS_STRING_H +#define LIBCJSONP_BITS_STRING_H + +extern void libcjsonp_string_unescape(vtype_string* x); +extern void libcjsonp_string_escape (vtype_string* s); + +#endif /* LIBCJSONP_BITS_STRING_H */ diff --git a/include/jsonp.h b/include/jsonp.h new file mode 100644 index 0000000..f571e57 --- /dev/null +++ b/include/jsonp.h @@ -0,0 +1,38 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include +#include "bits/__generics.h" +#include "../modules/libcdsb/include/vtype.h" + +#ifndef LIBCJSONP_JSONP_H +#define LIBCJSONP_JSONP_H + +typedef struct { void* data; vtype type; } json_t; + +extern void json_free (json_t* x); +inline vtype json_type (json_t* x) Always_inline__ Nonnull__(1); +inline void* json_value(json_t* x) Always_inline__ Nonnull__(1); + +extern bool json_load (json_t* x, FILE* s) Nonnull__(1, 2); + +inline vtype json_type (json_t* x) { return x->type; } +inline void* json_value(json_t* x) { return x->data; } + +/*#####################################################################################################################*/ + +#define json_parse(x, src) libcjsonp_json_parse (x, _LIBCDSB_to_cstring(src), 0) +#define json_init(x, value) libcjsonp_json_init (x, _LIBCJSONP_value_pointer(value), _LIBCJSONP_vtypeof(value), 0) +#define json_stringify(value) libcjsonp_json_stringify (_LIBCJSONP_value_pointer(value), _LIBCJSONP_vtypeof(value), 0, 0) +#define json_stringify_pretty(value) libcjsonp_json_stringify (_LIBCJSONP_value_pointer(value), _LIBCJSONP_vtypeof(value), 4, 0) +#define json_dump(dest, value) libcjsonp_json_dump(dest, _LIBCJSONP_value_pointer(value), _LIBCJSONP_vtypeof(value), 0, 0) +#define json_dump_pretty(dest, value) libcjsonp_json_dump(dest, _LIBCJSONP_value_pointer(value), _LIBCJSONP_vtypeof(value), 4, 0) + +/*#####################################################################################################################*/ + +extern void libcjsonp_json_init (json_t* x, void* value, vtype type, bool attach) Nonnull__(1); +extern bool libcjsonp_json_parse(json_t* x, const char* value, size_t nmemb) Nonnull__(1); +extern vtype_string libcjsonp_json_stringify (const void* value, vtype type, size_t padsize, int padchr) Pure__; +extern bool libcjsonp_json_dump(FILE* dest, const void* value, vtype type, size_t padsize, int padchr) Nonnull__(1); + +#endif /* LIBCJSONP_JSONP_H */