[PATCH] add functions that are needed if you'd like to do something fifo alike with lists

Wilfried Goesgens <[email protected]>
Newsgroups gmane.comp.mobile.kannel.devel
Message-ID <[email protected]>
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
add_stackalike.c.diff (text/plain, 847 B)
--- 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)
 {
add_stackalike.h.diff (text/plain, 740 B)
--- 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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.