[Spice-devel] [spice-common v2] utils: Add genum -> string helpers
Christophe Fergeau
cfergeau at redhat.com
Fri Nov 23 09:49:02 UTC 2018
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 at redhat.com>
---
common/Makefile.am | 2 ++
common/meson.build | 2 ++
common/utils.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++
common/utils.h | 33 ++++++++++++++++++++++
4 files changed, 105 insertions(+)
create mode 100644 common/utils.c
create mode 100644 common/utils.h
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
--
2.19.1
More information about the Spice-devel
mailing list