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)
697 if (LITE3_UNLIKELY(index > size)) {
698 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: ARRAY INDEX %u OUT OF BOUNDS (size == %u)\n", index, size);
702 lite3_key_data key_data = {
707 while ((ret = lite3_set_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, NULL, key_data, val_len, out)) < 0) {
708 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
717static inline int _lite3_ctx_set_by_append(
lite3_ctx *ctx,
size_t ofs,
size_t val_len,
lite3_val **out)
720 if ((ret = _lite3_verify_arr_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
723 lite3_key_data key_data = {
728 while ((ret = lite3_set_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, NULL, key_data, val_len, out)) < 0) {
729 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
751 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[
LITE3_TYPE_NULL], &val)) < 0)
770 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[
LITE3_TYPE_BOOL], &val)) < 0)
790 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[
LITE3_TYPE_I64], &val)) < 0)
810 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[
LITE3_TYPE_F64], &val)) < 0)
826 const unsigned char *__restrict bytes,
831 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[
LITE3_TYPE_BYTES] + bytes_len, &val)) < 0)
852 const char *__restrict str)
855 size_t str_size = strlen(str) + 1;
857 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
877 const char *__restrict str,
881 size_t str_size = str_len + 1;
883 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
901 size_t *__restrict out_ofs)
904 if ((ret = _lite3_verify_arr_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
907 while ((ret = lite3_arr_append_obj_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, out_ofs)) < 0) {
908 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
926 size_t *__restrict out_ofs)
929 if ((ret = _lite3_verify_arr_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
932 while ((ret = lite3_arr_append_arr_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, out_ofs)) < 0) {
933 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
982 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[
LITE3_TYPE_NULL], &val)) < 0)
1002 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[
LITE3_TYPE_BOOL], &val)) < 0)
1023 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[
LITE3_TYPE_I64], &val)) < 0)
1044 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[
LITE3_TYPE_F64], &val)) < 0)
1061 const unsigned char *__restrict bytes,
1066 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[
LITE3_TYPE_BYTES] + bytes_len, &val)) < 0)
1088 const char *__restrict str)
1091 size_t str_size = strlen(str) + 1;
1093 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
1114 const char *__restrict str,
1118 size_t str_size = str_len + 1;
1120 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
1139 size_t *__restrict out_ofs)
1142 if ((ret = _lite3_verify_arr_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
1145 while ((ret = lite3_arr_set_obj_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, index, out_ofs)) < 0) {
1146 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
1165 size_t *__restrict out_ofs)
1168 if ((ret = _lite3_verify_arr_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
1171 while ((ret = lite3_arr_set_arr_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, index, out_ofs)) < 0) {
1172 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
1216 if (_lite3_verify_get(ctx->buf, ctx->buflen, 0) < 0)
1231#define lite3_ctx_get_type(ctx, ofs, key) ({ \
1232 const char *__lite3_key__ = (key); \
1233 _lite3_ctx_get_type_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1235#ifndef DOXYGEN_IGNORE
1236static inline enum lite3_type _lite3_ctx_get_type_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1238 return _lite3_get_type_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1271#define lite3_ctx_get_type_size(ctx, ofs, key, out) ({ \
1272 const char *__lite3_key__ = (key); \
1273 _lite3_ctx_get_type_size_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1275#ifndef DOXYGEN_IGNORE
1276static 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)
1278 return _lite3_get_type_size_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out);
1292#define lite3_ctx_exists(ctx, ofs, key) ({ \
1293 const char *__lite3_key__ = (key); \
1294 _lite3_ctx_exists_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1296#ifndef DOXYGEN_IGNORE
1297static inline bool _lite3_ctx_exists_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1299 return _lite3_exists_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1316 return lite3_count(ctx->buf, ctx->buflen, ofs, out);
1329#define lite3_ctx_is_null(ctx, ofs, key) ({ \
1330 const char *__lite3_key__ = (key); \
1331 _lite3_ctx_is_null_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1333#ifndef DOXYGEN_IGNORE
1334static inline bool _lite3_ctx_is_null_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1336 return _lite3_is_null_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1350#define lite3_ctx_is_bool(ctx, ofs, key) ({ \
1351 const char *__lite3_key__ = (key); \
1352 _lite3_ctx_is_bool_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1354#ifndef DOXYGEN_IGNORE
1355static inline bool _lite3_ctx_is_bool_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1357 return _lite3_is_bool_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1371#define lite3_ctx_is_i64(ctx, ofs, key) ({ \
1372 const char *__lite3_key__ = (key); \
1373 _lite3_ctx_is_i64_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1375#ifndef DOXYGEN_IGNORE
1376static inline bool _lite3_ctx_is_i64_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1378 return _lite3_is_i64_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1392#define lite3_ctx_is_f64(ctx, ofs, key) ({ \
1393 const char *__lite3_key__ = (key); \
1394 _lite3_ctx_is_f64_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1396#ifndef DOXYGEN_IGNORE
1397static inline bool _lite3_ctx_is_f64_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1399 return _lite3_is_f64_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1413#define lite3_ctx_is_bytes(ctx, ofs, key) ({ \
1414 const char *__lite3_key__ = (key); \
1415 _lite3_ctx_is_bytes_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1417#ifndef DOXYGEN_IGNORE
1418static inline bool _lite3_ctx_is_bytes_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1420 return _lite3_is_bytes_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1434#define lite3_ctx_is_str(ctx, ofs, key) ({ \
1435 const char *__lite3_key__ = (key); \
1436 _lite3_ctx_is_str_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1438#ifndef DOXYGEN_IGNORE
1439static inline bool _lite3_ctx_is_str_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1441 return _lite3_is_str_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1455#define lite3_ctx_is_obj(ctx, ofs, key) ({ \
1456 const char *__lite3_key__ = (key); \
1457 _lite3_ctx_is_obj_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1459#ifndef DOXYGEN_IGNORE
1460static inline bool _lite3_ctx_is_obj_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1463 return _lite3_is_obj_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1477#define lite3_ctx_is_arr(ctx, ofs, key) ({ \
1478 const char *__lite3_key__ = (key); \
1479 _lite3_ctx_is_arr_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1481#ifndef DOXYGEN_IGNORE
1482static inline bool _lite3_ctx_is_arr_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1484 return _lite3_is_arr_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1522#define lite3_ctx_get(ctx, ofs, key, out) ({ \
1523 lite3_ctx *__lite3_ctx__ = (ctx); \
1524 size_t __lite3_ofs__ = (ofs); \
1525 int __lite3_ret__; \
1526 if ((__lite3_ret__ = _lite3_verify_get(__lite3_ctx__->buf, __lite3_ctx__->buflen, __lite3_ofs__)) < 0) \
1527 return __lite3_ret__; \
1528 const char *__lite3_key__ = (key); \
1529 lite3_get_impl(__lite3_ctx__->buf, __lite3_ctx__->buflen, __lite3_ofs__, __lite3_key__, LITE3_KEY_DATA(key), out); \
1543#define lite3_ctx_get_bool(ctx, ofs, key, out) ({ \
1544 const char *__lite3_key__ = (key); \
1545 _lite3_ctx_get_bool_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1547#ifndef DOXYGEN_IGNORE
1548static inline int _lite3_ctx_get_bool_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data,
bool *out)
1550 return _lite3_get_bool_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out);
1565#define lite3_ctx_get_i64(ctx, ofs, key, out) ({ \
1566 const char *__lite3_key__ = (key); \
1567 _lite3_ctx_get_i64_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1569#ifndef DOXYGEN_IGNORE
1570static 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)
1572 return _lite3_get_i64_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out);
1587#define lite3_ctx_get_f64(ctx, ofs, key, out) ({ \
1588 const char *__lite3_key__ = (key); \
1589 _lite3_ctx_get_f64_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1591#ifndef DOXYGEN_IGNORE
1592static inline int _lite3_ctx_get_f64_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data,
double *out)
1594 return _lite3_get_f64_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out);
1609#define lite3_ctx_get_bytes(ctx, ofs, key, out) ({ \
1610 const char *__lite3_key__ = (key); \
1611 _lite3_ctx_get_bytes_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1613#ifndef DOXYGEN_IGNORE
1614static 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)
1616 return _lite3_get_bytes_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out);
1631#define lite3_ctx_get_str(ctx, ofs, key, out) ({ \
1632 const char *__lite3_key__ = (key); \
1633 _lite3_ctx_get_str_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1635#ifndef DOXYGEN_IGNORE
1636static 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)
1638 return _lite3_get_str_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out);
1653#define lite3_ctx_get_obj(ctx, ofs, key, out) ({ \
1654 const char *__lite3_key__ = (key); \
1655 _lite3_ctx_get_obj_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1657#ifndef DOXYGEN_IGNORE
1658static 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)
1660 return _lite3_get_obj_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out_ofs);
1675#define lite3_ctx_get_arr(ctx, ofs, key, out) ({ \
1676 const char *__lite3_key__ = (key); \
1677 _lite3_ctx_get_arr_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1679#ifndef DOXYGEN_IGNORE
1680static 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)
1682 return _lite3_get_arr_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out_ofs);
1790 size_t *__restrict out_ofs)
1805 size_t *__restrict out_ofs)
1827#define LITE3_ITER_ITEM 1
1829#define LITE3_ITER_DONE 0
1866 size_t *out_val_ofs)
1868 return lite3_iter_next(ctx->buf, ctx->buflen, iter, out_key, out_val_ofs);
1903#if defined(DOXYGEN_ONLY) && !defined(LITE3_JSON)
1919 const char *__restrict json_str,
1924 while ((ret =
lite3_json_dec(ctx->buf, &ctx->buflen, ctx->bufsz, json_str, json_len)) < 0) {
1925 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
1945 const char *__restrict path)
1950 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
1975 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
2020 size_t *__restrict out_len)
2043 size_t *__restrict out_len)
2061 char *__restrict json_buf,
2083 char *__restrict json_buf,
2091 (void)ctx; (void)json_str; (void)json_len;
2097 (void)ctx; (void)path;
2103 (void)ctx; (void)fp;
2109 (void)ctx; (void)ofs;
2115 (void)ctx; (void)ofs; (void)out_len;
2121 (void)ctx; (void)ofs; (void)out_len;
2127 (void)ctx; (void)ofs; (void)json_buf; (void)json_bufsz;
2133 (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.