summaryrefslogtreecommitdiff
path: root/libdevkit-power
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2008-10-23 12:03:35 +0100
committerRichard Hughes <richard@hughsie.com>2008-10-23 12:03:35 +0100
commit8d2bf6ad14ffff398d0ab4c9558bc6b208f0b322 (patch)
tree73419c4740cc184d47abae88160f0c42b3581673 /libdevkit-power
parent44efaef725583a8592c3660fa4267ba00903d9e4 (diff)
trivial: egg updates
Diffstat (limited to 'libdevkit-power')
-rw-r--r--libdevkit-power/egg-obj-list.c221
-rw-r--r--libdevkit-power/egg-obj-list.h21
2 files changed, 218 insertions, 24 deletions
diff --git a/libdevkit-power/egg-obj-list.c b/libdevkit-power/egg-obj-list.c
index 6acceb3..e64115b 100644
--- a/libdevkit-power/egg-obj-list.c
+++ b/libdevkit-power/egg-obj-list.c
@@ -36,6 +36,7 @@ struct EggObjListPrivate
EggObjListNewFunc func_new;
EggObjListCopyFunc func_copy;
EggObjListFreeFunc func_free;
+ EggObjListCompareFunc func_compare;
EggObjListToStringFunc func_to_string;
EggObjListFromStringFunc func_from_string;
GPtrArray *array;
@@ -86,6 +87,20 @@ egg_obj_list_set_free (EggObjList *list, EggObjListFreeFunc func)
}
/**
+ * egg_obj_list_set_compare:
+ * @list: a valid #EggObjList instance
+ * @func: typedef'd function
+ *
+ * Adds a compare func
+ **/
+void
+egg_obj_list_set_compare (EggObjList *list, EggObjListCompareFunc func)
+{
+ g_return_if_fail (EGG_IS_OBJ_LIST (list));
+ list->priv->func_compare = func;
+}
+
+/**
* egg_obj_list_set_to_string:
* @list: a valid #EggObjList instance
* @func: typedef'd function
@@ -169,6 +184,41 @@ egg_obj_list_print (EggObjList *list)
}
/**
+ * egg_obj_list_to_string:
+ * @list: a valid #EggObjList instance
+ *
+ * Converts the list to a newline delimited string
+ **/
+gchar *
+egg_obj_list_to_string (EggObjList *list)
+{
+ guint i;
+ gpointer obj;
+ GPtrArray *array;
+ gchar *text;
+ EggObjListToStringFunc func_to_string;
+ GString *string;
+
+ g_return_val_if_fail (list->priv->func_to_string != NULL, NULL);
+ g_return_val_if_fail (EGG_IS_OBJ_LIST (list), NULL);
+
+ array = list->priv->array;
+ func_to_string = list->priv->func_to_string;
+ string = g_string_new ("");
+ for (i=0; i<array->len; i++) {
+ obj = g_ptr_array_index (array, i);
+ text = func_to_string (obj);
+ g_string_append_printf (string, "%s\n", text);
+ g_free (text);
+ }
+ /* remove trailing newline */
+ if (string->len != 0)
+ g_string_set_size (string, string->len-1);
+
+ return g_string_free (string, FALSE);
+}
+
+/**
* egg_obj_list_add:
* @list: a valid #EggObjList instance
* @obj: a valid #gpointer object
@@ -176,7 +226,7 @@ egg_obj_list_print (EggObjList *list)
* Adds a copy of the object to the list
**/
void
-egg_obj_list_add (EggObjList *list, const gpointer obj)
+egg_obj_list_add (EggObjList *list, gconstpointer obj)
{
gpointer obj_new;
@@ -191,32 +241,167 @@ egg_obj_list_add (EggObjList *list, const gpointer obj)
}
/**
+ * egg_obj_list_add_list:
+ *
+ * Makes a deep copy of the list
+ **/
+void
+egg_obj_list_add_list (EggObjList *list, const EggObjList *data)
+{
+ guint i;
+ gconstpointer obj;
+
+ g_return_if_fail (EGG_IS_OBJ_LIST (list));
+ g_return_if_fail (EGG_IS_OBJ_LIST (data));
+
+ /* add data items to list */
+ for (i=0; i < data->len; i++) {
+ obj = egg_obj_list_index (data, i);
+ egg_obj_list_add (list, obj);
+ }
+}
+
+/**
+ * egg_obj_list_remove_list:
+ *
+ * Makes a deep copy of the list
+ **/
+void
+egg_obj_list_remove_list (EggObjList *list, const EggObjList *data)
+{
+ guint i;
+ gconstpointer obj;
+
+ g_return_if_fail (EGG_IS_OBJ_LIST (list));
+ g_return_if_fail (EGG_IS_OBJ_LIST (data));
+
+ /* remove data items from list */
+ for (i=0; i < data->len; i++) {
+ obj = egg_obj_list_index (data, i);
+ egg_obj_list_remove (list, obj);
+ }
+}
+
+/**
+ * egg_obj_list_find_obj:
+ * @list: a valid #EggObjList instance
+ * @obj: a valid #gpointer object
+ *
+ * Return value: the object
+ *
+ * Removes an item from a list
+ **/
+static gboolean
+egg_obj_list_obj_equal (EggObjList *list, gconstpointer obj1, gconstpointer obj2)
+{
+ EggObjListCompareFunc func_compare;
+
+ /* two less pointer deferences... */
+ func_compare = list->priv->func_compare;
+
+ /* trivial case */
+ if (func_compare == NULL)
+ return obj1 == obj2;
+
+ /* use helper function */
+ return func_compare (obj1, obj2) == 0;
+}
+
+/**
+ * egg_obj_list_remove_duplicate:
+ *
+ * Removes duplicate entries
+ **/
+void
+egg_obj_list_remove_duplicate (EggObjList *list)
+{
+ guint i, j;
+ gconstpointer obj1;
+ gconstpointer obj2;
+
+ for (i=0; i<list->len; i++) {
+ obj1 = egg_obj_list_index (list, i);
+ for (j=0; j<list->len; j++) {
+ if (i == j)
+ break;
+ obj2 = egg_obj_list_index (list, j);
+ if (egg_obj_list_obj_equal (list, obj1, obj2))
+ egg_obj_list_remove_index (list, i);
+ }
+ }
+}
+
+/**
+ * egg_obj_list_find_obj:
+ * @list: a valid #EggObjList instance
+ * @obj: a valid #gpointer object
+ *
+ * Return value: the object
+ *
+ * Removes an item from a list
+ **/
+static gpointer
+egg_obj_list_find_obj (EggObjList *list, gconstpointer obj)
+{
+ guint i;
+ gconstpointer obj_tmp;
+ EggObjListCompareFunc func_compare;
+
+ /* the pointers point to the same thing */
+ func_compare = list->priv->func_compare;
+ if (func_compare == NULL)
+ return (gpointer) obj;
+
+ /* remove data items from list */
+ for (i=0; i < list->len; i++) {
+ obj_tmp = egg_obj_list_index (list, i);
+ if (func_compare (obj_tmp, obj) == 0)
+ return (gpointer) obj_tmp;
+ }
+
+ /* nothing found */
+ return NULL;
+}
+
+/**
* egg_obj_list_remove:
* @list: a valid #EggObjList instance
* @obj: a valid #gpointer object
*
* Return value: TRUE is we removed something
*
- * Removes an item from a list
+ * Removes all the items from a list matching obj
**/
gboolean
-egg_obj_list_remove (EggObjList *list, const gpointer obj)
+egg_obj_list_remove (EggObjList *list, gconstpointer obj)
{
gboolean ret;
gpointer obj_new;
+ gboolean found = FALSE;
g_return_val_if_fail (EGG_IS_OBJ_LIST (list), FALSE);
g_return_val_if_fail (obj != NULL, FALSE);
g_return_val_if_fail (list->priv->func_free != NULL, FALSE);
- /* the pointers point to the same thing */
- obj_new = (gpointer) obj;
- ret = g_ptr_array_remove (list->priv->array, obj_new);
- if (!ret)
- return FALSE;
- list->priv->func_free (obj_new);
- list->len = list->priv->array->len;
- return TRUE;
+ do {
+ /* get the object */
+ obj_new = egg_obj_list_find_obj (list, obj);
+ if (obj_new == NULL)
+ break;
+
+ /* try to remove */
+ ret = g_ptr_array_remove (list->priv->array, obj_new);
+
+ /* no compare function, and pointer not found */
+ if (!ret)
+ break;
+
+ found = TRUE;
+ list->priv->func_free (obj_new);
+ list->len = list->priv->array->len;
+ } while (ret);
+
+ return found;
}
/**
@@ -256,7 +441,7 @@ gboolean
egg_obj_list_to_file (EggObjList *list, const gchar *filename)
{
guint i;
- gpointer obj;
+ gconstpointer obj;
gchar *part;
GString *string;
gboolean ret = TRUE;
@@ -378,15 +563,15 @@ out:
*
* Gets an object from the list
**/
-const gpointer
+gconstpointer
egg_obj_list_index (const EggObjList *list, guint index)
{
- gpointer obj;
+ gconstpointer obj;
g_return_val_if_fail (EGG_IS_OBJ_LIST (list), NULL);
obj = g_ptr_array_index (list->priv->array, index);
- return (const gpointer) obj;
+ return obj;
}
/**
@@ -441,6 +626,7 @@ egg_obj_list_init (EggObjList *list)
list->priv->func_new = NULL;
list->priv->func_copy = NULL;
list->priv->func_free = NULL;
+ list->priv->func_compare = NULL;
list->priv->func_to_string = NULL;
list->priv->func_from_string = NULL;
list->priv->array = g_ptr_array_new ();
@@ -477,10 +663,7 @@ egg_obj_list_test (EggTest *test)
/************************************************************/
egg_test_title (test, "get an instance");
list = egg_obj_list_new ();
- if (list != NULL)
- egg_test_success (test, NULL);
- else
- egg_test_failed (test, NULL);
+ egg_test_assert (test, list != NULL);
g_object_unref (list);
diff --git a/libdevkit-power/egg-obj-list.h b/libdevkit-power/egg-obj-list.h
index 79e9b76..5237df7 100644
--- a/libdevkit-power/egg-obj-list.h
+++ b/libdevkit-power/egg-obj-list.h
@@ -48,10 +48,12 @@ typedef struct
} EggObjListClass;
typedef gpointer (*EggObjListNewFunc) (void);
-typedef gpointer (*EggObjListCopyFunc) (const gpointer data);
+typedef gpointer (*EggObjListCopyFunc) (gconstpointer data);
typedef void (*EggObjListFreeFunc) (gpointer data);
+typedef gint (*EggObjListCompareFunc) (gconstpointer data1,
+ gconstpointer data2);
typedef gpointer (*EggObjListFromStringFunc) (const gchar *data);
-typedef gchar *(*EggObjListToStringFunc) (gpointer data);
+typedef gchar *(*EggObjListToStringFunc) (gconstpointer data);
GType egg_obj_list_get_type (void) G_GNUC_CONST;
EggObjList *egg_obj_list_new (void);
@@ -62,23 +64,32 @@ void egg_obj_list_set_copy (EggObjList *list,
EggObjListCopyFunc func);
void egg_obj_list_set_free (EggObjList *list,
EggObjListFreeFunc func);
+void egg_obj_list_set_compare (EggObjList *list,
+ EggObjListCompareFunc func);
void egg_obj_list_set_to_string (EggObjList *list,
EggObjListToStringFunc func);
void egg_obj_list_set_from_string (EggObjList *list,
EggObjListFromStringFunc func);
void egg_obj_list_clear (EggObjList *list);
void egg_obj_list_print (EggObjList *list);
+gchar *egg_obj_list_to_string (EggObjList *list)
+ G_GNUC_WARN_UNUSED_RESULT;
gboolean egg_obj_list_to_file (EggObjList *list,
const gchar *filename);
gboolean egg_obj_list_from_file (EggObjList *list,
const gchar *filename);
void egg_obj_list_add (EggObjList *list,
- const gpointer data);
+ gconstpointer data);
+void egg_obj_list_add_list (EggObjList *list,
+ const EggObjList *data);
+void egg_obj_list_remove_list (EggObjList *list,
+ const EggObjList *data);
+void egg_obj_list_remove_duplicate (EggObjList *list);
gboolean egg_obj_list_remove (EggObjList *list,
- const gpointer data);
+ gconstpointer data);
gboolean egg_obj_list_remove_index (EggObjList *list,
guint index);
-const gpointer egg_obj_list_index (const EggObjList *list,
+gconstpointer egg_obj_list_index (const EggObjList *list,
guint index);
G_END_DECLS