[PATCH] gwlib enumerate list function

"fred" <[email protected]>
Newsgroups gmane.comp.mobile.kannel.devel
Message-ID <007601c51889$67c33640$0401a8c0@FRED4>
this is a new function, no code in the original kannel code uses it, but we are in various additions 
done here; its quite useful and neat and small.....
cheers
list.c.patch (application/octet-stream, 1.2 KB)
Index: list.c
===================================================================
RCS file: /home/cvs/gateway/gwlib/list.c,v
retrieving revision 1.42
diff -u -r1.42 list.c
--- list.c	11 Feb 2005 15:35:48 -0000	1.42
+++ list.c	22 Feb 2005 02:41:10 -0000
@@ -649,3 +649,42 @@
         list->len -= count;
     }
 }
+
+/**	\brief enumerate items of a list
+ *
+ * enumerate thru the items of the list, calling the user supplied function 
+ * with each item, plus optionally some environment structure.
+ * if user fn returns 0, the enumeration can prematurely abort
+ * 
+ * eg
+ * int prntmsgs(void *env, void * _msg);
+ * ...
+ * list_enumfn(list,env,prntmsgs);
+ * \param list pointer to gw list
+ * \param env pointer to your defined data
+ * \param fn pointer to your function
+ */
+void gwlist_enumfn(List *list, void * env, int (*fn)(void *env,void *item) )
+{
+    long i;
+    void *item;
+
+    lock(list);
+    item = NULL;
+    for (i = 0; i < list->len; ++i) 
+    {
+        item = GET(list, i);
+
+        if (fn(env,item)) 
+            break;
+        
+    }
+    if (i == list->len) 
+    {
+        item = NULL;
+    }
+    unlock(list);
+
+    return item;
+}
+
list.h.patch (application/octet-stream, 566 B)
Index: list.h
===================================================================
RCS file: /home/cvs/gateway/gwlib/list.h,v
retrieving revision 1.24
diff -u -r1.24 list.h
--- list.h	11 Feb 2005 15:35:48 -0000	1.24
+++ list.h	22 Feb 2005 02:41:20 -0000
@@ -296,4 +296,10 @@
  */
 void gwlist_sort(List *list, int(*cmp)(const void *, const void *));
 
+/*
+ * enumerate thru all list items with the user supplied function,
+ * if function returns non-zero, enumeration terminates
+ */
+void gwlist_enumfn(List *list, void * env, int (*fn)(void *,void *) );
+
 #endif
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.