From b9fa0e0a195e9cf7363b563cca7ffec4b5016afd Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 29 Mar 2017 18:56:01 +0200 Subject: [PATCH] utils: add _nm_utils_enum_from_str_full() to support aliases --- shared/nm-utils/nm-enum-utils.c | 43 ++++++++++++++++++++++++++++++--- shared/nm-utils/nm-enum-utils.h | 13 ++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/shared/nm-utils/nm-enum-utils.c b/shared/nm-utils/nm-enum-utils.c index 5b62a2691..3eda9eeed 100644 --- a/shared/nm-utils/nm-enum-utils.c +++ b/shared/nm-utils/nm-enum-utils.c @@ -155,6 +155,28 @@ nm_utils_enum_to_str (GType type, int value) gboolean nm_utils_enum_from_str (GType type, const char *str, int *out_value, char **err_token) +{ + return _nm_utils_enum_from_str_full (type, str, out_value, err_token, NULL); +} + +static const NMUtilsEnumValueInfo * +_find_value_info (const NMUtilsEnumValueInfo *value_infos, const char *needle) +{ + if (value_infos) { + for (; value_infos->nick; value_infos++) { + if (nm_streq (needle, value_infos->nick)) + return value_infos; + } + } + return NULL; +} + +gboolean +_nm_utils_enum_from_str_full (GType type, + const char *str, + int *out_value, + char **err_token, + const NMUtilsEnumValueInfo *value_infos) { GTypeClass *class; gboolean ret = FALSE; @@ -162,6 +184,7 @@ nm_utils_enum_from_str (GType type, const char *str, gs_free char *str_clone = NULL; char *s; gint64 v64; + const NMUtilsEnumValueInfo *nick; g_return_val_if_fail (str, FALSE); @@ -192,6 +215,12 @@ nm_utils_enum_from_str (GType type, const char *str, if (enum_value) { value = enum_value->value; ret = TRUE; + } else { + nick = _find_value_info (value_infos, s); + if (nick) { + value = nick->value; + ret = TRUE; + } } } } @@ -228,11 +257,17 @@ nm_utils_enum_from_str (GType type, const char *str, uvalue |= (unsigned) v64; } else { flags_value = g_flags_get_value_by_nick (G_FLAGS_CLASS (class), s); - if (!flags_value) { - ret = FALSE; - break; + if (flags_value) + uvalue |= flags_value->value; + else { + nick = _find_value_info (value_infos, s); + if (nick) + uvalue = (unsigned) nick->value; + else { + ret = FALSE; + break; + } } - uvalue |= flags_value->value; } } diff --git a/shared/nm-utils/nm-enum-utils.h b/shared/nm-utils/nm-enum-utils.h index 6f8d4924e..047fbc72b 100644 --- a/shared/nm-utils/nm-enum-utils.h +++ b/shared/nm-utils/nm-enum-utils.h @@ -24,7 +24,20 @@ /*****************************************************************************/ +typedef struct _NMUtilsEnumValueInfo { + /* currently, this is only used for _nm_utils_enum_from_str_full() to + * declare additional aliases for values. */ + const char *nick; + int value; +} NMUtilsEnumValueInfo; + char *_nm_utils_enum_to_str_full (GType type, int value, const char *sep); +gboolean _nm_utils_enum_from_str_full (GType type, + const char *str, + int *out_value, + char **err_token, + const NMUtilsEnumValueInfo *value_infos); + char *nm_utils_enum_to_str (GType type, int value); gboolean nm_utils_enum_from_str (GType type, const char *str, int *out_value, char **err_token); const char **nm_utils_enum_get_values (GType type, gint from, gint to);