]> git.ipfire.org Git - telemetry.git/commitdiff
string: Add a couple more pattern matching functions
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 27 Mar 2026 16:42:46 +0000 (16:42 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 27 Mar 2026 16:42:46 +0000 (16:42 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/string.h

index 1d4fb5e29333187e0fafccbc716b6d2cd2d67575..2caae98a7ed43c36641185868feec4301408e87e 100644 (file)
@@ -23,6 +23,7 @@
 
 #include <ctype.h>
 #include <errno.h>
+#include <fnmatch.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -208,6 +209,23 @@ static inline int td_string_consume(char** line, const char* s) {
        return 0;
 }
 
+static inline int td_string_matches(const char* s, const char* p) {
+       return fnmatch(p, s, 0) == 0;
+}
+
+static inline int td_string_matches_any(const char* s, const char** patterns) {
+       int r;
+
+       // Match the string against all patterns
+       for (const char** p = patterns; *p; p++) {
+               r = td_string_matches(s, *p);
+               if (r)
+                       return r;
+       }
+
+       return 0;
+}
+
 static inline void td_strings_free(char** array) {
        for (char** s = array; *s; s++)
                free(*s);
@@ -303,4 +321,17 @@ static inline int td_strings_contain(char** array, const char* string) {
        return 0;
 }
 
+static inline int td_strings_match(char** array, const char* pattern) {
+       int r;
+
+       // Check if any of the strings matches
+       for (char**s = array; *s; s++) {
+               r = td_string_matches(*s, pattern);
+               if (r)
+                       return r;
+       }
+
+       return 0;
+}
+
 #endif /* TELEMETRY_STRING_H */