[evolution-data-server] Added e_enum_from_string() and e_enum_to_string() utilities in e-data-server-utiils



commit ffc180a64f8ff01664e4b719e9bb38a6adfca86f
Author: Tristan Van Berkom <tristanvb openismus com>
Date:   Mon Oct 15 15:45:35 2012 +0900

    Added e_enum_from_string() and e_enum_to_string() utilities in e-data-server-utiils

 libedataserver/e-data-server-util.c |   83 +++++++++++++++++++++++++++++++++++
 libedataserver/e-data-server-util.h |    5 ++
 2 files changed, 88 insertions(+), 0 deletions(-)
---
diff --git a/libedataserver/e-data-server-util.c b/libedataserver/e-data-server-util.c
index e0aaa6b..70fad17 100644
--- a/libedataserver/e-data-server-util.c
+++ b/libedataserver/e-data-server-util.c
@@ -1266,6 +1266,89 @@ e_binding_transform_enum_nick_to_value (GBinding *binding,
 }
 
 /**
+ * e_enum_from_string:
+ * @type: The enum type
+ * @string: The string containing the enum value or nick
+ * @enum_value: A return location to store the result
+ *
+ * Fetches the appropriate enumeration value for @string in the given
+ * enum type @type and stores the result in @enum_value
+ *
+ * Returns: %TRUE if the string was a valid name or nick
+ *        for the given @type, %FALSE if the conversion failed.
+ *
+ * Since: 3.8
+ */
+gboolean
+e_enum_from_string (GType type,
+		    const gchar *string,
+		    gint *enum_value)
+{
+	GEnumClass *eclass;
+	GEnumValue *ev;
+	gchar *endptr;
+	gint value;
+	gboolean retval = TRUE;
+
+	g_return_val_if_fail (G_TYPE_IS_ENUM (type), FALSE);
+	g_return_val_if_fail (string != NULL, FALSE);
+
+	value = g_ascii_strtoull (string, &endptr, 0);
+	if (endptr != string)
+		/* parsed a number */
+		*enum_value = value;
+	else {
+		eclass = g_type_class_ref (type);
+		ev = g_enum_get_value_by_name (eclass, string);
+		if (!ev)
+			ev = g_enum_get_value_by_nick (eclass, string);
+
+		if (ev)
+			*enum_value = ev->value;
+		else
+			retval = FALSE;
+
+		g_type_class_unref (eclass);
+	}
+
+	return retval;
+}
+
+/**
+ * e_enum_to_string:
+ * @etype: An enum type
+ * @eval: The enum value to convert
+ *
+ * Converts an enum value to a string using strings from the GType system.
+ *
+ * Returns: the string representing @eval
+ *
+ * Since: 3.8
+ */
+const gchar *
+e_enum_to_string (GType etype,
+		  gint  eval)
+{
+	GEnumClass  *eclass;
+	const gchar *string = NULL;
+	guint       i;
+  
+	eclass = g_type_class_ref (etype);
+
+	g_return_val_if_fail (eclass != NULL, NULL);
+
+	for (i = 0; i < eclass->n_values; i++) {
+		if (eval == eclass->values[i].value) {
+			string = eclass->values[i].value_nick;
+			break;
+		}
+	}
+
+	g_type_class_unref (eclass);
+	return string;
+}
+
+/**
  * EAsyncClosure:
  *
  * #EAsyncClosure provides a simple way to run an asynchronous function
diff --git a/libedataserver/e-data-server-util.h b/libedataserver/e-data-server-util.h
index c891fbf..911c77d 100644
--- a/libedataserver/e-data-server-util.h
+++ b/libedataserver/e-data-server-util.h
@@ -101,6 +101,11 @@ gboolean	e_binding_transform_enum_nick_to_value
 						 const GValue *source_value,
 						 GValue *target_value,
 						 gpointer not_used);
+gboolean        e_enum_from_string              (GType type,
+						 const gchar *string,
+						 gint *enum_value);
+const gchar    *e_enum_to_string                (GType etype,
+						 gint eval);
 
 typedef struct _EAsyncClosure EAsyncClosure;
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]