The functions were copied from src/util/virutil.* files from
libvirt project
Signed-off-by: Katerina Koukiou <kkoukiou(a)redhat.com>
---
src/util.c | 27 +++++++++++++++++++++++++++
src/util.h | 28 ++++++++++++++++++++++++++++
2 files changed, 55 insertions(+)
diff --git a/src/util.c b/src/util.c
index d6c27f3..3179dd7 100644
--- a/src/util.c
+++ b/src/util.c
@@ -124,3 +124,30 @@ virtDBusUtilVirDomainListFree(virDomainPtr *domains)
g_free(domains);
}
+
+const gchar *
+virtDBusUtilEnumToString(const gchar *const*types,
+ guint ntypes,
+ gint type)
+{
+ if (type < 0 || (unsigned)type >= ntypes)
+ return NULL;
+
+ return types[type];
+}
+
+gint
+virtDBusUtilEnumFromString(const gchar *const*types,
+ guint ntypes,
+ const gchar *type)
+{
+ guint i;
+ if (!type)
+ return -1;
+
+ for (i = 0; i < ntypes; i++)
+ if (strcmp(types[i], type) == 0)
+ return i;
+
+ return -1;
+}
diff --git a/src/util.h b/src/util.h
index 4304bac..61e01c9 100644
--- a/src/util.h
+++ b/src/util.h
@@ -37,3 +37,31 @@ virtDBusUtilVirDomainListFree(virDomainPtr *domains);
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virDomain, virDomainFree);
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virDomainPtr, virtDBusUtilVirDomainListFree);
+
+gint
+virtDBusUtilEnumFromString(const gchar *const*types,
+ guint ntypes,
+ const gchar *type) G_GNUC_PURE;
+
+const gchar *
+virtDBusUtilEnumToString(const gchar *const*types,
+ guint ntypes,
+ gint type) G_GNUC_PURE;
+
+# define VIRT_DBUS_ENUM_IMPL(name, lastVal, ...) \
+ static const gchar *const name ##TypeList[] = { __VA_ARGS__ }; \
+ G_STATIC_ASSERT(G_N_ELEMENTS(name ##TypeList) == lastVal); \
+ const gchar *name ##TypeToString(int type) { \
+ return virtDBusUtilEnumToString(name ##TypeList, \
+ G_N_ELEMENTS(name ##TypeList), \
+ type); \
+ } \
+ gint name ##TypeFromString(const gchar *type) { \
+ return virtDBusUtilEnumFromString(name ##TypeList, \
+ G_N_ELEMENTS(name ##TypeList), \
+ type); \
+ }
+
+# define VIRT_DBUS_ENUM_DECL(name) \
+ const gchar *name ##TypeToString(gint type) G_GNUC_PURE; \
+ gint name ##TypeFromString(const gchar*type) G_GNUC_PURE;
--
2.15.0