Create utility function _g_enum_nick_to_value()

This commit is contained in:
Gergely Polonkai 2016-01-24 08:42:01 +01:00
parent abfbd7e53b
commit 94529710b1
2 changed files with 35 additions and 0 deletions

View File

@ -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;
}

View File

@ -23,5 +23,6 @@
#include <glib-object.h>
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__ */