diff --git a/src/utils.c b/src/utils.c index 15960c7..c12e4bb 100644 --- a/src/utils.c +++ b/src/utils.c @@ -17,6 +17,7 @@ */ #include "utils.h" +#include "matrix-types.h" gchar * _g_enum_to_string(GType enum_type, gint value, gboolean convert_dashes) @@ -43,3 +44,36 @@ _g_enum_to_string(GType enum_type, gint value, gboolean convert_dashes) return nick; } + +gint +_g_enum_nick_to_value(GType enum_type, const gchar *nick, GError **error) +{ + GEnumClass *enum_class = g_type_class_ref(enum_type); + GEnumValue *enum_value = NULL; + gchar *nick_c = NULL; + gchar *a; + int ret = 0; + + nick_c = g_strdup(nick); + + for (a = nick_c; *a; a++) { + if (*a == '_') { + *a = '-'; + } + } + + enum_value = g_enum_get_value_by_nick(enum_class, nick_c); + g_free(nick_c); + + if (enum_value) { + ret = enum_value->value; + } else { + g_set_error(error, + MATRIX_ERROR, MATRIX_ERROR_UNKNOWN_VALUE, + "Value %s is unknown", nick); + } + + g_type_class_unref(enum_class); + + return ret; +} diff --git a/src/utils.h b/src/utils.h index 487034d..ab85814 100644 --- a/src/utils.h +++ b/src/utils.h @@ -23,5 +23,6 @@ #include gchar *_g_enum_to_string(GType enum_type, gint value, gboolean convert_dash); +gint _g_enum_nick_to_value(GType enum_type, const gchar *nick, GError **error); #endif /* __MATRIX_UTILS_H__ */