summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristophe Fergeau <cfergeau@redhat.com>2018-11-23 10:49:02 +0100
committerFrediano Ziglio <fziglio@redhat.com>2018-11-23 10:25:01 +0000
commit6077523a5f020c30983ef27ba3d456ff52544ae0 (patch)
tree1d406f001b52cd0d771a1cd2d025d98d696ab742
parentaee2b91fe278fa0bdd6e8274119e37efae8323fd (diff)
utils: Add genum -> string helpers
This makes it easier to convert enums registered with glib type system to string in order to print them at runtime. Signed-off-by: Christophe Fergeau <cfergeau@redhat.com> Acked-by: Frediano Ziglio <fziglio@redhat.com>
-rw-r--r--common/Makefile.am2
-rw-r--r--common/meson.build2
-rw-r--r--common/utils.c68
-rw-r--r--common/utils.h33
4 files changed, 105 insertions, 0 deletions
diff --git a/common/Makefile.am b/common/Makefile.am
index da0d941..197e419 100644
--- a/common/Makefile.am
+++ b/common/Makefile.am
@@ -50,6 +50,8 @@ libspice_common_la_SOURCES = \
rop3.h \
snd_codec.c \
snd_codec.h \
+ utils.c \
+ utils.h \
verify.h \
$(NULL)
diff --git a/common/meson.build b/common/meson.build
index 6ac55dc..8f22310 100644
--- a/common/meson.build
+++ b/common/meson.build
@@ -35,6 +35,8 @@ spice_common_sources = [
'rop3.h',
'snd_codec.c',
'snd_codec.h',
+ 'utils.c',
+ 'utils.h',
'verify.h'
]
diff --git a/common/utils.c b/common/utils.c
new file mode 100644
index 0000000..460098c
--- /dev/null
+++ b/common/utils.c
@@ -0,0 +1,68 @@
+/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
+/*
+ Copyright (C) 2010, 2011, 2018 Red Hat, Inc.
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "utils.h"
+
+/* These 2 functions come from
+ * libvirt-glib/libvirt-gconfig/libvirt-gconfig-helpers.c
+ * Copyright (C) 2010, 2011 Red Hat, Inc.
+ * LGPLv2.1+ licensed */
+
+const char *
+spice_genum_get_nick(GType enum_type, gint value)
+{
+ GEnumClass *enum_class;
+ GEnumValue *enum_value;
+
+ g_return_val_if_fail(G_TYPE_IS_ENUM(enum_type), NULL);
+
+ enum_class = g_type_class_ref(enum_type);
+ enum_value = g_enum_get_value(enum_class, value);
+ g_type_class_unref(enum_class);
+
+ if (enum_value != NULL) {
+ return enum_value->value_nick;
+ }
+
+ g_return_val_if_reached(NULL);
+}
+
+int
+spice_genum_get_value(GType enum_type, const char *nick,
+ gint default_value)
+{
+ GEnumClass *enum_class;
+ GEnumValue *enum_value;
+
+ g_return_val_if_fail(G_TYPE_IS_ENUM(enum_type), default_value);
+ g_return_val_if_fail(nick != NULL, default_value);
+
+ enum_class = g_type_class_ref(enum_type);
+ enum_value = g_enum_get_value_by_nick(enum_class, nick);
+ g_type_class_unref(enum_class);
+
+ if (enum_value != NULL) {
+ return enum_value->value;
+ }
+
+ g_return_val_if_reached(default_value);
+}
diff --git a/common/utils.h b/common/utils.h
new file mode 100644
index 0000000..e853209
--- /dev/null
+++ b/common/utils.h
@@ -0,0 +1,33 @@
+/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
+/*
+ Copyright (C) 2010, 2011, 2018 Red Hat, Inc.
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef H_SPICE_COMMON_UTILS
+#define H_SPICE_COMMON_UTILS
+
+#include <glib.h>
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+const char *spice_genum_get_nick(GType enum_type, gint value);
+int spice_genum_get_value(GType enum_type, const char *nick,
+ gint default_value);
+
+G_END_DECLS
+
+#endif //H_SPICE_COMMON_UTILS