39#ifndef LITE3_CONTEXT_API_H
40#define LITE3_CONTEXT_API_H
130#define LITE3_CONTEXT_BUF_SIZE_MIN 1024
131static_assert(
LITE3_CONTEXT_BUF_SIZE_MIN > (size_t)LITE3_NODE_ALIGNMENT_MASK,
"LITE3_CONTEXT_BUF_SIZE_MIN must be greater than LITE3_NODE_ALIGNMENT_MASK");
144 void *underlying_buf;
183 const unsigned char *buf,
230 const unsigned char *buf,
244#ifndef DOXYGEN_IGNORE
274 return lite3_init_obj(ctx->buf, &ctx->buflen, ctx->bufsz);
288 return lite3_init_arr(ctx->buf, &ctx->buflen, ctx->bufsz);
329#define lite3_ctx_set_null(ctx, ofs, key) ({ \
330 const char *__lite3_key__ = (key); \
331 lite3_ctx_set_null_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
333#ifndef DOXYGEN_IGNORE
334static inline int lite3_ctx_set_null_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
337 if ((ret = _lite3_verify_obj_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key)) < 0)
341 while ((ret = lite3_set_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_NULL], &val)) < 0) {
342 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
364#define lite3_ctx_set_bool(ctx, ofs, key, value) ({ \
365 const char *__lite3_key__ = (key); \
366 _lite3_ctx_set_bool_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), value); \
368#ifndef DOXYGEN_IGNORE
369static inline int _lite3_ctx_set_bool_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data,
bool value)
372 if ((ret = _lite3_verify_obj_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key)) < 0)
376 while ((ret = lite3_set_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_BOOL], &val)) < 0) {
377 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
400#define lite3_ctx_set_i64(ctx, ofs, key, value) ({ \
401 const char *__lite3_key__ = (key); \
402 _lite3_ctx_set_i64_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), value); \
404#ifndef DOXYGEN_IGNORE
405static inline int _lite3_ctx_set_i64_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data, int64_t value)
408 if ((ret = _lite3_verify_obj_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key)) < 0)
412 while ((ret = lite3_set_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_I64], &val)) < 0) {
413 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
436#define lite3_ctx_set_f64(ctx, ofs, key, value) ({ \
437 const char *__lite3_key__ = (key); \
438 _lite3_ctx_set_f64_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), value); \
440#ifndef DOXYGEN_IGNORE
441static inline int _lite3_ctx_set_f64_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data,
double value)
444 if ((ret = _lite3_verify_obj_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key)) < 0)
448 while ((ret = lite3_set_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_F64], &val)) < 0) {
449 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
473#define lite3_ctx_set_bytes(ctx, ofs, key, bytes, bytes_len) ({ \
474 const char *__lite3_key__ = (key); \
475 _lite3_ctx_set_bytes_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), bytes, bytes_len); \
477#ifndef DOXYGEN_IGNORE
478static inline int _lite3_ctx_set_bytes_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data,
const unsigned char *__restrict bytes,
size_t bytes_len)
481 if ((ret = _lite3_verify_obj_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key)) < 0)
485 while ((ret = lite3_set_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_BYTES] + bytes_len, &val)) < 0) {
486 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
514#define lite3_ctx_set_str(ctx, ofs, key, str) ({ \
515 const char *__lite3_key__ = (key); \
516 _lite3_ctx_set_str_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), str); \
518#ifndef DOXYGEN_IGNORE
519static inline int _lite3_ctx_set_str_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data,
const char *__restrict str)
522 if ((ret = _lite3_verify_obj_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key)) < 0)
525 size_t str_size = strlen(str) + 1;
527 while ((ret = lite3_set_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0) {
528 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
556#define lite3_ctx_set_str_n(ctx, ofs, key, str, str_len) ({ \
557 const char *__lite3_key__ = (key); \
558 _lite3_ctx_set_str_n_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), str, str_len); \
560#ifndef DOXYGEN_IGNORE
561static inline int _lite3_ctx_set_str_n_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data,
const char *__restrict str,
size_t str_len)
564 if ((ret = _lite3_verify_obj_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key)) < 0)
567 size_t str_size = str_len + 1;
569 while ((ret = lite3_set_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0) {
570 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
595#define lite3_ctx_set_obj(ctx, ofs, key, out_ofs) ({ \
596 lite3_ctx *__lite3_ctx__ = (ctx); \
597 size_t __lite3_ofs__ = (ofs); \
598 const char *__lite3_key__ = (key); \
600 if ((__lite3_ret__ = _lite3_verify_obj_set( \
601 __lite3_ctx__->buf, \
602 &__lite3_ctx__->buflen, \
604 __lite3_ctx__->bufsz, \
605 __lite3_key__)) < 0) \
606 return __lite3_ret__; \
609 while ((__lite3_ret__ = lite3_set_obj_impl( \
610 __lite3_ctx__->buf, \
611 &__lite3_ctx__->buflen, \
613 __lite3_ctx__->bufsz, \
615 LITE3_KEY_DATA(key), \
617 if (errno == ENOBUFS && (lite3_ctx_grow_impl(__lite3_ctx__) == 0)) { \
620 return __lite3_ret__; \
637#define lite3_ctx_set_arr(ctx, ofs, key, out_ofs) ({ \
638 lite3_ctx *__lite3_ctx__ = (ctx); \
639 size_t __lite3_ofs__ = (ofs); \
640 const char *__lite3_key__ = (key); \
642 if ((__lite3_ret__ = _lite3_verify_obj_set( \
643 __lite3_ctx__->buf, \
644 &__lite3_ctx__->buflen, \
646 __lite3_ctx__->bufsz, \
647 __lite3_key__)) < 0) \
648 return __lite3_ret__; \
651 while ((__lite3_ret__ = lite3_set_arr_impl( \
652 __lite3_ctx__->buf, \
653 &__lite3_ctx__->buflen, \
655 __lite3_ctx__->bufsz, \
657 LITE3_KEY_DATA(key), \
659 if (errno == ENOBUFS && (lite3_ctx_grow_impl(__lite3_ctx__) == 0)) { \
662 return __lite3_ret__; \
690#ifndef DOXYGEN_IGNORE
691static inline int _lite3_ctx_set_by_index(
lite3_ctx *ctx,
size_t ofs, uint32_t index,
size_t val_len,
lite3_val **out)
694 if ((ret = _lite3_verify_arr_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
698 size >>= LITE3_NODE_SIZE_SHIFT;
699 if (LITE3_UNLIKELY(index > size)) {
700 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: ARRAY INDEX %u OUT OF BOUNDS (size == %u)\n", index, size);
704 lite3_key_data key_data = {
709 while ((ret = lite3_set_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, NULL, key_data, val_len, out)) < 0) {
710 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
719static inline int _lite3_ctx_set_by_append(
lite3_ctx *ctx,
size_t ofs,
size_t val_len,
lite3_val **out)
722 if ((ret = _lite3_verify_arr_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
726 size >>= LITE3_NODE_SIZE_SHIFT;
727 lite3_key_data key_data = {
732 while ((ret = lite3_set_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, NULL, key_data, val_len, out)) < 0) {
733 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
755 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[
LITE3_TYPE_NULL], &val)) < 0)
774 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[
LITE3_TYPE_BOOL], &val)) < 0)
794 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[
LITE3_TYPE_I64], &val)) < 0)
814 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[
LITE3_TYPE_F64], &val)) < 0)
830 const unsigned char *__restrict bytes,
835 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[
LITE3_TYPE_BYTES] + bytes_len, &val)) < 0)
856 const char *__restrict str)
859 size_t str_size = strlen(str) + 1;
861 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
881 const char *__restrict str,
885 size_t str_size = str_len + 1;
887 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
905 size_t *__restrict out_ofs)
908 if ((ret = _lite3_verify_arr_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
911 while ((ret = lite3_arr_append_obj_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, out_ofs)) < 0) {
912 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
930 size_t *__restrict out_ofs)
933 if ((ret = _lite3_verify_arr_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
936 while ((ret = lite3_arr_append_arr_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, out_ofs)) < 0) {
937 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
986 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[
LITE3_TYPE_NULL], &val)) < 0)
1006 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[
LITE3_TYPE_BOOL], &val)) < 0)
1027 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[
LITE3_TYPE_I64], &val)) < 0)
1048 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[
LITE3_TYPE_F64], &val)) < 0)
1065 const unsigned char *__restrict bytes,
1070 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[
LITE3_TYPE_BYTES] + bytes_len, &val)) < 0)
1092 const char *__restrict str)
1095 size_t str_size = strlen(str) + 1;
1097 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
1118 const char *__restrict str,
1122 size_t str_size = str_len + 1;
1124 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
1143 size_t *__restrict out_ofs)
1146 if ((ret = _lite3_verify_arr_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
1149 while ((ret = lite3_arr_set_obj_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, index, out_ofs)) < 0) {
1150 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
1169 size_t *__restrict out_ofs)
1172 if ((ret = _lite3_verify_arr_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
1175 while ((ret = lite3_arr_set_arr_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, index, out_ofs)) < 0) {
1176 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
1220 if (_lite3_verify_get(ctx->buf, ctx->buflen, 0) < 0)
1235#define lite3_ctx_get_type(ctx, ofs, key) ({ \
1236 const char *__lite3_key__ = (key); \
1237 _lite3_ctx_get_type_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1239#ifndef DOXYGEN_IGNORE
1240static inline enum lite3_type _lite3_ctx_get_type_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1242 return _lite3_get_type_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1275#define lite3_ctx_get_type_size(ctx, ofs, key, out) ({ \
1276 const char *__lite3_key__ = (key); \
1277 _lite3_ctx_get_type_size_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1279#ifndef DOXYGEN_IGNORE
1280static inline int _lite3_ctx_get_type_size_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data,
size_t *__restrict out)
1282 return _lite3_get_type_size_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out);
1296#define lite3_ctx_exists(ctx, ofs, key) ({ \
1297 const char *__lite3_key__ = (key); \
1298 _lite3_ctx_exists_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1300#ifndef DOXYGEN_IGNORE
1301static inline bool _lite3_ctx_exists_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1303 return _lite3_exists_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1320 return lite3_count(ctx->buf, ctx->buflen, ofs, out);
1333#define lite3_ctx_is_null(ctx, ofs, key) ({ \
1334 const char *__lite3_key__ = (key); \
1335 _lite3_ctx_is_null_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1337#ifndef DOXYGEN_IGNORE
1338static inline bool _lite3_ctx_is_null_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1340 return _lite3_is_null_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1354#define lite3_ctx_is_bool(ctx, ofs, key) ({ \
1355 const char *__lite3_key__ = (key); \
1356 _lite3_ctx_is_bool_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1358#ifndef DOXYGEN_IGNORE
1359static inline bool _lite3_ctx_is_bool_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1361 return _lite3_is_bool_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1375#define lite3_ctx_is_i64(ctx, ofs, key) ({ \
1376 const char *__lite3_key__ = (key); \
1377 _lite3_ctx_is_i64_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1379#ifndef DOXYGEN_IGNORE
1380static inline bool _lite3_ctx_is_i64_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1382 return _lite3_is_i64_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1396#define lite3_ctx_is_f64(ctx, ofs, key) ({ \
1397 const char *__lite3_key__ = (key); \
1398 _lite3_ctx_is_f64_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1400#ifndef DOXYGEN_IGNORE
1401static inline bool _lite3_ctx_is_f64_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1403 return _lite3_is_f64_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1417#define lite3_ctx_is_bytes(ctx, ofs, key) ({ \
1418 const char *__lite3_key__ = (key); \
1419 _lite3_ctx_is_bytes_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1421#ifndef DOXYGEN_IGNORE
1422static inline bool _lite3_ctx_is_bytes_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1424 return _lite3_is_bytes_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1438#define lite3_ctx_is_str(ctx, ofs, key) ({ \
1439 const char *__lite3_key__ = (key); \
1440 _lite3_ctx_is_str_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1442#ifndef DOXYGEN_IGNORE
1443static inline bool _lite3_ctx_is_str_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1445 return _lite3_is_str_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1459#define lite3_ctx_is_obj(ctx, ofs, key) ({ \
1460 const char *__lite3_key__ = (key); \
1461 _lite3_ctx_is_obj_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1463#ifndef DOXYGEN_IGNORE
1464static inline bool _lite3_ctx_is_obj_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1467 return _lite3_is_obj_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1481#define lite3_ctx_is_arr(ctx, ofs, key) ({ \
1482 const char *__lite3_key__ = (key); \
1483 _lite3_ctx_is_arr_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1485#ifndef DOXYGEN_IGNORE
1486static inline bool _lite3_ctx_is_arr_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1488 return _lite3_is_arr_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1526#define lite3_ctx_get(ctx, ofs, key, out) ({ \
1527 lite3_ctx *__lite3_ctx__ = (ctx); \
1528 size_t __lite3_ofs__ = (ofs); \
1529 int __lite3_ret__; \
1530 if ((__lite3_ret__ = _lite3_verify_get(__lite3_ctx__->buf, __lite3_ctx__->buflen, __lite3_ofs__)) < 0) \
1531 return __lite3_ret__; \
1532 const char *__lite3_key__ = (key); \
1533 lite3_get_impl(__lite3_ctx__->buf, __lite3_ctx__->buflen, __lite3_ofs__, __lite3_key__, LITE3_KEY_DATA(key), out); \
1547#define lite3_ctx_get_bool(ctx, ofs, key, out) ({ \
1548 const char *__lite3_key__ = (key); \
1549 _lite3_ctx_get_bool_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1551#ifndef DOXYGEN_IGNORE
1552static inline int _lite3_ctx_get_bool_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data,
bool *out)
1554 return _lite3_get_bool_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out);
1569#define lite3_ctx_get_i64(ctx, ofs, key, out) ({ \
1570 const char *__lite3_key__ = (key); \
1571 _lite3_ctx_get_i64_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1573#ifndef DOXYGEN_IGNORE
1574static inline int _lite3_ctx_get_i64_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data, int64_t *out)
1576 return _lite3_get_i64_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out);
1591#define lite3_ctx_get_f64(ctx, ofs, key, out) ({ \
1592 const char *__lite3_key__ = (key); \
1593 _lite3_ctx_get_f64_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1595#ifndef DOXYGEN_IGNORE
1596static inline int _lite3_ctx_get_f64_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data,
double *out)
1598 return _lite3_get_f64_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out);
1613#define lite3_ctx_get_bytes(ctx, ofs, key, out) ({ \
1614 const char *__lite3_key__ = (key); \
1615 _lite3_ctx_get_bytes_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1617#ifndef DOXYGEN_IGNORE
1618static inline int _lite3_ctx_get_bytes_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data,
lite3_bytes *out)
1620 return _lite3_get_bytes_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out);
1635#define lite3_ctx_get_str(ctx, ofs, key, out) ({ \
1636 const char *__lite3_key__ = (key); \
1637 _lite3_ctx_get_str_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1639#ifndef DOXYGEN_IGNORE
1640static inline int _lite3_ctx_get_str_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data,
lite3_str *out)
1642 return _lite3_get_str_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out);
1657#define lite3_ctx_get_obj(ctx, ofs, key, out) ({ \
1658 const char *__lite3_key__ = (key); \
1659 _lite3_ctx_get_obj_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1661#ifndef DOXYGEN_IGNORE
1662static inline int _lite3_ctx_get_obj_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data,
size_t *__restrict out_ofs)
1664 return _lite3_get_obj_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out_ofs);
1679#define lite3_ctx_get_arr(ctx, ofs, key, out) ({ \
1680 const char *__lite3_key__ = (key); \
1681 _lite3_ctx_get_arr_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1683#ifndef DOXYGEN_IGNORE
1684static inline int _lite3_ctx_get_arr_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data,
size_t *__restrict out_ofs)
1686 return _lite3_get_arr_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out_ofs);
1794 size_t *__restrict out_ofs)
1809 size_t *__restrict out_ofs)
1831#define LITE3_ITER_ITEM 1
1833#define LITE3_ITER_DONE 0
1870 size_t *out_val_ofs)
1872 return lite3_iter_next(ctx->buf, ctx->buflen, iter, out_key, out_val_ofs);
1907#if defined(DOXYGEN_ONLY) && !defined(LITE3_JSON)
1923 const char *__restrict json_str,
1928 while ((ret =
lite3_json_dec(ctx->buf, &ctx->buflen, ctx->bufsz, json_str, json_len)) < 0) {
1929 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
1949 const char *__restrict path)
1954 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
1979 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
2024 size_t *__restrict out_len)
2047 size_t *__restrict out_len)
2065 char *__restrict json_buf,
2087 char *__restrict json_buf,
2095 (void)ctx; (void)json_str; (void)json_len;
2101 (void)ctx; (void)path;
2107 (void)ctx; (void)fp;
2113 (void)ctx; (void)ofs;
2119 (void)ctx; (void)ofs; (void)out_len;
2125 (void)ctx; (void)ofs; (void)out_len;
2131 (void)ctx; (void)ofs; (void)json_buf; (void)json_bufsz;
2137 (void)ctx; (void)ofs; (void)json_buf; (void)json_bufsz;
static int lite3_arr_get_obj(const unsigned char *buf, size_t buflen, size_t ofs, uint32_t index, size_t *__restrict out_ofs)
Get object by index.
static int lite3_arr_get_i64(const unsigned char *buf, size_t buflen, size_t ofs, uint32_t index, int64_t *out)
Get integer value by index.
static int lite3_arr_get_bool(const unsigned char *buf, size_t buflen, size_t ofs, uint32_t index, bool *out)
Get boolean value by index.
static int lite3_arr_get_f64(const unsigned char *buf, size_t buflen, size_t ofs, uint32_t index, double *out)
Get floating point value by index.
static int lite3_arr_get_str(const unsigned char *buf, size_t buflen, size_t ofs, uint32_t index, lite3_str *out)
Get string value by index.
static int lite3_arr_get_bytes(const unsigned char *buf, size_t buflen, size_t ofs, uint32_t index, lite3_bytes *out)
Get bytes value by index.
static int lite3_arr_get_arr(const unsigned char *buf, size_t buflen, size_t ofs, uint32_t index, size_t *__restrict out_ofs)
Get array by index.
#define LITE3_CONTEXT_BUF_SIZE_MIN
The minimum buffer size for a Lite³ context.
#define LITE3_NODE_ALIGNMENT
B-tree node alignment.
#define LITE3_NODE_SIZE_KC_OFFSET
Offset of the size_kc field inside struct node.
static int lite3_ctx_arr_append_bytes(lite3_ctx *ctx, size_t ofs, const unsigned char *__restrict bytes, size_t bytes_len)
Append bytes to array.
static int lite3_ctx_arr_append_f64(lite3_ctx *ctx, size_t ofs, double value)
Append floating point to array.
static int lite3_ctx_arr_append_null(lite3_ctx *ctx, size_t ofs)
Append null to array.
static int lite3_ctx_arr_append_str_n(lite3_ctx *ctx, size_t ofs, const char *__restrict str, size_t str_len)
Append string to array by length.
static int lite3_ctx_arr_append_str(lite3_ctx *ctx, size_t ofs, const char *__restrict str)
Append string to array.
static int lite3_ctx_arr_append_obj(lite3_ctx *ctx, size_t ofs, size_t *__restrict out_ofs)
Append object to array.
static int lite3_ctx_arr_append_arr(lite3_ctx *ctx, size_t ofs, size_t *__restrict out_ofs)
Append array to array.
static int lite3_ctx_arr_append_i64(lite3_ctx *ctx, size_t ofs, int64_t value)
Append integer to array.
static int lite3_ctx_arr_append_bool(lite3_ctx *ctx, size_t ofs, bool value)
Append boolean to array.
static int lite3_ctx_arr_get_arr(lite3_ctx *ctx, size_t ofs, uint32_t index, size_t *__restrict out_ofs)
Get array by index.
static int lite3_ctx_arr_get_str(lite3_ctx *ctx, size_t ofs, uint32_t index, lite3_str *out)
Get string value by index.
static int lite3_ctx_arr_get_i64(lite3_ctx *ctx, size_t ofs, uint32_t index, int64_t *out)
Get integer value by index.
static int lite3_ctx_arr_get_bool(lite3_ctx *ctx, size_t ofs, uint32_t index, bool *out)
Get boolean value by index.
static int lite3_ctx_arr_get_bytes(lite3_ctx *ctx, size_t ofs, uint32_t index, lite3_bytes *out)
Get bytes value by index.
static int lite3_ctx_arr_get_obj(lite3_ctx *ctx, size_t ofs, uint32_t index, size_t *__restrict out_ofs)
Get object by index.
static int lite3_ctx_arr_get_f64(lite3_ctx *ctx, size_t ofs, uint32_t index, double *out)
Get floating point value by index.
static int lite3_ctx_arr_set_i64(lite3_ctx *ctx, size_t ofs, uint32_t index, int64_t value)
Set integer in array.
static int lite3_ctx_arr_set_arr(lite3_ctx *ctx, size_t ofs, uint32_t index, size_t *__restrict out_ofs)
Set array in array.
static int lite3_ctx_arr_set_str(lite3_ctx *ctx, size_t ofs, uint32_t index, const char *__restrict str)
Set string in array.
static int lite3_ctx_arr_set_f64(lite3_ctx *ctx, size_t ofs, uint32_t index, double value)
Set float in array.
static int lite3_ctx_arr_set_obj(lite3_ctx *ctx, size_t ofs, uint32_t index, size_t *__restrict out_ofs)
Set object in array.
static int lite3_ctx_arr_set_str_n(lite3_ctx *ctx, size_t ofs, uint32_t index, const char *__restrict str, size_t str_len)
Set string in array by length.
static int lite3_ctx_arr_set_bool(lite3_ctx *ctx, size_t ofs, uint32_t index, bool value)
Set boolean in array.
static int lite3_ctx_arr_set_bytes(lite3_ctx *ctx, size_t ofs, uint32_t index, const unsigned char *__restrict bytes, size_t bytes_len)
Set bytes in array.
static int lite3_ctx_arr_set_null(lite3_ctx *ctx, size_t ofs, uint32_t index)
Set null in array.
static int lite3_ctx_init_arr(lite3_ctx *ctx)
Initialize a Lite³ context as an array.
static int lite3_ctx_init_obj(lite3_ctx *ctx)
Initialize a Lite³ context as an object.
static int lite3_ctx_iter_create(lite3_ctx *ctx, size_t ofs, lite3_iter *out)
Create a lite3 iterator for the given object or array.
static int lite3_ctx_iter_next(lite3_ctx *ctx, lite3_iter *iter, lite3_str *out_key, size_t *out_val_ofs)
Get the next item from a lite3 iterator.
static int64_t lite3_ctx_json_enc_pretty_buf(lite3_ctx *ctx, size_t ofs, char *__restrict json_buf, size_t json_bufsz)
Convert Lite³ to prettified JSON and write to output buffer.
static char * lite3_ctx_json_enc(lite3_ctx *ctx, size_t ofs, size_t *__restrict out_len)
Convert Lite³ to JSON string.
static int lite3_ctx_json_dec(lite3_ctx *ctx, const char *__restrict json_str, size_t json_len)
Convert JSON string to Lite³
static int lite3_ctx_json_print(lite3_ctx *ctx, size_t ofs)
Print Lite³ buffer as JSON to stdout
static int64_t lite3_ctx_json_enc_buf(lite3_ctx *ctx, size_t ofs, char *__restrict json_buf, size_t json_bufsz)
Convert Lite³ to JSON and write to output buffer.
static int lite3_ctx_json_dec_file(lite3_ctx *ctx, const char *__restrict path)
Convert JSON from file path to Lite³
static int lite3_ctx_json_dec_fp(lite3_ctx *ctx, FILE *fp)
Convert JSON from file pointer to Lite³
static char * lite3_ctx_json_enc_pretty(lite3_ctx *ctx, size_t ofs, size_t *__restrict out_len)
Convert Lite³ to JSON prettified string.
static lite3_ctx * lite3_ctx_create(void)
Create context with minimum size.
lite3_ctx * lite3_ctx_create_with_size(size_t bufsz)
Create context with custom size.
void lite3_ctx_destroy(lite3_ctx *ctx)
Destroy context.
lite3_ctx * lite3_ctx_create_take_ownership(unsigned char *buf, size_t buflen, size_t bufsz)
Create context by taking ownership of a buffer.
lite3_ctx * lite3_ctx_create_from_buf(const unsigned char *buf, size_t buflen)
Create context by copying from a buffer.
int lite3_ctx_import_from_buf(lite3_ctx *ctx, const unsigned char *buf, size_t buflen)
Copy data into existing context.
static int lite3_ctx_count(lite3_ctx *ctx, size_t ofs, uint32_t *out)
Write back the number of object entries or array elements.
static enum lite3_type lite3_ctx_arr_get_type(lite3_ctx *ctx, size_t ofs, uint32_t index)
Find array value by index and return value type.
static enum lite3_type lite3_ctx_get_root_type(lite3_ctx *ctx)
Get the root type of a Lite³ buffer.
static void lite3_ctx_print(lite3_ctx *ctx)
View the internal structure of a Lite³ buffer.
int lite3_iter_next(const unsigned char *buf, size_t buflen, lite3_iter *iter, lite3_str *out_key, size_t *out_val_ofs)
Get the next item from a lite3 iterator.
static int lite3_iter_create(const unsigned char *buf, size_t buflen, size_t ofs, lite3_iter *out)
Create a lite3 iterator for the given object or array.
int lite3_json_dec_file(unsigned char *buf, size_t *__restrict out_buflen, size_t bufsz, const char *__restrict path)
Convert JSON from file path to Lite³
int64_t lite3_json_enc_pretty_buf(const unsigned char *buf, size_t buflen, size_t ofs, char *__restrict json_buf, size_t json_bufsz)
Convert Lite³ to prettified JSON and write to output buffer.
char * lite3_json_enc_pretty(const unsigned char *buf, size_t buflen, size_t ofs, size_t *__restrict out_len)
Convert Lite³ to JSON prettified string.
int lite3_json_print(const unsigned char *buf, size_t buflen, size_t ofs)
Print Lite³ buffer as JSON to stdout
int lite3_json_dec(unsigned char *buf, size_t *__restrict out_buflen, size_t bufsz, const char *__restrict json_str, size_t json_len)
Convert JSON string to Lite³
int lite3_json_dec_fp(unsigned char *buf, size_t *__restrict out_buflen, size_t bufsz, FILE *fp)
Convert JSON from file pointer to Lite³
int64_t lite3_json_enc_buf(const unsigned char *buf, size_t buflen, size_t ofs, char *__restrict json_buf, size_t json_bufsz)
Convert Lite³ to JSON and write to output buffer.
char * lite3_json_enc(const unsigned char *buf, size_t buflen, size_t ofs, size_t *__restrict out_len)
Convert Lite³ to JSON string.
lite3_type
enum containing all Lite³ types
@ LITE3_TYPE_INVALID
any type value equal or greater than this is considered invalid
@ LITE3_TYPE_STRING
maps to 'string' type in JSON
@ LITE3_TYPE_BOOL
maps to 'boolean' type in JSON; underlying datatype: bool
@ LITE3_TYPE_BYTES
coverted to base64 string in JSON
@ LITE3_TYPE_F64
maps to 'number' type in JSON; underlying datatype: double
@ LITE3_TYPE_I64
maps to 'number' type in JSON; underlying datatype: int64_t
@ LITE3_TYPE_NULL
maps to 'null' type in JSON
static int lite3_count(unsigned char *buf, size_t buflen, size_t ofs, uint32_t *out)
Write back the number of object entries or array elements.
static enum lite3_type lite3_arr_get_type(const unsigned char *buf, size_t buflen, size_t ofs, uint32_t index)
Find value by index and return value type.
Struct holding a reference to a bytes value inside a Lite³ buffer.
Struct containing iterator state.
Struct holding a reference to a string inside a Lite³ buffer.
Struct representing a value inside a Lite³ buffer.