diff options
Diffstat (limited to 'libnm-core/nm-keyfile-utils.c')
| -rw-r--r-- | libnm-core/nm-keyfile-utils.c | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/libnm-core/nm-keyfile-utils.c b/libnm-core/nm-keyfile-utils.c index e243150d..522d5b71 100644 --- a/libnm-core/nm-keyfile-utils.c +++ b/libnm-core/nm-keyfile-utils.c @@ -1,4 +1,3 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ /* NetworkManager system settings service * * This program is free software; you can redistribute it and/or modify @@ -29,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; @@ -415,7 +451,7 @@ _keyfile_key_encode (const char *name, /* See g_key_file_is_key_name(). * - * GKeyfile allows all UTF-8 characters (even non-well formed sequences), + * GKeyFile allows all UTF-8 characters (even non-well formed sequences), * except: * - no empty keys * - no leading/trailing ' ' |