Added e_enum_from_string() and e_enum_to_string() utilities in e-data-server-utiils
authorTristan Van Berkom <tristanvb@openismus.com>
Mon, 15 Oct 2012 06:45:35 +0000 (15:45 +0900)
committerTristan Van Berkom <tristanvb@openismus.com>
Thu, 22 Nov 2012 03:53:44 +0000 (12:53 +0900)
libedataserver/e-data-server-util.c
libedataserver/e-data-server-util.h

index e0aaa6b..70fad17 100644 (file)
@@ -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
index c891fbf..911c77d 100644 (file)
@@ -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;