]> git.ipfire.org Git - network.git/commitdiff
networkd: config: Add functions to handle boolean values
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 14 Apr 2023 13:04:17 +0000 (13:04 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 14 Apr 2023 13:04:17 +0000 (13:04 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/config.c
src/networkd/config.h

index b3d52845eb491d56407fe54788dbd974d37f5f8d..d879ace761d86f54470cd4caefc4f1afe26b70cb 100644 (file)
@@ -368,3 +368,31 @@ int nw_config_set_int(nw_config* config, const char* key, const int value) {
 
        return nw_config_set(config, key, __value);
 }
+
+static const char* nw_config_true[] = {
+       "true",
+       "yes",
+       "1",
+       NULL,
+};
+
+int nw_config_get_bool(nw_config* config, const char* key) {
+       const char* value = nw_config_get(config, key);
+
+       // No value indicates false
+       if (!value)
+               return 0;
+
+       // Check if we match any known true words
+       for (const char** s = nw_config_true; *s; s++) {
+               if (strcmp(value, *s) == 0)
+                       return 1;
+       }
+
+       // No match means false
+       return 0;
+}
+
+int nw_config_set_bool(nw_config* config, const char* key, const int value) {
+       return nw_config_set(config, key, value ? "true" : "false");
+}
index 041a10e8840c0fbe3577a13fb8ad22a722d49d93..0b25f75467206b2395b598ba8ad2007f281c60ff 100644 (file)
@@ -48,4 +48,7 @@ int nw_config_set(nw_config* config, const char* key, const char* value);
 int nw_config_get_int(nw_config* config, const char* key, const int __default);
 int nw_config_set_int(nw_config* config, const char* key, const int value);
 
+int nw_config_get_bool(nw_config* config, const char* key);
+int nw_config_set_bool(nw_config* config, const char* key, const int value);
+
 #endif /* NETWORKD_CONFIG_H */