From: Michael Tremer Date: Wed, 29 Oct 2025 15:42:55 +0000 (+0000) Subject: string: Move function that consumes a string if it matches X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4620843a709ea7c43215401945cda990126c36f3;p=telemetry.git string: Move function that consumes a string if it matches Signed-off-by: Michael Tremer --- diff --git a/src/daemon/source.c b/src/daemon/source.c index a512f6d..3a5f6a9 100644 --- a/src/daemon/source.c +++ b/src/daemon/source.c @@ -679,34 +679,6 @@ static int td_source_check_step(td_source* self, const char* path, rrd_info_t* i return 1; } -// Continues to read line as long as it matches s -static int consume(char** line, const char* s) { - char* p = *line; - - for (;;) { - // Successful if we have consumed all of s - if (!*s) - break; - - // It is bad if we have run out of input - if (!*p) - return -EBADMSG; - - // The strings don't seem to match - if (*p != *s) - return -EBADMSG; - - // Advance both pointers - p++; - s++; - } - - // Restore the line - *line = p; - - return 0; -} - static int td_source_get_info(td_source* self, rrd_info_t* info, const char* field, const char* key, rrd_info_type_t type, rrd_info_t** result) { char* p = NULL; @@ -722,22 +694,22 @@ static int td_source_get_info(td_source* self, rrd_info_t* info, p = info->key; // Lines must start with "ds[" - r = consume(&p, "ds["); + r = td_string_consume(&p, "ds["); if (r < 0) continue; // After that, we must have the field name - r = consume(&p, field); + r = td_string_consume(&p, field); if (r < 0) continue; // Then, we must have "]." - r = consume(&p, "]."); + r = td_string_consume(&p, "]."); if (r < 0) continue; // Then we must have the key - r = consume(&p, key); + r = td_string_consume(&p, key); if (r < 0) continue; diff --git a/src/daemon/string.h b/src/daemon/string.h index c3b41fb..1d4fb5e 100644 --- a/src/daemon/string.h +++ b/src/daemon/string.h @@ -180,6 +180,34 @@ static inline void td_string_strip(char* s) { td_string_rstrip(s); } +// Continues to read line as long as it matches s +static inline int td_string_consume(char** line, const char* s) { + char* p = *line; + + for (;;) { + // Successful if we have consumed all of s + if (!*s) + break; + + // It is bad if we have run out of input + if (!*p) + return -EBADMSG; + + // The strings don't seem to match + if (*p != *s) + return -EBADMSG; + + // Advance both pointers + p++; + s++; + } + + // Restore the line + *line = p; + + return 0; +} + static inline void td_strings_free(char** array) { for (char** s = array; *s; s++) free(*s);