From 2705099d300189f2e1e4b6683ac4b6d575f550ef Mon Sep 17 00:00:00 2001 From: s3lph Date: Fri, 29 Nov 2024 23:43:50 +0100 Subject: [PATCH] feat: deduplicate code snippets in tok_read --- honeywell-config.c | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/honeywell-config.c b/honeywell-config.c index 14ac575..33385b9 100644 --- a/honeywell-config.c +++ b/honeywell-config.c @@ -51,10 +51,16 @@ void tok_init(struct tokenizer *t, int (*cb)(const char *tok, uint8_t last)) { int tok_read(struct tokenizer *t, char *buf, size_t n) { char *start = buf; - char *end = start; + char *end = NULL, *tmpend = NULL; while (!t->eot) { // non-final tokens: terminated by ; end = strchr(start, ';'); + // last token: terminated by . + tmpend = strchr(start, '.'); + if (tmpend && (tmpend < end || !end)) { + end = tmpend; + t->eot = 1; + } if (end) { if (end > start) { for (char *c = start; c < end; ++c) { @@ -64,7 +70,7 @@ int tok_read(struct tokenizer *t, char *buf, size_t n) { } } *(t->tok) = 0; - if (t->cb(t->tokbuf, 0)) { + if (t->cb(t->tokbuf, t->eot)) { return 1; } t->tok = t->tokbuf; @@ -72,25 +78,6 @@ int tok_read(struct tokenizer *t, char *buf, size_t n) { start = end + 1; continue; } - // last token: terminated by . - end = strchr(start, '.'); - if (end) { - t->eot = 1; - if (end > start) { - for (char *c = start; c < end; ++c) { - if (isprint(*c)) { - *(t->tok++) = *c; - } - } - } - *(t->tok) = 0; - if (t->cb(t->tokbuf, 1)) { - return 1; - } - t->tok = t->tokbuf; - *(t->tok) = 0; - break; - } // incomplete tokens continued in the next message end = strchr(start, '?'); if (end) {