xref: /haiku/src/add-ons/kernel/file_systems/websearchfs/lists.c (revision 106388ddbfdd00f4409c86bd3fe8d581bae532ec)
1 /*
2  * Copyright 2004-2008, François Revol, <revol@free.fr>.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include <OS.h>
7 #include "lists.h"
8 
9 void slist_init(struct _slist_entry *item)
10 {
11 	item->next = NULL;
12 }
13 
14 void slist_uninit(struct _slist_entry *item)
15 {
16 	item->next = NULL;
17 }
18 
19 struct _slist_entry *slist_find(struct _slist_entry *head, slist_compare_func func, void *id)
20 {
21 	struct _slist_entry *p = head;
22 	if (head == NULL)
23 		return NULL;
24 	while (p) {
25 		if (func(p, id) == 0)
26 			return p;
27 		p = p->next;
28 	}
29 	return NULL;
30 }
31 
32 status_t slist_insert_head(struct _slist_entry **head, struct _slist_entry *item)
33 {
34 	struct _slist_entry *next = NULL;
35 	if (head == NULL || item == NULL)
36 		return EINVAL;
37 	if (*head)
38 		next = *head;
39 	item->next = next;
40 	*head = item;
41 	return B_OK;
42 }
43 
44 struct _slist_entry *slist_dequeue_tail(struct _slist_entry **head)
45 {
46 	struct _slist_entry **prev = NULL;
47 	struct _slist_entry *curr = NULL;
48 	if (head == NULL || *head == NULL)
49 		return NULL;
50 	prev = head;
51 	curr = *head;
52 	while (curr->next) {
53 		prev = &(curr->next);
54 		curr = curr->next;
55 	}
56 	*prev = NULL;
57 	return curr;
58 }
59 
60 status_t slist_remove(struct _slist_entry **head, struct _slist_entry *item)
61 {
62 	struct _slist_entry **prev = NULL;
63 	struct _slist_entry *curr = NULL;
64 	if (head == NULL || *head == NULL || item == NULL)
65 		return EINVAL;
66 	prev = head;
67 	curr = *head;
68 	while (prev && curr) {
69 		if (curr == item) {
70 			*prev = curr->next;
71 			curr->next = NULL;
72 			return B_OK;
73 		}
74 		prev = &(curr->next);
75 		curr = curr->next;
76 	}
77 	return ENOENT;
78 }
79 
80 struct _slist_entry *slist_next(struct _slist_entry *item)
81 {
82 	if (!item || !item->next)
83 		return NULL;
84 	return item->next;
85 }
86