]> git.ipfire.org Git - collecty.git/commitdiff
string: Add our string comparison function
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Oct 2025 14:08:53 +0000 (14:08 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Oct 2025 14:08:53 +0000 (14:08 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/graph-bus.c
src/daemon/graphs.c
src/daemon/proc.c
src/daemon/proto.c
src/daemon/queue.c
src/daemon/sources.c
src/daemon/string.h

index 3029a1ca33815fef33768a1b51a5130fd577a1d9..ea89fd5580c91fb3c718497a576210256504c075 100644 (file)
@@ -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;
index d2dda708be18da75a4bd698ec331fbbb5ba6ec9a..4e59d034bbf319ba1de265694891709e2b98afc3 100644 (file)
@@ -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]);
        }
 
index 97fa1da7e87e15a97d1850a52a82dc640caf431f..972709f6b5c391700e9289cb552aac666cceb6bc 100644 (file)
@@ -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
index d655e73c4fa1c20f9ed4626a8daa69f33778e052..71b43c7232fd925ff326fdf63db4f3c0e02635d0 100644 (file)
@@ -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
index 4144b40f4e4f5c11a6155a52a5910510846947bc..ca8ebda7996fc6f3d94e280fc3bf84135b3ea9ae 100644 (file)
@@ -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;
        }
 
index 764bb13a3cc1a9c4275fde08fc230db30a98fd6c..51b0a99c344c11d708e5e4ecc337ea2f04db1bb6 100644 (file)
@@ -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]);
        }
 
index 2d05fa4a359f25ff5ed82492db81c227df179ae6..ea52538857f7f3fbd3be7d1cebf2e0c24b07d7ad 100644 (file)
 #include <ctype.h>
 #include <string.h>
 
+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;