serial: toml: update iterator usage

This commit is contained in:
2025-11-01 10:04:28 +00:00
parent 5d3a987b0e
commit 9e21e0c4b2

View File

@@ -111,7 +111,7 @@ struct ctx {
b_stream *ctx_src; b_stream *ctx_src;
b_string *ctx_wordbuf; b_string *ctx_wordbuf;
b_string *ctx_linebuf; b_string *ctx_linebuf;
b_string_iterator ctx_linebuf_ptr; b_iterator *ctx_linebuf_ptr;
enum b_status ctx_status; enum b_status ctx_status;
b_hashmap *ctx_objects_flags; b_hashmap *ctx_objects_flags;
@@ -207,16 +207,16 @@ static enum b_status data_available(struct ctx *ctx)
return B_ERR_NO_DATA; return B_ERR_NO_DATA;
} }
if (!B_OK(ctx->ctx_linebuf_ptr.status)) { return b_iterator_get_status(ctx->ctx_linebuf_ptr);
return ctx->ctx_linebuf_ptr.status;
}
return b_string_iterator_is_valid(&ctx->ctx_linebuf_ptr) ? B_SUCCESS
: B_ERR_NO_DATA;
} }
static enum b_status refill_linebuf(struct ctx *ctx) 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_string_clear(ctx->ctx_linebuf);
b_stringstream *buf = b_stringstream_create(); b_stringstream *buf = b_stringstream_create();
@@ -233,7 +233,7 @@ static enum b_status refill_linebuf(struct ctx *ctx)
b_stringstream_unref(buf); 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; return B_SUCCESS;
} }
@@ -282,19 +282,15 @@ static b_wchar advance_char(struct ctx *ctx)
} }
const char *s = b_string_ptr(ctx->ctx_linebuf); 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)) { if (!is_valid_char(c)) {
ctx->ctx_status = B_ERR_BAD_FORMAT; ctx->ctx_status = B_ERR_BAD_FORMAT;
return -1; return -1;
} }
b_string_iterator_next(&ctx->ctx_linebuf_ptr); b_iterator_move_next(ctx->ctx_linebuf_ptr);
return c; return c;
} }
@@ -318,12 +314,8 @@ static b_wchar peek_char(struct ctx *ctx)
} }
const char *s = b_string_ptr(ctx->ctx_linebuf); 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)) { if (!is_valid_char(c)) {
ctx->ctx_status = B_ERR_BAD_FORMAT; ctx->ctx_status = B_ERR_BAD_FORMAT;
@@ -909,23 +901,26 @@ static void split_word(struct ctx *ctx, b_string *wordbuf)
#endif #endif
const char *delims[] = {"."}; const char *delims[] = {"."};
size_t nr_delims = sizeof delims / sizeof delims[0]; size_t nr_delims = sizeof delims / sizeof delims[0];
b_string_iterator it; b_iterator *it = b_string_tokenise(
b_string_tokenise( wordbuf, delims, nr_delims, B_STRING_TOK_F_INCLUDE_EMPTY_TOKENS);
wordbuf, delims, nr_delims, B_STRING_TOK_F_INCLUDE_EMPTY_TOKENS,
&it);
while (b_string_iterator_is_valid(&it)) { size_t i = 0;
if (it.iteration_index > 0) { b_foreach_c(const char *, tok, it)
{
if (i > 0) {
enqueue_token(ctx, TOK_DOT); 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); 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) static void read_number(struct ctx *ctx)
@@ -1012,17 +1007,17 @@ static void read_word(struct ctx *ctx)
return; return;
} }
b_string_iterator it; b_iterator *it = b_iterator_begin(wordbuf);
b_string_foreach(&it, wordbuf) b_foreach(b_wchar, c, it)
{ {
/* only allow ASCII numbers/letters here */ /* only allow ASCII numbers/letters here */
bool ok = isalnum(it.char_value) || it.char_value == '_' bool ok = isalnum(c) || c == '_' || c == '-' || c == '.';
|| it.char_value == '-' || it.char_value == '.';
if (!ok) { if (!ok) {
ctx->ctx_status = B_ERR_BAD_FORMAT; ctx->ctx_status = B_ERR_BAD_FORMAT;
return; return;
} }
} }
b_iterator_unref(it);
split_word(ctx, wordbuf); split_word(ctx, wordbuf);
} }
@@ -1514,6 +1509,11 @@ static struct token *peek_token(struct ctx *ctx)
static void ctx_cleanup(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) { if (ctx->ctx_linebuf) {
b_string_unref(ctx->ctx_linebuf); b_string_unref(ctx->ctx_linebuf);
ctx->ctx_linebuf = NULL; ctx->ctx_linebuf = NULL;