]> git.ipfire.org Git - oddments/collecty.git/commitdiff
string: Move function that consumes a string if it matches
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 29 Oct 2025 15:42:55 +0000 (15:42 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 29 Oct 2025 15:42:55 +0000 (15:42 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/source.c
src/daemon/string.h

index a512f6df9ed1e1ad7008f6b3ccfc1726d0340212..3a5f6a93e7d02a1fbd6d57e5a28a3f36f5bdda37 100644 (file)
@@ -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;
 
index c3b41fb3df687b2030ec2f3cd751fadf12a44a84..1d4fb5e29333187e0fafccbc716b6d2cd2d67575 100644 (file)
@@ -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);