Rework device discovery API
[libjaylink.git] / libjaylink / list.c
blob6a1e32894cfccff169f6dd705afaeefe77b10ae7
1 /*
2 * This file is part of the libjaylink project.
4 * Copyright (C) 2014-2016 Marc Schink <jaylink-dev@marcschink.de>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <stdlib.h>
22 #include "libjaylink-internal.h"
24 /**
25 * @file
27 * Singly-linked list functions.
30 JAYLINK_PRIV struct list *list_prepend(struct list *list, void *data)
32 struct list *item;
34 item = malloc(sizeof(struct list));
36 if (!item)
37 return NULL;
39 item->data = data;
40 item->next = list;
42 return item;
45 JAYLINK_PRIV struct list *list_remove(struct list *list, const void *data)
47 struct list *item;
48 struct list *tmp;
50 if (!list)
51 return NULL;
53 item = list;
55 if (item->data == data) {
56 tmp = item->next;
57 free(item);
58 return tmp;
61 while (item->next) {
62 if (item->next->data == data) {
63 tmp = item->next;
64 item->next = item->next->next;
65 free(tmp);
66 break;
69 item = item->next;
72 return list;
75 JAYLINK_PRIV struct list *list_find_custom(struct list *list,
76 list_compare_callback cb, const void *cb_data)
78 if (!cb)
79 return NULL;
81 while (list) {
82 if (!cb(list->data, cb_data))
83 return list;
85 list = list->next;
88 return NULL;
91 JAYLINK_PRIV size_t list_length(struct list *list)
93 size_t n;
95 for (n = 0; list; n++)
96 list = list->next;
98 return n;
101 JAYLINK_PRIV void list_free(struct list *list)
103 struct list *tmp;
105 while (list) {
106 tmp = list;
107 list = list->next;
108 free(tmp);