utils: add _nm_utils_enum_from_str_full() to support aliases

This commit is contained in:
Thomas Haller
2017-03-29 18:56:01 +02:00
parent a8730c51c8
commit b9fa0e0a19
2 changed files with 52 additions and 4 deletions

View File

@@ -155,6 +155,28 @@ nm_utils_enum_to_str (GType type, int value)
gboolean gboolean
nm_utils_enum_from_str (GType type, const char *str, nm_utils_enum_from_str (GType type, const char *str,
int *out_value, char **err_token) 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; GTypeClass *class;
gboolean ret = FALSE; gboolean ret = FALSE;
@@ -162,6 +184,7 @@ nm_utils_enum_from_str (GType type, const char *str,
gs_free char *str_clone = NULL; gs_free char *str_clone = NULL;
char *s; char *s;
gint64 v64; gint64 v64;
const NMUtilsEnumValueInfo *nick;
g_return_val_if_fail (str, FALSE); g_return_val_if_fail (str, FALSE);
@@ -192,6 +215,12 @@ nm_utils_enum_from_str (GType type, const char *str,
if (enum_value) { if (enum_value) {
value = enum_value->value; value = enum_value->value;
ret = TRUE; 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; uvalue |= (unsigned) v64;
} else { } else {
flags_value = g_flags_get_value_by_nick (G_FLAGS_CLASS (class), s); flags_value = g_flags_get_value_by_nick (G_FLAGS_CLASS (class), s);
if (!flags_value) { if (flags_value)
ret = FALSE; uvalue |= flags_value->value;
break; else {
nick = _find_value_info (value_infos, s);
if (nick)
uvalue = (unsigned) nick->value;
else {
ret = FALSE;
break;
}
} }
uvalue |= flags_value->value;
} }
} }

View File

@@ -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); 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); 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); 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); const char **nm_utils_enum_get_values (GType type, gint from, gint to);