serial: update to use new object system

This commit is contained in:
2025-10-19 21:01:27 +01:00
parent 297ccdc9ce
commit 6e979579ea
5 changed files with 169 additions and 184 deletions

View File

@@ -119,7 +119,7 @@ struct ctx {
};
static void ctx_set_object_flags(
struct ctx *ctx, struct b_dsref *obj, enum object_flags flags)
struct ctx *ctx, b_object *obj, enum object_flags flags)
{
if (!obj) {
return;
@@ -127,7 +127,7 @@ static void ctx_set_object_flags(
b_hashmap_key key = {
.key_data = obj,
.key_size = sizeof(struct b_dsref *),
.key_size = sizeof(b_object *),
.key_flags = B_HASHMAP_KEY_F_INTVALUE,
};
@@ -150,7 +150,7 @@ static void ctx_set_object_flags(
}
static void ctx_clear_object_flags(
struct ctx *ctx, struct b_dsref *obj, enum object_flags mask)
struct ctx *ctx, b_object *obj, enum object_flags mask)
{
if (!obj) {
return;
@@ -158,7 +158,7 @@ static void ctx_clear_object_flags(
b_hashmap_key key = {
.key_data = obj,
.key_size = sizeof(struct b_dsref *),
.key_size = sizeof(b_object *),
.key_flags = B_HASHMAP_KEY_F_INTVALUE,
};
@@ -180,7 +180,7 @@ static void ctx_clear_object_flags(
b_hashmap_put(ctx->ctx_objects_flags, &key, &value);
}
static enum object_flags ctx_get_object_flags(struct ctx *ctx, struct b_dsref *obj)
static enum object_flags ctx_get_object_flags(struct ctx *ctx, b_object *obj)
{
if (!obj) {
return 0;
@@ -188,7 +188,7 @@ static enum object_flags ctx_get_object_flags(struct ctx *ctx, struct b_dsref *o
b_hashmap_key key = {
.key_data = obj,
.key_size = sizeof(struct b_dsref *),
.key_size = sizeof(b_object *),
.key_flags = B_HASHMAP_KEY_F_INTVALUE,
};
@@ -231,7 +231,7 @@ static enum b_status refill_linebuf(struct ctx *ctx)
return B_SUCCESS;
}
static struct b_string *get_wordbuf(struct ctx *ctx)
static b_string *get_wordbuf(struct ctx *ctx)
{
b_string_clear(ctx->ctx_wordbuf);
return ctx->ctx_wordbuf;
@@ -394,7 +394,7 @@ static void discard_token(struct ctx *ctx)
free(tok);
}
static bool try_convert_word_to_timestamp(struct ctx *ctx, struct b_string *token_str)
static bool try_convert_word_to_timestamp(struct ctx *ctx, b_string *token_str)
{
b_datetime *dt = b_datetime_parse(
B_DATETIME_FORMAT_RFC3339, b_string_ptr(token_str));
@@ -410,7 +410,7 @@ static bool try_convert_word_to_timestamp(struct ctx *ctx, struct b_string *toke
}
#if 0
static bool try_convert_word_to_timestamp(struct ctx *ctx, struct b_string *token_str)
static bool try_convert_word_to_timestamp(struct ctx *ctx, b_string *token_str)
{
const char *s = b_string_ptr(token_str);
size_t len = b_string_get_size(token_str, B_STRLEN_NORMAL);
@@ -614,10 +614,10 @@ static bool has_trailing_zero(const char *s)
return false;
}
static bool try_convert_word_to_number(struct ctx *ctx, struct b_string *token_str)
static bool try_convert_word_to_number(struct ctx *ctx, b_string *token_str)
{
size_t len = b_string_get_size(token_str, B_STRLEN_NORMAL);
struct b_string *str = b_string_duplicate(token_str);
b_string *str = b_string_duplicate(token_str);
struct token *tok = NULL;
const char *s = b_string_ptr(str);
@@ -817,7 +817,7 @@ static bool try_convert_word_to_number(struct ctx *ctx, struct b_string *token_s
return true;
}
static bool try_convert_word_to_bool(struct ctx *ctx, struct b_string *token_str)
static bool try_convert_word_to_bool(struct ctx *ctx, b_string *token_str)
{
const char *s = b_string_ptr(token_str);
struct token *tok = NULL;
@@ -837,7 +837,7 @@ static bool try_convert_word_to_bool(struct ctx *ctx, struct b_string *token_str
return true;
}
static void split_word(struct ctx *ctx, struct b_string *wordbuf)
static void split_word(struct ctx *ctx, b_string *wordbuf)
{
#if 0
long len = b_string_get_size(wordbuf, B_STRLEN_NORMAL);
@@ -924,7 +924,7 @@ static void split_word(struct ctx *ctx, struct b_string *wordbuf)
static void read_number(struct ctx *ctx)
{
b_wchar c = 0;
struct b_string *wordbuf = get_wordbuf(ctx);
b_string *wordbuf = get_wordbuf(ctx);
while (1) {
c = peek_char(ctx);
@@ -954,7 +954,7 @@ static void read_number(struct ctx *ctx)
static void read_word(struct ctx *ctx)
{
b_wchar c = 0;
struct b_string *wordbuf = get_wordbuf(ctx);
b_string *wordbuf = get_wordbuf(ctx);
while (1) {
c = peek_char(ctx);
@@ -1084,7 +1084,7 @@ static void read_string(struct ctx *ctx, bool squote)
bool multiline = false;
struct token *tok = enqueue_token(ctx, TOK_STRING);
struct b_string *str = get_wordbuf(ctx);
b_string *str = get_wordbuf(ctx);
b_wchar c = peek_char(ctx);
if (c == term) {
@@ -1513,17 +1513,17 @@ static void ctx_cleanup(struct ctx *ctx)
}
if (ctx->ctx_linebuf) {
b_string_release(ctx->ctx_linebuf);
b_string_unref(ctx->ctx_linebuf);
ctx->ctx_linebuf = NULL;
}
if (ctx->ctx_wordbuf) {
b_string_release(ctx->ctx_wordbuf);
b_string_unref(ctx->ctx_wordbuf);
ctx->ctx_wordbuf = NULL;
}
if (ctx->ctx_objects_flags) {
b_hashmap_release(ctx->ctx_objects_flags);
b_hashmap_unref(ctx->ctx_objects_flags);
ctx->ctx_objects_flags = NULL;
}
}
@@ -1543,7 +1543,7 @@ static enum b_status ctx_init(struct ctx *ctx)
}
static enum b_status toml_serialise(
struct b_serial_ctx *serial, struct b_dsref *src, struct b_stream *dest,
struct b_serial_ctx *serial, b_object *src, struct b_stream *dest,
enum b_serial_flags flags)
{
return B_SUCCESS;
@@ -1636,35 +1636,35 @@ static void print_token(struct token *tok)
}
}
static enum b_status parse_value(struct ctx *ctx, struct b_dsref **result);
static enum b_status parse_key_value_pair(struct ctx *ctx, struct b_dict *container);
static enum b_status parse_value(struct ctx *ctx, b_object **result);
static enum b_status parse_key_value_pair(struct ctx *ctx, b_dict *container);
static enum b_status parse_timestamp(struct ctx *ctx, struct b_dsref **result)
static enum b_status parse_timestamp(struct ctx *ctx, b_object **result)
{
struct token *tok = peek_token(ctx);
struct b_datetime *dt = tok->tok_value.time;
b_datetime *dt = tok->tok_value.time;
tok->tok_value.time = NULL;
*result = B_DSREF(dt);
*result = (dt);
return B_SUCCESS;
}
static enum b_status parse_string(struct ctx *ctx, struct b_dsref **result)
static enum b_status parse_string(struct ctx *ctx, b_object **result)
{
struct token *tok = peek_token(ctx);
struct b_string *str = b_string_duplicate(tok->tok_str);
b_string *str = b_string_duplicate(tok->tok_str);
if (!str) {
return B_ERR_NO_MEMORY;
}
*result = B_DSREF(str);
*result = (str);
return B_SUCCESS;
}
static enum b_status parse_int(struct ctx *ctx, struct b_dsref **result)
static enum b_status parse_int(struct ctx *ctx, b_object **result)
{
struct token *tok = peek_token(ctx);
struct b_number *val = B_LONGLONG(tok->tok_value.i.v);
b_number *val = B_LONGLONG(tok->tok_value.i.v);
if (!val) {
return B_ERR_NO_MEMORY;
}
@@ -1683,14 +1683,14 @@ static enum b_status parse_int(struct ctx *ctx, struct b_dsref **result)
}
}
*result = B_DSREF(val);
*result = (val);
return B_SUCCESS;
}
static enum b_status parse_float(struct ctx *ctx, struct b_dsref **result)
static enum b_status parse_float(struct ctx *ctx, b_object **result)
{
struct token *tok = peek_token(ctx);
struct b_number *val = B_DOUBLE(tok->tok_value.f.v);
b_number *val = B_DOUBLE(tok->tok_value.f.v);
if (!val) {
return B_ERR_NO_MEMORY;
}
@@ -1709,53 +1709,53 @@ static enum b_status parse_float(struct ctx *ctx, struct b_dsref **result)
}
}
*result = B_DSREF(val);
*result = (val);
return B_SUCCESS;
}
static enum b_status parse_bool(struct ctx *ctx, struct b_dsref **result)
static enum b_status parse_bool(struct ctx *ctx, b_object **result)
{
struct token *tok = peek_token(ctx);
struct b_number *val = B_INT8(tok->tok_value.b);
b_number *val = B_INT8(tok->tok_value.b);
if (!val) {
return B_ERR_NO_MEMORY;
}
*result = B_DSREF(val);
*result = (val);
return B_SUCCESS;
}
static enum b_status parse_table_inline(struct ctx *ctx, struct b_dsref **result)
static enum b_status parse_table_inline(struct ctx *ctx, b_object **result)
{
DISABLE_EXTENDED_LEXING(ctx);
advance_token(ctx);
struct b_dict *table = b_dict_create();
b_dict *table = b_dict_create();
if (!table) {
return B_ERR_NO_MEMORY;
}
struct token *tok = peek_token(ctx);
if (tok && tok->tok_type == TOK_RIGHT_BRACE) {
*result = B_DSREF(table);
*result = (table);
return B_SUCCESS;
}
bool done = false;
while (!done) {
struct b_dsref *value;
b_object *value;
enum b_status status = parse_key_value_pair(ctx, table);
if (!B_OK(status)) {
b_dict_release(table);
b_dict_unref(table);
return status;
}
tok = peek_token(ctx);
if (!tok) {
b_dict_release(table);
b_dict_unref(table);
return status;
}
@@ -1767,12 +1767,12 @@ static enum b_status parse_table_inline(struct ctx *ctx, struct b_dsref **result
advance_token(ctx);
break;
default:
b_dict_release(table);
b_dict_unref(table);
return B_ERR_BAD_FORMAT;
}
}
*result = B_DSREF(table);
*result = (table);
return B_SUCCESS;
}
@@ -1786,21 +1786,21 @@ static void skip_newlines(struct ctx *ctx)
}
}
static enum b_status parse_array_inline(struct ctx *ctx, struct b_dsref **result)
static enum b_status parse_array_inline(struct ctx *ctx, b_object **result)
{
bool done = false;
ENABLE_EXTENDED_LEXING(ctx);
advance_token(ctx);
struct b_array *array = b_array_create();
b_array *array = b_array_create();
if (!array) {
return B_ERR_NO_MEMORY;
}
struct token *tok = peek_token(ctx);
if (!tok) {
b_array_release(array);
b_array_unref(array);
return B_ERR_BAD_FORMAT;
}
@@ -1814,7 +1814,7 @@ static enum b_status parse_array_inline(struct ctx *ctx, struct b_dsref **result
tok = peek_token(ctx);
if (!tok) {
b_array_release(array);
b_array_unref(array);
return B_ERR_BAD_FORMAT;
}
@@ -1823,10 +1823,10 @@ static enum b_status parse_array_inline(struct ctx *ctx, struct b_dsref **result
break;
}
struct b_dsref *value;
b_object *value;
enum b_status status = parse_value(ctx, &value);
if (!B_OK(status)) {
b_array_release(array);
b_array_unref(array);
return status;
}
@@ -1844,7 +1844,7 @@ static enum b_status parse_array_inline(struct ctx *ctx, struct b_dsref **result
}
if (!tok || tok->tok_type != TOK_COMMA) {
b_array_release(array);
b_array_unref(array);
return B_ERR_BAD_FORMAT;
}
@@ -1853,11 +1853,11 @@ static enum b_status parse_array_inline(struct ctx *ctx, struct b_dsref **result
}
DISABLE_EXTENDED_LEXING(ctx);
*result = B_DSREF(array);
*result = (array);
return B_SUCCESS;
}
static enum b_status parse_value(struct ctx *ctx, struct b_dsref **result)
static enum b_status parse_value(struct ctx *ctx, b_object **result)
{
struct token *tok = peek_token(ctx);
@@ -1885,7 +1885,7 @@ static enum b_status parse_value(struct ctx *ctx, struct b_dsref **result)
}
}
static enum b_status parse_key_value_pair(struct ctx *ctx, struct b_dict *container)
static enum b_status parse_key_value_pair(struct ctx *ctx, b_dict *container)
{
struct token *tok = peek_token(ctx);
if (!IS_VALID_KEY_COMPONENT(tok)) {
@@ -1904,11 +1904,11 @@ static enum b_status parse_key_value_pair(struct ctx *ctx, struct b_dict *contai
}
while (tok && tok->tok_type == TOK_DOT) {
struct b_dsref *sub_dict = b_dict_at_sk(container, key);
b_object *sub_dict = b_dict_at_sk(container, key);
if (!sub_dict) {
sub_dict = B_DSREF(b_dict_create());
sub_dict = (b_dict_create());
b_dict_put_sk(container, key, B_RV(sub_dict));
} else if (sub_dict && !B_DSREF_IS(sub_dict, DICT)) {
} else if (sub_dict && !b_object_is_type(sub_dict, B_TYPE_DICT)) {
free(key);
return B_ERR_BAD_FORMAT;
}
@@ -1930,8 +1930,8 @@ static enum b_status parse_key_value_pair(struct ctx *ctx, struct b_dict *contai
return B_ERR_BAD_FORMAT;
}
container = B_DICT(sub_dict);
b_string_release(key);
container = sub_dict;
b_string_unref(key);
key = b_string_duplicate(tok->tok_str);
if (!key) {
return B_ERR_NO_MEMORY;
@@ -1956,7 +1956,7 @@ static enum b_status parse_key_value_pair(struct ctx *ctx, struct b_dict *contai
ENABLE_EXTENDED_LEXING(ctx);
advance_token(ctx);
struct b_dsref *value = NULL;
b_object *value = NULL;
enum b_status status = parse_value(ctx, &value);
DISABLE_EXTENDED_LEXING(ctx);
@@ -1972,7 +1972,8 @@ static enum b_status parse_key_value_pair(struct ctx *ctx, struct b_dict *contai
b_dict_put_sk(container, key, B_RV(value));
if (B_DSREF_IS(value, DICT) || B_DSREF_IS(value, ARRAY)) {
if (b_object_is_type(value, B_TYPE_DICT)
|| b_object_is_type(value, B_TYPE_ARRAY)) {
ctx_set_object_flags(ctx, value, OBJECT_KV_END_DEFINED);
}
@@ -1980,7 +1981,7 @@ static enum b_status parse_key_value_pair(struct ctx *ctx, struct b_dict *contai
}
static enum b_status parse_table_header(
struct ctx *ctx, struct b_dict *container, struct b_dict **new_container)
struct ctx *ctx, b_dict *container, b_dict **new_container)
{
advance_token(ctx);
struct token *tok = peek_token(ctx);
@@ -2000,17 +2001,15 @@ static enum b_status parse_table_header(
}
while (tok && tok->tok_type == TOK_DOT) {
struct b_dsref *sub_dict = b_dict_at_sk(container, key);
b_object *sub_dict = b_dict_at_sk(container, key);
enum object_flags flags = ctx_get_object_flags(ctx, sub_dict);
if (!sub_dict) {
sub_dict = B_DSREF(b_dict_create());
sub_dict = (b_dict_create());
b_dict_put_sk(container, key, B_RV(sub_dict));
} else if (B_DSREF_IS(sub_dict, ARRAY)) {
} else if (b_object_is_type(sub_dict, B_TYPE_ARRAY)) {
sub_dict = b_array_at(
B_ARRAY(sub_dict),
b_array_size(B_ARRAY(sub_dict)) - 1);
} else if (!B_DSREF_IS(sub_dict, DICT)) {
sub_dict = b_array_at(sub_dict, b_array_size(sub_dict) - 1);
} else if (!b_object_is_type(sub_dict, B_TYPE_DICT)) {
return B_ERR_BAD_FORMAT;
}
@@ -2026,8 +2025,8 @@ static enum b_status parse_table_header(
ctx_set_object_flags(ctx, sub_dict, OBJECT_HEADER_MID_DEFINED);
container = B_DICT(sub_dict);
b_string_release(key);
container = sub_dict;
b_string_unref(key);
key = b_string_duplicate(tok->tok_str);
if (!key) {
return B_ERR_NO_MEMORY;
@@ -2041,7 +2040,7 @@ static enum b_status parse_table_header(
return B_ERR_BAD_FORMAT;
}
struct b_dict *new_table = B_DICT(b_dict_at_sk(container, key));
b_dict *new_table = b_dict_at_sk(container, key);
if (!new_table) {
new_table = b_dict_create();
@@ -2054,19 +2053,19 @@ static enum b_status parse_table_header(
b_dict_put_sk(container, key, B_RV(new_table));
}
if (!B_DSREF_IS(B_DSREF(new_table), DICT)) {
if (!b_object_is_type((new_table), B_TYPE_DICT)) {
return B_ERR_BAD_FORMAT;
}
enum object_flags flags = ctx_get_object_flags(ctx, B_DSREF(new_table));
enum object_flags flags = ctx_get_object_flags(ctx, (new_table));
if (flags
& (OBJECT_HEADER_END_DEFINED | OBJECT_KV_MID_DEFINED
| OBJECT_KV_END_DEFINED)) {
return B_ERR_BAD_FORMAT;
}
ctx_set_object_flags(ctx, B_DSREF(new_table), OBJECT_HEADER_END_DEFINED);
b_string_release(key);
ctx_set_object_flags(ctx, (new_table), OBJECT_HEADER_END_DEFINED);
b_string_unref(key);
advance_token(ctx);
*new_container = new_table;
@@ -2074,7 +2073,7 @@ static enum b_status parse_table_header(
}
static enum b_status parse_array_header(
struct ctx *ctx, struct b_dict *container, struct b_dict **new_container)
struct ctx *ctx, b_dict *container, b_dict **new_container)
{
advance_token(ctx);
struct token *tok = peek_token(ctx);
@@ -2094,15 +2093,13 @@ static enum b_status parse_array_header(
}
while (tok && tok->tok_type == TOK_DOT) {
struct b_dsref *sub_dict = b_dict_at_sk(container, key);
b_object *sub_dict = b_dict_at_sk(container, key);
if (!sub_dict) {
sub_dict = B_DSREF(b_dict_create());
sub_dict = (b_dict_create());
b_dict_put_sk(container, key, B_RV(sub_dict));
} else if (B_DSREF_IS(sub_dict, ARRAY)) {
sub_dict = b_array_at(
B_ARRAY(sub_dict),
b_array_size(B_ARRAY(sub_dict)) - 1);
} else if (!B_DSREF_IS(sub_dict, DICT)) {
} else if (b_object_is_type(sub_dict, B_TYPE_ARRAY)) {
sub_dict = b_array_at(sub_dict, b_array_size(sub_dict) - 1);
} else if (!b_object_is_type(sub_dict, B_TYPE_DICT)) {
return B_ERR_BAD_FORMAT;
}
@@ -2112,8 +2109,8 @@ static enum b_status parse_array_header(
return B_ERR_BAD_FORMAT;
}
container = B_DICT(sub_dict);
b_string_release(key);
container = sub_dict;
b_string_unref(key);
key = b_string_duplicate(tok->tok_str);
if (!key) {
return B_ERR_NO_MEMORY;
@@ -2127,21 +2124,21 @@ static enum b_status parse_array_header(
return B_ERR_BAD_FORMAT;
}
struct b_array *array = B_ARRAY(b_dict_get_sk(container, key));
b_array *array = b_dict_get_sk(container, key);
if (!array) {
array = b_array_create();
b_dict_put_sk(container, key, B_RV(array));
} else if (!B_DSREF_IS(array, ARRAY)) {
} else if (!b_object_is_type(array, B_TYPE_ARRAY)) {
return B_ERR_BAD_FORMAT;
}
free(key);
enum object_flags flags = ctx_get_object_flags(ctx, B_DSREF(array));
enum object_flags flags = ctx_get_object_flags(ctx, (array));
if (flags & OBJECT_KV_END_DEFINED) {
return B_ERR_NO_MEMORY;
}
struct b_dict *new_table = b_dict_create();
b_dict *new_table = b_dict_create();
if (!new_table) {
return B_ERR_NO_MEMORY;
}
@@ -2153,11 +2150,11 @@ static enum b_status parse_array_header(
return B_SUCCESS;
}
static enum b_status parse_root(struct ctx *ctx, struct b_dict **result)
static enum b_status parse_root(struct ctx *ctx, b_dict **result)
{
enum b_status status = B_SUCCESS;
struct b_dict *root = b_dict_create();
struct b_dict *current = root;
b_dict *root = b_dict_create();
b_dict *current = root;
while (!(ctx->ctx_flags & CTX_EOF) && B_OK(status)) {
struct token *tok = peek_token(ctx);
@@ -2215,7 +2212,7 @@ static enum b_status parse_root(struct ctx *ctx, struct b_dict **result)
}
if (!B_OK(status)) {
b_dict_release(root);
b_dict_unref(root);
root = NULL;
}
@@ -2224,8 +2221,8 @@ static enum b_status parse_root(struct ctx *ctx, struct b_dict **result)
}
static enum b_status toml_deserialise(
struct b_serial_ctx *serial, struct b_stream *src,
struct b_dsref **dest, enum b_serial_flags flags)
struct b_serial_ctx *serial, struct b_stream *src, b_object **dest,
enum b_serial_flags flags)
{
struct ctx ctx = {0};
enum b_status status = ctx_init(&ctx);
@@ -2244,17 +2241,17 @@ static enum b_status toml_deserialise(
}
if (ctx.ctx_flags & CTX_EOF) {
*dest = B_DSREF(b_dict_create());
*dest = (b_dict_create());
return B_SUCCESS;
}
struct b_dict *result = NULL;
b_dict *result = NULL;
status = parse_root(&ctx, &result);
if (!B_OK(status)) {
return status;
}
*dest = B_DSREF(result);
*dest = (result);
#if 0
ctx.ctx_flags
= CTX_ENABLE_NUMBERS | CTX_ENABLE_TIMESTAMPS | CTX_ENABLE_BOOLS;