]> git.ipfire.org Git - thirdparty/plymouth.git/commitdiff
libply: Add ply_key_file_get_bool function
authorHans de Goede <hdegoede@redhat.com>
Wed, 9 Jan 2019 10:31:06 +0000 (11:31 +0100)
committerHans de Goede <hdegoede@redhat.com>
Wed, 9 Jan 2019 10:42:34 +0000 (11:42 +0100)
Add a function to read a boolean value from a ply-key-file.

This function will return true if the key exists and it has a value of "1",
"y", "yes" or "true" (case-insensitive). In all other cases it returns
false.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
src/libply/ply-key-file.c
src/libply/ply-key-file.h

index 862d6d59de7a9abdb1e8aab06b3a5fcf878cdce7..1c05766aae1a6e22d35ee755a0ec81bca957307c 100644 (file)
@@ -29,6 +29,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <string.h>
+#include <strings.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -356,6 +357,38 @@ ply_key_file_get_value (ply_key_file_t *key_file,
         return strdup (entry->value);
 }
 
+bool
+ply_key_file_get_bool (ply_key_file_t *key_file,
+                       const char     *group_name,
+                       const char     *key)
+{
+        ply_key_file_group_t *group;
+        ply_key_file_entry_t *entry;
+
+        group = ply_key_file_find_group (key_file, group_name);
+
+        if (group == NULL) {
+                ply_trace ("key file does not have group '%s'", group_name);
+                return false;
+        }
+
+        entry = ply_key_file_find_entry (key_file, group, key);
+
+        if (entry == NULL) {
+                ply_trace ("key file does not have entry for key '%s'", key);
+                return false;
+        }
+
+        /* We treat "1", "y" and "yes" and "true" as true, all else is false */
+        if (strcasecmp (entry->value, "1")    == 0 ||
+            strcasecmp (entry->value, "y")    == 0 ||
+            strcasecmp (entry->value, "yes")  == 0 ||
+            strcasecmp (entry->value, "true") == 0)
+                return true;
+
+        return false;
+}
+
 static void
 ply_key_file_foreach_entry_entries (void *key,
                                     void *data,
index e69d14a85d7b7fe136a4c7301e44a7aeda48f0f5..ef7124b586e347ad001a7d2e168ca8fc4a720478 100644 (file)
@@ -42,6 +42,10 @@ bool ply_key_file_has_key (ply_key_file_t *key_file,
 char *ply_key_file_get_value (ply_key_file_t *key_file,
                               const char     *group_name,
                               const char     *key);
+/* Note this returns false for non existing keys */
+bool ply_key_file_get_bool (ply_key_file_t *key_file,
+                            const char     *group_name,
+                            const char     *key);
 void ply_key_file_foreach_entry (ply_key_file_t             *key_file,
                                  ply_key_file_foreach_func_t func,
                                  void                       *user_data);