diff options
author | Thomas Haller <thaller@redhat.com> | 2017-03-29 18:56:01 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2017-03-30 13:09:58 +0200 |
commit | b9fa0e0a195e9cf7363b563cca7ffec4b5016afd (patch) | |
tree | f7d76f702a5af1190c186456578ff91150917ad2 | |
parent | a8730c51c8915d7cd4209c0420734bf446b902d3 (diff) | |
download | NetworkManager-b9fa0e0a195e9cf7363b563cca7ffec4b5016afd.tar.gz |
utils: add _nm_utils_enum_from_str_full() to support aliases
-rw-r--r-- | shared/nm-utils/nm-enum-utils.c | 43 | ||||
-rw-r--r-- | 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 5b62a26910..3eda9eeed3 100644 --- a/shared/nm-utils/nm-enum-utils.c +++ b/shared/nm-utils/nm-enum-utils.c @@ -156,12 +156,35 @@ 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; int value = 0; 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 6f8d4924ea..047fbc72b0 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); |