1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Linked list API definitions
12 * Copyright (c) 2007 Michael Sevakis
14 * All files in this archive are subject to the GNU General Public License.
15 * See the file COPYING in the source tree root for full license agreement.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "mpegplayer.h"
23 #include "mpeg_linkedlist.h"
25 /* Initialize a master list head */
26 void list_initialize(struct list_item
*master_list_head
)
28 master_list_head
->prev
= master_list_head
->next
= NULL
;
31 /* Are there items after the head item? */
32 bool list_is_empty(struct list_item
*head_item
)
34 return head_item
->next
== NULL
;
37 /* Does the item belong to a list? */
38 bool list_is_item_listed(struct list_item
*item
)
40 return item
->prev
!= NULL
;
43 /* Is the item a member in a particular list? */
44 bool list_is_member(struct list_item
*master_list_head
,
45 struct list_item
*item
)
47 if (item
!= master_list_head
&& item
->prev
!= NULL
)
49 struct list_item
*curr
= master_list_head
->next
;
66 /* Remove an item from a list - no head item needed */
67 void list_remove_item(struct list_item
*item
)
69 if (item
->prev
== NULL
)
71 /* Not in a list - no change - could be the master list head
72 * as well which cannot be removed */
76 item
->prev
->next
= item
->next
;
78 if (item
->next
!= NULL
)
81 item
->next
->prev
= item
->prev
;
84 /* Mark as not in a list */
88 /* Add a list item after the base item */
89 void list_add_item(struct list_item
*head_item
,
90 struct list_item
*item
)
92 if (item
->prev
!= NULL
)
94 /* Already in a list - no change */
95 DEBUGF("list_add_item: item already in a list\n");
99 if (item
== head_item
)
101 /* Cannot add the item to itself */
102 DEBUGF("list_add_item: item == head_item\n");
107 item
->prev
= head_item
;
108 item
->next
= head_item
->next
;
110 if (head_item
->next
!= NULL
)
113 head_item
->next
->prev
= item
;
116 head_item
->next
= item
;
119 /* Clear list items after the head item */
120 void list_clear_all(struct list_item
*head_item
)
122 struct list_item
*curr
= head_item
->next
;
126 list_remove_item(curr
);
127 curr
= head_item
->next
;
131 /* Enumerate all items after the head item - passing each item in turn
132 * to the callback as well as the data value. The current item may be
133 * safely removed. Items added after the current position will be enumated
134 * but not ones added before it. The callback may return false to stop
135 * the enumeration. */
136 void list_enum_items(struct list_item
*head_item
,
137 list_enum_callback_t callback
,
140 struct list_item
*next
= head_item
->next
;
144 struct list_item
*curr
= next
;
146 if (!callback(curr
, data
))