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,
231 const unsigned char *buf,
245#ifndef DOXYGEN_IGNORE
275 return lite3_init_obj(ctx->buf, &ctx->buflen, ctx->bufsz);
289 return lite3_init_arr(ctx->buf, &ctx->buflen, ctx->bufsz);
330#define lite3_ctx_set_null(ctx, ofs, key) ({ \
331 const char *__lite3_key__ = (key); \
332 lite3_ctx_set_null_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
334#ifndef DOXYGEN_IGNORE
335static inline int lite3_ctx_set_null_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
338 if ((ret = _lite3_verify_obj_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
342 while ((ret = lite3_set_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_NULL], &val)) < 0) {
343 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
365#define lite3_ctx_set_bool(ctx, ofs, key, value) ({ \
366 const char *__lite3_key__ = (key); \
367 _lite3_ctx_set_bool_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), value); \
369#ifndef DOXYGEN_IGNORE
370static inline int _lite3_ctx_set_bool_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data,
bool value)
373 if ((ret = _lite3_verify_obj_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
377 while ((ret = lite3_set_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_BOOL], &val)) < 0) {
378 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
401#define lite3_ctx_set_i64(ctx, ofs, key, value) ({ \
402 const char *__lite3_key__ = (key); \
403 _lite3_ctx_set_i64_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), value); \
405#ifndef DOXYGEN_IGNORE
406static 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)
409 if ((ret = _lite3_verify_obj_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
413 while ((ret = lite3_set_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_I64], &val)) < 0) {
414 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
437#define lite3_ctx_set_f64(ctx, ofs, key, value) ({ \
438 const char *__lite3_key__ = (key); \
439 _lite3_ctx_set_f64_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), value); \
441#ifndef DOXYGEN_IGNORE
442static inline int _lite3_ctx_set_f64_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data,
double value)
445 if ((ret = _lite3_verify_obj_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
449 while ((ret = lite3_set_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_F64], &val)) < 0) {
450 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
474#define lite3_ctx_set_bytes(ctx, ofs, key, bytes, bytes_len) ({ \
475 const char *__lite3_key__ = (key); \
476 _lite3_ctx_set_bytes_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), bytes, bytes_len); \
478#ifndef DOXYGEN_IGNORE
479static 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)
482 if ((ret = _lite3_verify_obj_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
486 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) {
487 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
515#define lite3_ctx_set_str(ctx, ofs, key, str) ({ \
516 const char *__lite3_key__ = (key); \
517 _lite3_ctx_set_str_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), str); \
519#ifndef DOXYGEN_IGNORE
520static 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)
523 if ((ret = _lite3_verify_obj_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
526 size_t str_size = strlen(str) + 1;
528 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) {
529 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
557#define lite3_ctx_set_str_n(ctx, ofs, key, str, str_len) ({ \
558 const char *__lite3_key__ = (key); \
559 _lite3_ctx_set_str_n_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), str, str_len); \
561#ifndef DOXYGEN_IGNORE
562static 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)
565 if ((ret = _lite3_verify_obj_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
568 size_t str_size = str_len + 1;
570 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) {
571 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
596#define lite3_ctx_set_obj(ctx, ofs, key, out_ofs) ({ \
597 lite3_ctx *__lite3_ctx__ = (ctx); \
598 size_t __lite3_ofs__ = (ofs); \
600 if ((__lite3_ret__ = _lite3_verify_obj_set( \
601 __lite3_ctx__->buf, \
602 &__lite3_ctx__->buflen, \
604 __lite3_ctx__->bufsz)) < 0) \
605 return __lite3_ret__; \
607 const char *__lite3_key__ = (key); \
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); \
641 if ((__lite3_ret__ = _lite3_verify_obj_set( \
642 __lite3_ctx__->buf, \
643 &__lite3_ctx__->buflen, \
645 __lite3_ctx__->bufsz)) < 0) \
646 return __lite3_ret__; \
648 const char *__lite3_key__ = (key); \
650 while ((__lite3_ret__ = lite3_set_arr_impl( \
651 __lite3_ctx__->buf, \
652 &__lite3_ctx__->buflen, \
654 __lite3_ctx__->bufsz, \
656 LITE3_KEY_DATA(key), \
658 if (errno == ENOBUFS && (lite3_ctx_grow_impl(__lite3_ctx__) == 0)) { \
661 return __lite3_ret__; \
689#ifndef DOXYGEN_IGNORE
690static inline int _lite3_ctx_set_by_index(
lite3_ctx *ctx,
size_t ofs, uint32_t index,
size_t val_len,
lite3_val **out)
693 if ((ret = _lite3_verify_arr_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
696 if (LITE3_UNLIKELY(index > size)) {
697 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: ARRAY INDEX %u OUT OF BOUNDS (size == %u)\n", index, size);
701 lite3_key_data key_data = {
706 while ((ret = lite3_set_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, NULL, key_data, val_len, out)) < 0) {
707 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
716static inline int _lite3_ctx_set_by_append(
lite3_ctx *ctx,
size_t ofs,
size_t val_len,
lite3_val **out)
719 if ((ret = _lite3_verify_arr_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
722 lite3_key_data key_data = {
727 while ((ret = lite3_set_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, NULL, key_data, val_len, out)) < 0) {
728 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
750 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[
LITE3_TYPE_NULL], &val)) < 0)
769 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[
LITE3_TYPE_BOOL], &val)) < 0)
789 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[
LITE3_TYPE_I64], &val)) < 0)
809 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[
LITE3_TYPE_F64], &val)) < 0)
825 const unsigned char *__restrict bytes,
830 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[
LITE3_TYPE_BYTES] + bytes_len, &val)) < 0)
851 const char *__restrict str)
854 size_t str_size = strlen(str) + 1;
856 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
876 const char *__restrict str,
880 size_t str_size = str_len + 1;
882 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
900 size_t *__restrict out_ofs)
903 if ((ret = _lite3_verify_arr_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
906 while ((ret = lite3_arr_append_obj_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, out_ofs)) < 0) {
907 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
925 size_t *__restrict out_ofs)
928 if ((ret = _lite3_verify_arr_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
931 while ((ret = lite3_arr_append_arr_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, out_ofs)) < 0) {
932 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
981 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[
LITE3_TYPE_NULL], &val)) < 0)
1001 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[
LITE3_TYPE_BOOL], &val)) < 0)
1022 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[
LITE3_TYPE_I64], &val)) < 0)
1043 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[
LITE3_TYPE_F64], &val)) < 0)
1060 const unsigned char *__restrict bytes,
1065 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[
LITE3_TYPE_BYTES] + bytes_len, &val)) < 0)
1087 const char *__restrict str)
1090 size_t str_size = strlen(str) + 1;
1092 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
1113 const char *__restrict str,
1117 size_t str_size = str_len + 1;
1119 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
1138 size_t *__restrict out_ofs)
1141 if ((ret = _lite3_verify_arr_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
1144 while ((ret = lite3_arr_set_obj_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, index, out_ofs)) < 0) {
1145 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
1164 size_t *__restrict out_ofs)
1167 if ((ret = _lite3_verify_arr_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
1170 while ((ret = lite3_arr_set_arr_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, index, out_ofs)) < 0) {
1171 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
1215#define lite3_ctx_get_type(ctx, ofs, key) ({ \
1216 const char *__lite3_key__ = (key); \
1217 _lite3_ctx_get_type_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1219#ifndef DOXYGEN_IGNORE
1220static inline enum lite3_type _lite3_ctx_get_type_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1222 if (_lite3_verify_obj_get(ctx->buf, ctx->buflen, ofs) < 0)
1225 if (lite3_get_impl(ctx->buf, ctx->buflen, ofs, key, key_data, &val) < 0)
1246#define lite3_ctx_get_type_size(ctx, ofs, key, out) ({ \
1247 const char *__lite3_key__ = (key); \
1248 _lite3_ctx_get_type_size_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1250#ifndef DOXYGEN_IGNORE
1251static 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)
1254 if ((ret = _lite3_verify_obj_get(ctx->buf, ctx->buflen, ofs)) < 0)
1257 if ((ret = lite3_get_impl(ctx->buf, ctx->buflen, ofs, key, key_data, &val)) < 0)
1264 *out = lite3_type_sizes[val->type];
1279#define lite3_ctx_exists(ctx, ofs, key) ({ \
1280 const char *__lite3_key__ = (key); \
1281 _lite3_ctx_exists_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1283#ifndef DOXYGEN_IGNORE
1284static inline bool _lite3_ctx_exists_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1286 if (_lite3_verify_obj_get(ctx->buf, ctx->buflen, ofs) < 0)
1289 if (lite3_get_impl(ctx->buf, ctx->buflen, ofs, key, key_data, &val) < 0)
1309 if ((ret = _lite3_verify_get(ctx->buf, ctx->buflen, ofs)) < 0)
1313 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: EXPECTING ARRAY OR OBJECT TYPE\n");
1331#define lite3_ctx_is_null(ctx, ofs, key) ({ \
1332 const char *__lite3_key__ = (key); \
1333 _lite3_ctx_is_null_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1335#ifndef DOXYGEN_IGNORE
1336static inline bool _lite3_ctx_is_null_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1338 if (_lite3_verify_obj_get(ctx->buf, ctx->buflen, ofs) < 0)
1341 if (lite3_get_impl(ctx->buf, ctx->buflen, ofs, key, key_data, &val) < 0)
1357#define lite3_ctx_is_bool(ctx, ofs, key) ({ \
1358 const char *__lite3_key__ = (key); \
1359 _lite3_ctx_is_bool_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1361#ifndef DOXYGEN_IGNORE
1362static inline bool _lite3_ctx_is_bool_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1364 if (_lite3_verify_obj_get(ctx->buf, ctx->buflen, ofs) < 0)
1367 if (lite3_get_impl(ctx->buf, ctx->buflen, ofs, key, key_data, &val) < 0)
1383#define lite3_ctx_is_i64(ctx, ofs, key) ({ \
1384 const char *__lite3_key__ = (key); \
1385 _lite3_ctx_is_i64_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1387#ifndef DOXYGEN_IGNORE
1388static inline bool _lite3_ctx_is_i64_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1390 if (_lite3_verify_obj_get(ctx->buf, ctx->buflen, ofs) < 0)
1393 if (lite3_get_impl(ctx->buf, ctx->buflen, ofs, key, key_data, &val) < 0)
1409#define lite3_ctx_is_f64(ctx, ofs, key) ({ \
1410 const char *__lite3_key__ = (key); \
1411 _lite3_ctx_is_f64_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1413#ifndef DOXYGEN_IGNORE
1414static inline bool _lite3_ctx_is_f64_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1416 if (_lite3_verify_obj_get(ctx->buf, ctx->buflen, ofs) < 0)
1419 if (lite3_get_impl(ctx->buf, ctx->buflen, ofs, key, key_data, &val) < 0)
1435#define lite3_ctx_is_bytes(ctx, ofs, key) ({ \
1436 const char *__lite3_key__ = (key); \
1437 _lite3_ctx_is_bytes_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1439#ifndef DOXYGEN_IGNORE
1440static inline bool _lite3_ctx_is_bytes_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1442 if (_lite3_verify_obj_get(ctx->buf, ctx->buflen, ofs) < 0)
1445 if (lite3_get_impl(ctx->buf, ctx->buflen, ofs, key, key_data, &val) < 0)
1461#define lite3_ctx_is_str(ctx, ofs, key) ({ \
1462 const char *__lite3_key__ = (key); \
1463 _lite3_ctx_is_str_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1465#ifndef DOXYGEN_IGNORE
1466static inline bool _lite3_ctx_is_str_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1468 if (_lite3_verify_obj_get(ctx->buf, ctx->buflen, ofs) < 0)
1471 if (lite3_get_impl(ctx->buf, ctx->buflen, ofs, key, key_data, &val) < 0)
1487#define lite3_ctx_is_obj(ctx, ofs, key) ({ \
1488 const char *__lite3_key__ = (key); \
1489 _lite3_ctx_is_obj_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1491#ifndef DOXYGEN_IGNORE
1492static inline bool _lite3_ctx_is_obj_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1494 if (_lite3_verify_obj_get(ctx->buf, ctx->buflen, ofs) < 0)
1497 if (lite3_get_impl(ctx->buf, ctx->buflen, ofs, key, key_data, &val) < 0)
1513#define lite3_ctx_is_arr(ctx, ofs, key) ({ \
1514 const char *__lite3_key__ = (key); \
1515 _lite3_ctx_is_arr_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1517#ifndef DOXYGEN_IGNORE
1518static inline bool _lite3_ctx_is_arr_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1520 if (_lite3_verify_obj_get(ctx->buf, ctx->buflen, ofs) < 0)
1523 if (lite3_get_impl(ctx->buf, ctx->buflen, ofs, key, key_data, &val) < 0)
1563#define lite3_ctx_get(ctx, ofs, key, out) ({ \
1564 lite3_ctx *__lite3_ctx__ = (ctx); \
1565 size_t __lite3_ofs__ = (ofs); \
1566 int __lite3_ret__; \
1567 if ((__lite3_ret__ = _lite3_verify_get(__lite3_ctx__->buf, __lite3_ctx__->buflen, __lite3_ofs__)) < 0) \
1568 return __lite3_ret__; \
1569 const char *__lite3_key__ = (key); \
1570 lite3_get_impl(__lite3_ctx__->buf, __lite3_ctx__->buflen, __lite3_ofs__, __lite3_key__, LITE3_KEY_DATA(key), out); \
1584#define lite3_ctx_get_bool(ctx, ofs, key, out) ({ \
1585 const char *__lite3_key__ = (key); \
1586 _lite3_ctx_get_bool_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1588#ifndef DOXYGEN_IGNORE
1589static inline int _lite3_ctx_get_bool_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data,
bool *out)
1592 if ((ret = _lite3_verify_obj_get(ctx->buf, ctx->buflen, ofs)) < 0)
1595 if ((ret = lite3_get_impl(ctx->buf, ctx->buflen, ofs, key, key_data, &val)) < 0)
1598 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_BOOL\n");
1602 *out = (bool)(*(val->val));
1618#define lite3_ctx_get_i64(ctx, ofs, key, out) ({ \
1619 const char *__lite3_key__ = (key); \
1620 _lite3_ctx_get_i64_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1622#ifndef DOXYGEN_IGNORE
1623static 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)
1626 if ((ret = _lite3_verify_obj_get(ctx->buf, ctx->buflen, ofs)) < 0)
1629 if ((ret = lite3_get_impl(ctx->buf, ctx->buflen, ofs, key, key_data, &val)) < 0)
1632 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_I64\n");
1652#define lite3_ctx_get_f64(ctx, ofs, key, out) ({ \
1653 const char *__lite3_key__ = (key); \
1654 _lite3_ctx_get_f64_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1656#ifndef DOXYGEN_IGNORE
1657static inline int _lite3_ctx_get_f64_impl(
lite3_ctx *ctx,
size_t ofs,
const char *__restrict key, lite3_key_data key_data,
double *out)
1660 if ((ret = _lite3_verify_obj_get(ctx->buf, ctx->buflen, ofs)) < 0)
1663 if ((ret = lite3_get_impl(ctx->buf, ctx->buflen, ofs, key, key_data, &val)) < 0)
1666 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_F64\n");
1686#define lite3_ctx_get_bytes(ctx, ofs, key, out) ({ \
1687 const char *__lite3_key__ = (key); \
1688 _lite3_ctx_get_bytes_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1690#ifndef DOXYGEN_IGNORE
1691static 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)
1694 if ((ret = _lite3_verify_obj_get(ctx->buf, ctx->buflen, ofs)) < 0)
1697 if ((ret = lite3_get_impl(ctx->buf, ctx->buflen, ofs, key, key_data, &val)) < 0)
1700 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_BYTES\n");
1705 .
gen = *(uint32_t *)(ctx->buf),
1725#define lite3_ctx_get_str(ctx, ofs, key, out) ({ \
1726 const char *__lite3_key__ = (key); \
1727 _lite3_ctx_get_str_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1729#ifndef DOXYGEN_IGNORE
1730static 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)
1733 if ((ret = _lite3_verify_obj_get(ctx->buf, ctx->buflen, ofs)) < 0)
1736 if ((ret = lite3_get_impl(ctx->buf, ctx->buflen, ofs, key, key_data, &val)) < 0)
1739 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_STRING\n");
1744 .
gen = *(uint32_t *)(ctx->buf),
1765#define lite3_ctx_get_obj(ctx, ofs, key, out) ({ \
1766 const char *__lite3_key__ = (key); \
1767 _lite3_ctx_get_obj_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1769#ifndef DOXYGEN_IGNORE
1770static 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)
1773 if ((ret = _lite3_verify_obj_get(ctx->buf, ctx->buflen, ofs)) < 0)
1776 if ((ret = lite3_get_impl(ctx->buf, ctx->buflen, ofs, key, key_data, &val)) < 0)
1779 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_OBJECT\n");
1783 *out_ofs = (size_t)((uint8_t *)val - ctx->buf);
1799#define lite3_ctx_get_arr(ctx, ofs, key, out) ({ \
1800 const char *__lite3_key__ = (key); \
1801 _lite3_ctx_get_arr_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1803#ifndef DOXYGEN_IGNORE
1804static 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)
1807 if ((ret = _lite3_verify_obj_get(ctx->buf, ctx->buflen, ofs)) < 0)
1810 if ((ret = lite3_get_impl(ctx->buf, ctx->buflen, ofs, key, key_data, &val)) < 0)
1813 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_ARRAY\n");
1817 *out_ofs = (size_t)((uint8_t *)val - ctx->buf);
1855 if ((ret = _lite3_get_by_index(ctx->buf, ctx->buflen, ofs, index, &val)) < 0)
1858 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_BOOL\n");
1862 *out = (bool)(*(val->val));
1880 if ((ret = _lite3_get_by_index(ctx->buf, ctx->buflen, ofs, index, &val)) < 0)
1883 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_I64\n");
1905 if ((ret = _lite3_get_by_index(ctx->buf, ctx->buflen, ofs, index, &val)) < 0)
1908 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_F64\n");
1930 if ((ret = _lite3_get_by_index(ctx->buf, ctx->buflen, ofs, index, &val)) < 0)
1933 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_BYTES\n");
1938 .
gen = *(uint32_t *)(ctx->buf),
1960 if ((ret = _lite3_get_by_index(ctx->buf, ctx->buflen, ofs, index, &val)) < 0)
1963 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_STRING\n");
1968 .
gen = *(uint32_t *)(ctx->buf),
1987 size_t *__restrict out_ofs)
1991 if ((ret = _lite3_get_by_index(ctx->buf, ctx->buflen, ofs, index, &val)) < 0)
1994 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_OBJECT\n");
1998 *out_ofs = (size_t)((uint8_t *)val - (ctx->buf));
2012 size_t *__restrict out_ofs)
2016 if ((ret = _lite3_get_by_index(ctx->buf, ctx->buflen, ofs, index, &val)) < 0)
2019 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_ARRAY\n");
2023 *out_ofs = (size_t)((uint8_t *)val - (ctx->buf));
2044#define LITE3_ITER_ITEM 1
2046#define LITE3_ITER_DONE 0
2061 if ((ret = _lite3_verify_get(ctx->buf, ctx->buflen, ofs)) < 0)
2063 return lite3_iter_create_impl(ctx->buf, ctx->buflen, ofs, out);
2086 size_t *out_val_ofs)
2088 return lite3_iter_next(ctx->buf, ctx->buflen, iter, out_key, out_val_ofs);
2123#if defined(DOXYGEN_ONLY) && !defined(LITE3_JSON)
2139 const char *__restrict json_str,
2144 while ((ret =
lite3_json_dec(ctx->buf, &ctx->buflen, ctx->bufsz, json_str, json_len)) < 0) {
2145 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
2165 const char *__restrict path)
2170 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
2195 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
2240 size_t *__restrict out_len)
2263 size_t *__restrict out_len)
2281 char *__restrict json_buf,
2303 char *__restrict json_buf,
2311 (void)ctx; (void)json_str; (void)json_len;
2317 (void)ctx; (void)path;
2323 (void)ctx; (void)fp;
2329 (void)ctx; (void)ofs;
2335 (void)ctx; (void)ofs; (void)out_len;
2341 (void)ctx; (void)ofs; (void)out_len;
2347 (void)ctx; (void)ofs; (void)json_buf; (void)json_bufsz;
2353 (void)ctx; (void)ofs; (void)json_buf; (void)json_bufsz;
#define LITE3_CONTEXT_BUF_SIZE_MIN
The minimum buffer size for a Lite³ context.
#define LITE3_NODE_ALIGNMENT
B-tree node alignment configuration.
#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 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.
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_ARRAY
maps to 'array' type in JSON
@ 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_OBJECT
maps to 'object' type in JSON
@ LITE3_TYPE_I64
maps to 'number' type in JSON; underlying datatype: int64_t
@ LITE3_TYPE_NULL
maps to 'null' type in JSON
Struct holding a reference to a bytes value inside a Lite³ buffer.
uint32_t gen
generation of the Lite³ buffer when this struct was returned
uint32_t len
byte array length (bytes)
Struct containing iterator state.
Struct holding a reference to a string inside a Lite³ buffer.
uint32_t len
char array length (characters, exclusive of NULL-terminator)
uint32_t gen
generation of the Lite³ buffer when this struct was returned
Struct representing a value inside a Lite³ buffer.