From: Michael Tremer Date: Sun, 5 Oct 2025 14:08:53 +0000 (+0000) Subject: string: Add our string comparison function X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ad8fcbc041e9b1bfa70c12d947eb710fce5694ba;p=collecty.git string: Add our string comparison function Signed-off-by: Michael Tremer --- diff --git a/src/daemon/graph-bus.c b/src/daemon/graph-bus.c index 3029a1c..ea89fd5 100644 --- a/src/daemon/graph-bus.c +++ b/src/daemon/graph-bus.c @@ -24,6 +24,7 @@ #include "graph.h" #include "graph-bus.h" +#include "string.h" static int collecty_graph_node_enumerator(sd_bus* bus, const char* path, void* data, char*** nodes, sd_bus_error* error) { @@ -134,19 +135,19 @@ static int collecty_graph_bus_render(sd_bus_message* m, void* data, sd_bus_error goto ERROR; // Parse "format" - if (strcmp(key, "format") == 0) { + if (collecty_string_equals(key, "format")) { r = sd_bus_message_read(m, "v", "s", &options.format); if (r < 0) goto ERROR; // Parse "height" - } else if (strcmp(key, "height") == 0) { + } else if (collecty_string_equals(key, "height")) { r = sd_bus_message_read(m, "v", "t", &options.dimensions.h); if (r < 0) goto ERROR; // Parse "width" - } else if (strcmp(key, "width") == 0) { + } else if (collecty_string_equals(key, "width")) { r = sd_bus_message_read(m, "v", "t", &options.dimensions.w); if (r < 0) goto ERROR; diff --git a/src/daemon/graphs.c b/src/daemon/graphs.c index d2dda70..4e59d03 100644 --- a/src/daemon/graphs.c +++ b/src/daemon/graphs.c @@ -25,6 +25,7 @@ #include "daemon.h" #include "graph.h" #include "graphs.h" +#include "string.h" // Load all graphs #include "graphs/conntrack.h" @@ -163,7 +164,7 @@ collecty_graph* collecty_graphs_get_by_name(collecty_graphs* self, const char* n continue; // Return the object if the name matches - if (strcmp(name, n) == 0) + if (collecty_string_equals(name, n)) return collecty_graph_ref(self->graphs[i]); } diff --git a/src/daemon/proc.c b/src/daemon/proc.c index 97fa1da..972709f 100644 --- a/src/daemon/proc.c +++ b/src/daemon/proc.c @@ -53,39 +53,39 @@ int collecty_proc_read_meminfo(collecty_ctx* ctx, collecty_proc_meminfo* meminfo v *= 1024; // Store the values - if (strcmp("MemTotal:", k) == 0) + if (collecty_string_equals("MemTotal:", k)) meminfo->mem_total = v; - else if (strcmp("MemFree:", k) == 0) + else if (collecty_string_equals("MemFree:", k)) meminfo->mem_free = v; - else if (strcmp("MemAvailable:", k) == 0) + else if (collecty_string_equals("MemAvailable:", k)) meminfo->mem_available = v; - else if (strcmp("Cached:", k) == 0) + else if (collecty_string_equals("Cached:", k)) meminfo->cached = v; - else if (strcmp("Buffers:", k) == 0) + else if (collecty_string_equals("Buffers:", k)) meminfo->buffers = v; - else if (strcmp("Active:", k) == 0) + else if (collecty_string_equals("Active:", k)) meminfo->active = v; - else if (strcmp("Inactive:", k) == 0) + else if (collecty_string_equals("Inactive:", k)) meminfo->inactive = v; - else if (strcmp("Active(anon):", k) == 0) + else if (collecty_string_equals("Active(anon):", k)) meminfo->active_anon = v; - else if (strcmp("Inactive(anon):", k) == 0) + else if (collecty_string_equals("Inactive(anon):", k)) meminfo->inactive_anon = v; - else if (strcmp("Active(file):", k) == 0) + else if (collecty_string_equals("Active(file):", k)) meminfo->active_file = v; - else if (strcmp("Inactive(file):", k) == 0) + else if (collecty_string_equals("Inactive(file):", k)) meminfo->inactive_file = v; - else if (strcmp("SwapTotal:", k) == 0) + else if (collecty_string_equals("SwapTotal:", k)) meminfo->swap_total = v; - else if (strcmp("SwapFree:", k) == 0) + else if (collecty_string_equals("SwapFree:", k)) meminfo->swap_free = v; - else if (strcmp("Shmem:", k) == 0) + else if (collecty_string_equals("Shmem:", k)) meminfo->shmem = v; - else if (strcmp("Slab:", k) == 0) + else if (collecty_string_equals("Slab:", k)) meminfo->slab = v; - else if (strcmp("SReclaimable:", k) == 0) + else if (collecty_string_equals("SReclaimable:", k)) meminfo->sreclaimable = v; - else if (strcmp("SUnreclaim:", k) == 0) + else if (collecty_string_equals("SUnreclaim:", k)) meminfo->sunreclaim = v; } @@ -115,7 +115,7 @@ static int collecty_proc_read_stat_line(collecty_ctx* ctx, t = strtok_r(line, " ", &p); // If the first token does not match the tag, we skip the line - if (strcmp(tag, t) != 0) + if (!collecty_string_equals(tag, t)) return 0; // Read all tokens diff --git a/src/daemon/proto.c b/src/daemon/proto.c index d655e73..71b43c7 100644 --- a/src/daemon/proto.c +++ b/src/daemon/proto.c @@ -26,6 +26,7 @@ #include "ctx.h" #include "proto.h" +#include "string.h" typedef struct collecty_proto_object { STAILQ_ENTRY(collecty_proto_object) nodes; @@ -275,11 +276,11 @@ int collecty_proto_get(collecty_proto* self, // Walk through all objects STAILQ_FOREACH(o, &self->objects, nodes) { // Protocol must match - if (strcmp(o->proto, proto) != 0) + if (!collecty_string_equals(o->proto, proto)) continue; // Key must match - if (strcmp(o->key, key) != 0) + if (!collecty_string_equals(o->key, key)) continue; // Return the value diff --git a/src/daemon/queue.c b/src/daemon/queue.c index 4144b40..ca8ebda 100644 --- a/src/daemon/queue.c +++ b/src/daemon/queue.c @@ -28,6 +28,7 @@ #include "ctx.h" #include "daemon.h" #include "queue.h" +#include "string.h" #include "time.h" #define HEARTBEAT SEC_TO_USEC(300) // 5 minutes @@ -205,7 +206,7 @@ static struct collecty_queue_object* collecty_queue_find_object( // If both have objects, we need to compare else if (o->object && object) - if (strcmp(o->object, object) == 0) + if (collecty_string_equals(o->object, object)) return o; } diff --git a/src/daemon/sources.c b/src/daemon/sources.c index 764bb13..51b0a99 100644 --- a/src/daemon/sources.c +++ b/src/daemon/sources.c @@ -26,6 +26,7 @@ #include "daemon.h" #include "source.h" #include "sources.h" +#include "string.h" // Load all sources #include "sources/conntrack.h" @@ -188,7 +189,7 @@ collecty_source* collecty_sources_get_by_name(collecty_sources* self, const char continue; // Return the object if the name matches - if (strcmp(name, n) == 0) + if (collecty_string_equals(name, n)) return collecty_source_ref(self->sources[i]); } diff --git a/src/daemon/string.h b/src/daemon/string.h index 2d05fa4..ea52538 100644 --- a/src/daemon/string.h +++ b/src/daemon/string.h @@ -24,6 +24,10 @@ #include #include +inline int collecty_string_equals(const char* s1, const char* s2) { + return strcmp(s1, s2) == 0; +} + inline void collecty_string_lstrip(char* s) { if (!s) return;