libnm: add nm_key_file_get_boolean() helper

This commit is contained in:
Thomas Haller 2019-07-11 12:23:31 +02:00
parent d5ad315f11
commit 355390fad4
2 changed files with 39 additions and 0 deletions

View file

@ -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);

View file

@ -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;