Move c/h files implementing/defining standard library stuff into a new libc directory...
[kugel-rb.git] / apps / plugins / mpegplayer / mpeg_linkedlist.c
blobf2d01ee493eee969da5d4350b3e4a69975c67f6e
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Linked list API definitions
12 * Copyright (c) 2007 Michael Sevakis
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
23 #include "plugin.h"
24 #include "mpegplayer.h"
25 #include "mpeg_linkedlist.h"
27 /* Initialize a master list head */
28 void list_initialize(struct list_item *master_list_head)
30 master_list_head->prev = master_list_head->next = NULL;
33 /* Are there items after the head item? */
34 bool list_is_empty(struct list_item *head_item)
36 return head_item->next == NULL;
39 /* Does the item belong to a list? */
40 bool list_is_item_listed(struct list_item *item)
42 return item->prev != NULL;
45 /* Is the item a member in a particular list? */
46 bool list_is_member(struct list_item *master_list_head,
47 struct list_item *item)
49 if (item != master_list_head && item->prev != NULL)
51 struct list_item *curr = master_list_head->next;
53 while (curr != NULL)
55 if (item != curr)
57 curr = curr->next;
58 continue;
61 return true;
65 return false;
68 /* Remove an item from a list - no head item needed */
69 void list_remove_item(struct list_item *item)
71 if (item->prev == NULL)
73 /* Not in a list - no change - could be the master list head
74 * as well which cannot be removed */
75 return;
78 item->prev->next = item->next;
80 if (item->next != NULL)
82 /* Not last item */
83 item->next->prev = item->prev;
86 /* Mark as not in a list */
87 item->prev = NULL;
90 /* Add a list item after the base item */
91 void list_add_item(struct list_item *head_item,
92 struct list_item *item)
94 if (item->prev != NULL)
96 /* Already in a list - no change */
97 DEBUGF("list_add_item: item already in a list\n");
98 return;
101 if (item == head_item)
103 /* Cannot add the item to itself */
104 DEBUGF("list_add_item: item == head_item\n");
105 return;
108 /* Insert first */
109 item->prev = head_item;
110 item->next = head_item->next;
112 if (head_item->next != NULL)
114 /* Not first item */
115 head_item->next->prev = item;
118 head_item->next = item;
121 /* Clear list items after the head item */
122 void list_clear_all(struct list_item *head_item)
124 struct list_item *curr = head_item->next;
126 while (curr != NULL)
128 list_remove_item(curr);
129 curr = head_item->next;
133 /* Enumerate all items after the head item - passing each item in turn
134 * to the callback as well as the data value. The current item may be
135 * safely removed. Items added after the current position will be enumated
136 * but not ones added before it. The callback may return false to stop
137 * the enumeration. */
138 void list_enum_items(struct list_item *head_item,
139 list_enum_callback_t callback,
140 intptr_t data)
142 struct list_item *next = head_item->next;
144 while (next != NULL)
146 struct list_item *curr = next;
147 next = curr->next;
148 if (!callback(curr, data))
149 break;