From 9e21e0c4b2bf16d20257f16ad82dab5d4fe72d02 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sat, 1 Nov 2025 10:04:28 +0000 Subject: [PATCH] serial: toml: update iterator usage --- serial/toml.c | 64 +++++++++++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/serial/toml.c b/serial/toml.c index e415e28..76e336a 100644 --- a/serial/toml.c +++ b/serial/toml.c @@ -111,7 +111,7 @@ struct ctx { b_stream *ctx_src; b_string *ctx_wordbuf; b_string *ctx_linebuf; - b_string_iterator ctx_linebuf_ptr; + b_iterator *ctx_linebuf_ptr; enum b_status ctx_status; b_hashmap *ctx_objects_flags; @@ -207,16 +207,16 @@ static enum b_status data_available(struct ctx *ctx) return B_ERR_NO_DATA; } - if (!B_OK(ctx->ctx_linebuf_ptr.status)) { - return ctx->ctx_linebuf_ptr.status; - } - - return b_string_iterator_is_valid(&ctx->ctx_linebuf_ptr) ? B_SUCCESS - : B_ERR_NO_DATA; + return b_iterator_get_status(ctx->ctx_linebuf_ptr); } static enum b_status refill_linebuf(struct ctx *ctx) { + if (ctx->ctx_linebuf_ptr) { + b_iterator_unref(ctx->ctx_linebuf_ptr); + ctx->ctx_linebuf_ptr = NULL; + } + b_string_clear(ctx->ctx_linebuf); b_stringstream *buf = b_stringstream_create(); @@ -233,7 +233,7 @@ static enum b_status refill_linebuf(struct ctx *ctx) b_stringstream_unref(buf); - b_string_iterator_begin(ctx->ctx_linebuf, &ctx->ctx_linebuf_ptr); + ctx->ctx_linebuf_ptr = b_iterator_begin(ctx->ctx_linebuf); return B_SUCCESS; } @@ -282,19 +282,15 @@ static b_wchar advance_char(struct ctx *ctx) } const char *s = b_string_ptr(ctx->ctx_linebuf); - if (!B_OK(ctx->ctx_linebuf_ptr.status)) { - ctx->ctx_status = B_ERR_BAD_FORMAT; - return -1; - } - b_wchar c = ctx->ctx_linebuf_ptr.char_value; + b_wchar c = b_iterator_get_value(ctx->ctx_linebuf_ptr).v_int; if (!is_valid_char(c)) { ctx->ctx_status = B_ERR_BAD_FORMAT; return -1; } - b_string_iterator_next(&ctx->ctx_linebuf_ptr); + b_iterator_move_next(ctx->ctx_linebuf_ptr); return c; } @@ -318,12 +314,8 @@ static b_wchar peek_char(struct ctx *ctx) } const char *s = b_string_ptr(ctx->ctx_linebuf); - if (!B_OK(ctx->ctx_linebuf_ptr.status)) { - ctx->ctx_status = B_ERR_BAD_FORMAT; - return -1; - } - b_wchar c = ctx->ctx_linebuf_ptr.char_value; + b_wchar c = b_iterator_get_value(ctx->ctx_linebuf_ptr).v_int; if (!is_valid_char(c)) { ctx->ctx_status = B_ERR_BAD_FORMAT; @@ -909,23 +901,26 @@ static void split_word(struct ctx *ctx, b_string *wordbuf) #endif const char *delims[] = {"."}; size_t nr_delims = sizeof delims / sizeof delims[0]; - b_string_iterator it; - b_string_tokenise( - wordbuf, delims, nr_delims, B_STRING_TOK_F_INCLUDE_EMPTY_TOKENS, - &it); + b_iterator *it = b_string_tokenise( + wordbuf, delims, nr_delims, B_STRING_TOK_F_INCLUDE_EMPTY_TOKENS); - while (b_string_iterator_is_valid(&it)) { - if (it.iteration_index > 0) { + size_t i = 0; + b_foreach_c(const char *, tok, it) + { + if (i > 0) { enqueue_token(ctx, TOK_DOT); } - if (it.string_length > 0) { + size_t len = strlen(tok); + if (len > 0) { struct token *word = enqueue_token(ctx, TOK_WORD); - word->tok_str = b_string_create_from_cstr(it.string_value); + word->tok_str = b_string_create_from_cstr(tok); } - b_string_iterator_next(&it); + i++; } + + b_iterator_unref(it); } static void read_number(struct ctx *ctx) @@ -1012,17 +1007,17 @@ static void read_word(struct ctx *ctx) return; } - b_string_iterator it; - b_string_foreach(&it, wordbuf) + b_iterator *it = b_iterator_begin(wordbuf); + b_foreach(b_wchar, c, it) { /* only allow ASCII numbers/letters here */ - bool ok = isalnum(it.char_value) || it.char_value == '_' - || it.char_value == '-' || it.char_value == '.'; + bool ok = isalnum(c) || c == '_' || c == '-' || c == '.'; if (!ok) { ctx->ctx_status = B_ERR_BAD_FORMAT; return; } } + b_iterator_unref(it); split_word(ctx, wordbuf); } @@ -1514,6 +1509,11 @@ static struct token *peek_token(struct ctx *ctx) static void ctx_cleanup(struct ctx *ctx) { + if (ctx->ctx_linebuf_ptr) { + b_iterator_unref(ctx->ctx_linebuf_ptr); + ctx->ctx_linebuf_ptr = NULL; + } + if (ctx->ctx_linebuf) { b_string_unref(ctx->ctx_linebuf); ctx->ctx_linebuf = NULL;