]> git.ipfire.org Git - telemetry.git/commitdiff
util: Add a helper function to walk through a buffer
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 6 Oct 2025 16:15:31 +0000 (16:15 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 6 Oct 2025 16:15:31 +0000 (16:15 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/util.c
src/daemon/util.h

index bc9dc031b44d134b533bfaa14b29a94bc1ea740f..fcdda7b9ff6a7c477a89997ebc038a50d03d296d 100644 (file)
@@ -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;
+}
index c50d507c9a0d327f4b99a1441e359bc9b7d1615b..e9ffa9134e32c5de602e88a7952e71ae10152403 100644 (file)
@@ -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 */