[PATCH]gwlib enumerate list function
"fred" <[email protected]>
| Newsgroups | gmane.comp.mobile.kannel.devel |
|---|---|
| Message-ID | <008c01c5188e$8952ba10$0401a8c0@FRED4> |
ignore previous post, i used wrong version to gen diff and crlf problem on patch files?? 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.3 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 03:20:54 -0000
@@ -649,3 +649,46 @@
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 1, the enumeration can prematurely abort, returning the
+ * item last seen in the list.
+ *
+ * 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
+ *
+ * \warning locks the list and unlocks when exits
+ *
+ */
+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, 568 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 03:25:31 -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