From 355390fad46ab69ca6a632739aba6d1c5a618237 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 11 Jul 2019 12:23:31 +0200 Subject: [PATCH] libnm: add nm_key_file_get_boolean() helper --- libnm-core/nm-keyfile-internal.h | 2 ++ libnm-core/nm-keyfile-utils.c | 37 ++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/libnm-core/nm-keyfile-internal.h b/libnm-core/nm-keyfile-internal.h index a73f85bbd1..941fe0a18b 100644 --- a/libnm-core/nm-keyfile-internal.h +++ b/libnm-core/nm-keyfile-internal.h @@ -162,6 +162,8 @@ GKeyFile *nm_keyfile_write (NMConnection *connection, char *nm_keyfile_plugin_kf_get_string (GKeyFile *kf, const char *group, const char *key, GError **error); void nm_keyfile_plugin_kf_set_string (GKeyFile *kf, const char *group, const char *key, const char *value); +int nm_key_file_get_boolean (GKeyFile *kf, const char *group, const char *key, int default_value); + void _nm_keyfile_copy (GKeyFile *dst, GKeyFile *src); gboolean _nm_keyfile_a_contains_all_in_b (GKeyFile *kf_a, GKeyFile *kf_b); gboolean _nm_keyfile_equals (GKeyFile *kf_a, GKeyFile *kf_b, gboolean consider_order); diff --git a/libnm-core/nm-keyfile-utils.c b/libnm-core/nm-keyfile-utils.c index 71e6ed9ca0..522d5b71fa 100644 --- a/libnm-core/nm-keyfile-utils.c +++ b/libnm-core/nm-keyfile-utils.c @@ -28,6 +28,43 @@ #include "nm-setting-wireless.h" #include "nm-setting-wireless-security.h" +/*****************************************************************************/ + +/** + * nm_key_file_get_boolean: + * @kf: the #GKeyFile + * @group: the group + * @key: the key + * @default_value: the default value if the value is set or not parsable as a boolean. + * + * Replacement for g_key_file_get_boolean() (which uses g_key_file_parse_value_as_boolean()). + * g_key_file_get_boolean() seems odd to me, because it accepts trailing ASCII whitespace, + * but not leading. + * This uses _nm_utils_ascii_str_to_bool(), which accepts trailing and leading whitespace, + * case-insensitive words, and also strings like "on" and "off". + * _nm_utils_ascii_str_to_bool() is our way to parse booleans from string, and we should + * use that one consistently. + * + * Also, it doesn't have g_key_file_get_boolean()'s odd API to require an error argument + * to detect parsing failures. + * + * Returns: either %TRUE or %FALSE if the key exists and is parsable as a boolean. + * Otherwise, @default_value. + */ +int +nm_key_file_get_boolean (GKeyFile *kf, const char *group, const char *key, int default_value) +{ + gs_free char *value = NULL; + + value = g_key_file_get_value (kf, group, key, NULL); + + if (!value) + return default_value; + return _nm_utils_ascii_str_to_bool (value, default_value); +} + +/*****************************************************************************/ + typedef struct { const char *setting; const char *alias;