From 919f6b6d75eb395cacbd3e8501ddc8374a8343ad Mon Sep 17 00:00:00 2001 From: Beniamino Galvani Date: Wed, 4 Apr 2018 09:31:54 +0200 Subject: [PATCH] shared: use value infos in _nm_utils_enum_to_str_full --- libnm-core/nm-utils.c | 2 +- shared/nm-utils/nm-enum-utils.c | 47 +++++++++++++++++++++------ shared/nm-utils/nm-enum-utils.h | 5 ++- src/settings/plugins/ifcfg-rh/shvar.c | 2 +- 4 files changed, 43 insertions(+), 13 deletions(-) diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index 050d0c5da5..4e38e35718 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -4805,7 +4805,7 @@ gssize _nm_utils_dns_option_find_idx (GPtrArray *array, const char *option) char * nm_utils_enum_to_str (GType type, int value) { - return _nm_utils_enum_to_str_full (type, value, ", "); + return _nm_utils_enum_to_str_full (type, value, ", ", NULL); } /** diff --git a/shared/nm-utils/nm-enum-utils.c b/shared/nm-utils/nm-enum-utils.c index 70a8b415dd..b9bc6e88fa 100644 --- a/shared/nm-utils/nm-enum-utils.c +++ b/shared/nm-utils/nm-enum-utils.c @@ -64,10 +64,10 @@ _enum_is_valid_flags_nick (const char *str) char * _nm_utils_enum_to_str_full (GType type, int value, - const char *flags_separator) + const char *flags_separator, + const NMUtilsEnumValueInfo *value_infos) { - GTypeClass *class; - char *ret; + nm_auto_unref_gtypeclass GTypeClass *class = NULL; if ( flags_separator && ( !flags_separator[0] @@ -79,12 +79,17 @@ _nm_utils_enum_to_str_full (GType type, if (G_IS_ENUM_CLASS (class)) { GEnumValue *enum_value; + for ( ; value_infos && value_infos->nick; value_infos++) { + if (value_infos->value == value) + return g_strdup (value_infos->nick); + } + enum_value = g_enum_get_value (G_ENUM_CLASS (class), value); if ( !enum_value || !_enum_is_valid_enum_nick (enum_value->value_nick)) - ret = g_strdup_printf ("%d", value); + return g_strdup_printf ("%d", value); else - ret = strdup (enum_value->value_nick); + return g_strdup (enum_value->value_nick); } else if (G_IS_FLAGS_CLASS (class)) { GFlagsValue *flags_value; GString *str = g_string_new (""); @@ -92,6 +97,28 @@ _nm_utils_enum_to_str_full (GType type, flags_separator = flags_separator ?: " "; + for ( ; value_infos && value_infos->nick; value_infos++) { + + nm_assert (_enum_is_valid_flags_nick (value_infos->nick)); + + if (uvalue == 0) { + if (value_infos->value != 0) + continue; + } else { + if (!NM_FLAGS_ALL (uvalue, (unsigned) value_infos->value)) + continue; + } + + if (str->len) + g_string_append (str, flags_separator); + g_string_append (str, value_infos->nick); + uvalue &= ~((unsigned) value_infos->value); + if (uvalue == 0) { + /* we printed all flags. Done. */ + goto flags_done; + } + } + do { flags_value = g_flags_get_first_value (G_FLAGS_CLASS (class), uvalue); if (str->len) @@ -105,12 +132,12 @@ _nm_utils_enum_to_str_full (GType type, g_string_append (str, flags_value->value_nick); uvalue &= ~flags_value->value; } while (uvalue); - ret = g_string_free (str, FALSE); - } else - g_return_val_if_reached (NULL); - g_type_class_unref (class); - return ret; +flags_done: + return g_string_free (str, FALSE); + } + + g_return_val_if_reached (NULL); } static const NMUtilsEnumValueInfo * diff --git a/shared/nm-utils/nm-enum-utils.h b/shared/nm-utils/nm-enum-utils.h index b78d919176..d6dae859ad 100644 --- a/shared/nm-utils/nm-enum-utils.h +++ b/shared/nm-utils/nm-enum-utils.h @@ -31,7 +31,10 @@ typedef struct _NMUtilsEnumValueInfo { int value; } NMUtilsEnumValueInfo; -char *_nm_utils_enum_to_str_full (GType type, int value, const char *sep); +char *_nm_utils_enum_to_str_full (GType type, + int value, + const char *sep, + const NMUtilsEnumValueInfo *value_infos); gboolean _nm_utils_enum_from_str_full (GType type, const char *str, int *out_value, diff --git a/src/settings/plugins/ifcfg-rh/shvar.c b/src/settings/plugins/ifcfg-rh/shvar.c index 2b64f3fc7f..c0dad6ab27 100644 --- a/src/settings/plugins/ifcfg-rh/shvar.c +++ b/src/settings/plugins/ifcfg-rh/shvar.c @@ -1278,7 +1278,7 @@ svSetValueEnum (shvarFile *s, const char *key, GType gtype, int value) { gs_free char *v = NULL; - v = _nm_utils_enum_to_str_full (gtype, value, " "); + v = _nm_utils_enum_to_str_full (gtype, value, " ", NULL); return svSetValueStr (s, key, v); }