From b604b177bb0dbd50fdfb668b513c49bc4baeefcb Mon Sep 17 00:00:00 2001 From: Christian Dywan Date: Sun, 5 Dec 2010 23:34:17 +0100 Subject: [PATCH] Use foreach with copied list for importing bookmarks The new macro KATZE_ARRAY_FOREACH_ITEM_L is added, taking a list that must be freed afterwards. It copies the list to not enter infinite recursion if items are removed or appended. --- katze/katze-array.h | 25 +++++++++++++++++++++++++ panels/midori-bookmarks.c | 4 +++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/katze/katze-array.h b/katze/katze-array.h index c4d45d5d..0593bec0 100644 --- a/katze/katze-array.h +++ b/katze/katze-array.h @@ -84,6 +84,15 @@ GList* katze_array_peek_items (KatzeArray* array); extern GList* kalistglobal; +/* KATZE_ARRAY_FOREACH_ITEM: + * @item: a #KatzeItem variable + * @array: a #KatzeArray to loop through + * + * Loops through all items of the array. The macro can + * be used like a for() loop. + * If the array is modified during the loop, you must + * use KATZE_ARRAY_FOREACH_ITEM_L instead. + * */ #define KATZE_ARRAY_FOREACH_ITEM(kaitem, kaarray) \ for (kalistglobal = katze_array_peek_items (kaarray), \ kaitem = kalistglobal ? kalistglobal->data : NULL; \ @@ -91,6 +100,22 @@ extern GList* kalistglobal; kalistglobal = g_list_next (kalistglobal), \ kaitem = kalistglobal ? kalistglobal->data : NULL) +/* KATZE_ARRAY_FOREACH_ITEM_L: + * @item: a #KatzeItem variable + * @array: a #KatzeArray to loop through + * @list: a #GList variable + * + * Loops through all items of the array. The list must be freed. + * + * Since: 0.3.0 + * */ +#define KATZE_ARRAY_FOREACH_ITEM_L(kaitem, kaarray, kalist) \ + for (kalist = katze_array_get_items (kaarray), \ + kaitem = kalist ? kalist->data : NULL; \ + kalist != NULL; \ + kalist = g_list_next (kalist), \ + kaitem = kalist ? kalist->data : NULL) + void katze_array_clear (KatzeArray* array); diff --git a/panels/midori-bookmarks.c b/panels/midori-bookmarks.c index d2e34c4b..8b30e406 100644 --- a/panels/midori-bookmarks.c +++ b/panels/midori-bookmarks.c @@ -155,15 +155,17 @@ midori_bookmarks_import_array_db (sqlite3* db, KatzeArray* array, const gchar* folder) { + GList* list; KatzeItem* item; - KATZE_ARRAY_FOREACH_ITEM (item, array) + KATZE_ARRAY_FOREACH_ITEM_L (item, array, list) { if (KATZE_IS_ARRAY (item)) midori_bookmarks_import_array_db (db, KATZE_ARRAY (item), folder); katze_item_set_meta_string (item, "folder", folder); katze_array_add_item (array, item); } + g_list_free (list); } static KatzeArray*