From: Hans de Goede Date: Wed, 9 Jan 2019 10:31:06 +0000 (+0100) Subject: libply: Add ply_key_file_get_bool function X-Git-Tag: 0.9.5~78^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e111ba8bafaa41fced0824e6fbccc20372917a3d;p=thirdparty%2Fplymouth.git libply: Add ply_key_file_get_bool function 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 --- diff --git a/src/libply/ply-key-file.c b/src/libply/ply-key-file.c index 862d6d59..1c05766a 100644 --- a/src/libply/ply-key-file.c +++ b/src/libply/ply-key-file.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -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, diff --git a/src/libply/ply-key-file.h b/src/libply/ply-key-file.h index e69d14a8..ef7124b5 100644 --- a/src/libply/ply-key-file.h +++ b/src/libply/ply-key-file.h @@ -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);