From: Michael Tremer Date: Mon, 6 Oct 2025 16:15:31 +0000 (+0000) Subject: util: Add a helper function to walk through a buffer X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fbe9f29ab2c766ed16bf50f8bd2ba9a9de966717;p=telemetry.git util: Add a helper function to walk through a buffer Signed-off-by: Michael Tremer --- diff --git a/src/daemon/util.c b/src/daemon/util.c index bc9dc03..fcdda7b 100644 --- a/src/daemon/util.c +++ b/src/daemon/util.c @@ -121,3 +121,25 @@ int collecty_fwalk(FILE* f, collecty_fwalk_callback callback, void* data) { return r; } + +int collecty_fwalk_buffer(const char* buffer, const size_t length, + collecty_fwalk_callback callback, void* data) { + FILE* f = NULL; + int r; + + // Open as a file handle + f = fmemopen((char*)buffer, length, "r"); + if (!f) { + r = -errno; + goto ERROR; + } + + // Walk through all lines + r = collecty_fwalk(f, callback, data); + +ERROR: + if (f) + fclose(f); + + return r; +} diff --git a/src/daemon/util.h b/src/daemon/util.h index c50d507..e9ffa91 100644 --- a/src/daemon/util.h +++ b/src/daemon/util.h @@ -56,4 +56,7 @@ typedef int (*collecty_fwalk_callback)(const char* line, const size_t length, vo int collecty_fwalk(FILE* f, collecty_fwalk_callback callback, void* data); +int collecty_fwalk_buffer(const char* buffer, const size_t length, + collecty_fwalk_callback callback, void* data); + #endif /* COLLECTY_UTIL_H */