I've needed something to make a list behave like a stack/fifo/kellerspeicher.
which means, push in, get out again, or, append, get last again.
These Functions are shameless cut'n'paste programming with a close look to
gwlist_extract_first()
Find the patches for the header and the .c atached.
Wilfried G?sgens
--- a/gateway/gwlib/list.c 2005-02-11 16:35:48.000000000 +0100
+++ b/gateway/gwlib/list.c 2006-04-04 19:29:17.000000000 +0200
@@ -298,6 +298,19 @@
return item;
}
+void *gwlist_get_last(List *list)
+{
+ void *item;
+
+ lock(list);
+ if (list->len==0)
+ item=NULL;
+ else
+ item=GET(list, list->len-1);
+ unlock(list);
+ return item;
+}
+
void *gwlist_extract_first(List *list)
{
@@ -315,6 +328,22 @@
return item;
}
+void *gwlist_extract_last(List *list)
+{
+ void *item;
+
+ gw_assert(list != NULL);
+ lock(list);
+ if (list->len == 0)
+ item = NULL;
+ else {
+ item = GET(list, list->len-1);
+ delete_items_from_list(list, list->len-1, 1);
+ }
+ unlock(list);
+ return item;
+}
+
List *gwlist_extract_matching(List *list, void *pat, gwlist_item_matches_t
*cmp)
{
--- a/gateway/gwlib/list.h 2005-02-11 16:35:48.000000000 +0100
+++ b/gateway/gwlib/list.h 2006-04-04 16:22:40.000000000 +0200
@@ -199,6 +199,11 @@
*/
void *gwlist_get(List *list, long pos);
+/*
+ * Return the item at position `pos'.
+ */
+void *gwlist_get_last(List *list);
+
/*
* Remove and return the first item in the list. Return NULL if list is
@@ -207,6 +212,13 @@
*/
void *gwlist_extract_first(List *list);
+/*
+ * Remove and return the last item in the list. Return NULL if list is
+ * empty. Note that unlike gwlist_consume, this won't sleep until there is
+ * something in the list.
+ */
+void *gwlist_extract_last(List *list);
+
/*
* Create a new list with items from `list' that match a pattern. The items