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



commit ba3ff25dbc4d2d515315609e80f4f09ba7e4fc6e
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 |   80 +++++++++++++++++++++++++++++++++++
 libedataserver/e-data-server-util.h |    5 ++
 2 files changed, 85 insertions(+), 0 deletions(-)
---
diff --git a/libedataserver/e-data-server-util.c b/libedataserver/e-data-server-util.c
index e5539be..ebd45a1 100644
--- a/libedataserver/e-data-server-util.c
+++ b/libedataserver/e-data-server-util.c
@@ -1266,6 +1266,86 @@ 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.
+ */
+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), 0);
+  g_return_val_if_fail (string != NULL, 0);
+
+  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 
+ */
+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]