aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarry Jeffery <harry@exec64.co.uk>2017-11-23 19:12:26 +0000
committerHarry Jeffery <harry@exec64.co.uk>2017-11-23 21:34:11 +0000
commit96c320e999927d964fbd128724022e7eeb976b8d (patch)
treeb2df386912cdba848a94fb234f87a6e7c643d17e
parent57eb6a9168219d3afd90589e15b14778e832cd28 (diff)
downloadimv-96c320e999927d964fbd128724022e7eeb976b8d.tar.gz
Add list_find function
-rw-r--r--src/list.c10
-rw-r--r--src/list.h6
2 files changed, 16 insertions, 0 deletions
diff --git a/src/list.c b/src/list.c
index 3682185..3c648bf 100644
--- a/src/list.c
+++ b/src/list.c
@@ -108,4 +108,14 @@ struct list *list_from_string(const char *string, char delim)
return list;
}
+int list_find(struct list *list, int (*cmp)(const void *, const void *), const void *key)
+{
+ for(size_t i = 0; i < list->len; ++i) {
+ if(!cmp(list->items[i], key)) {
+ return (int)i;
+ }
+ }
+ return -1;
+}
+
/* vim:set ts=2 sts=2 sw=2 et: */
diff --git a/src/list.h b/src/list.h
index c74eb7f..bcc76e5 100644
--- a/src/list.h
+++ b/src/list.h
@@ -43,6 +43,12 @@ void list_insert(struct list *list, size_t index, void *item);
struct list *list_from_string(const char *string, char delim);
+int list_find(
+ struct list *list,
+ int (*cmp)(const void *item, const void *key),
+ const void *key
+);
+
#endif
/* vim:set ts=2 sts=2 sw=2 et: */