23
|
1 /* Libvisual - The audio visualisation framework.
|
|
2 *
|
|
3 * Copyright (C) 2004, 2005 Dennis Smit <ds@nerds-incorporated.org>
|
|
4 *
|
|
5 * List implementation from RCL.
|
|
6 * Copyright (C) 2002, 2003, 2004
|
|
7 * Dennis Smit <ds@nerds-incorporated.org>,
|
|
8 * Sepp Wijnands <mrrazz@nerds-incorporated.org>,
|
|
9 * Tom Wimmenhove <nohup@nerds-incorporated.org>
|
|
10 *
|
|
11 * Authors: Dennis Smit <ds@nerds-incorporated.org>
|
|
12 * Sepp Wijnands <mrrazz@nerds-incorporated.org>,
|
|
13 * Tom Wimmenhove <nohup@nerds-incorporated.org>
|
|
14 *
|
|
15 * $Id:
|
|
16 *
|
|
17 * This program is free software; you can redistribute it and/or modify
|
|
18 * it under the terms of the GNU Lesser General Public License as
|
|
19 * published by the Free Software Foundation; either version 2.1
|
|
20 * of the License, or (at your option) any later version.
|
|
21 *
|
|
22 * This program is distributed in the hope that it will be useful,
|
|
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
25 * GNU Lesser General Public License for more details.
|
|
26 *
|
|
27 * You should have received a copy of the GNU Lesser General Public License
|
|
28 * along with this program; if not, write to the Free Software
|
|
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
30 */
|
|
31
|
|
32 #include <stdio.h>
|
|
33 #include <stdlib.h>
|
|
34 #include <unistd.h>
|
|
35 #include <string.h>
|
|
36 #include <fcntl.h>
|
|
37
|
|
38 #include <lvconfig.h>
|
|
39 #include "lv_list.h"
|
|
40 #include "lv_log.h"
|
|
41 #include "lv_mem.h"
|
|
42
|
|
43 static int list_dtor (VisObject *object);
|
|
44
|
|
45 static int list_dtor (VisObject *object)
|
|
46 {
|
|
47 VisList *list = VISUAL_LIST (object);
|
|
48
|
|
49 visual_list_destroy_elements (list);
|
|
50
|
|
51 return VISUAL_OK;
|
|
52 }
|
|
53
|
|
54 /**
|
|
55 * @defgroup VisList VisList
|
|
56 * @{
|
|
57 */
|
|
58
|
|
59 /**
|
|
60 * Creates a new VisList structure.
|
|
61 * The VisList system is a double linked list implementation.
|
|
62 *
|
|
63 * @return A newly allocated VisList.
|
|
64 */
|
|
65 VisList *visual_list_new (VisListDestroyerFunc destroyer)
|
|
66 {
|
|
67 VisList *list;
|
|
68
|
|
69 list = visual_mem_new0 (VisList, 1);
|
|
70
|
|
71 /* Do the VisObject initialization */
|
|
72 visual_object_initialize (VISUAL_OBJECT (list), TRUE, list_dtor);
|
|
73
|
|
74 list->destroyer = destroyer;
|
|
75
|
|
76 return list;
|
|
77 }
|
|
78
|
|
79 /**
|
|
80 * Frees the VisList. This frees the VisList data structure.
|
|
81 *
|
|
82 * @param list Pointer to a VisList that needs to be freed.
|
|
83 *
|
|
84 * @return VISUAL_OK on succes, -VISUAL_ERROR_LIST_NULL or error values returned by
|
|
85 * visual_mem_free () on failure.
|
|
86 */
|
|
87 int visual_list_free (VisList *list)
|
|
88 {
|
|
89 visual_log_return_val_if_fail (list != NULL, -VISUAL_ERROR_LIST_NULL);
|
|
90
|
|
91 return visual_mem_free (list);
|
|
92 }
|
|
93
|
|
94 /**
|
|
95 * Destroys the entries that are in a list, but not the list itself. It uses the element
|
|
96 * destroyer set at visual_list_new or visual_list_set_destroyer.
|
|
97 *
|
|
98 * @param list Pointer to a VisList of which the elements need to be destroyed.
|
|
99 *
|
|
100 * @return VISUAL_OK on succes, or -VISUAL_ERROR_LIST_NULL on failure.
|
|
101 */
|
|
102 int visual_list_destroy_elements (VisList *list)
|
|
103 {
|
|
104 VisListEntry *le = NULL;
|
|
105 void *elem;
|
|
106
|
|
107 visual_log_return_val_if_fail (list != NULL, -VISUAL_ERROR_LIST_NULL);
|
|
108
|
|
109 /* Walk through the given list, possibly calling the destroyer for it */
|
|
110 if (list->destroyer == NULL) {
|
|
111 while ((elem = visual_list_next (list, &le)) != NULL)
|
|
112 visual_list_delete (list, &le);
|
|
113 } else {
|
|
114 while ((elem = visual_list_next (list, &le)) != NULL) {
|
|
115 list->destroyer (elem);
|
|
116 visual_list_delete (list, &le);
|
|
117 }
|
|
118 }
|
|
119
|
|
120 return VISUAL_OK;
|
|
121 }
|
|
122
|
|
123 /**
|
|
124 * Sets a VisListEntry destroyer function a VisList.
|
|
125 *
|
|
126 * @param list Pointer to a VisList to which the VisListDestroyerFunc is set.
|
|
127 * @param destroyer The VisListEntry destroyer function.
|
|
128 *
|
|
129 * @return VISUAL_OK on succes, -VISUAL_ERROR_LIST_NULL on failure.
|
|
130 */
|
|
131 int visual_list_set_destroyer (VisList *list, VisListDestroyerFunc destroyer)
|
|
132 {
|
|
133 visual_log_return_val_if_fail (list != NULL, -VISUAL_ERROR_LIST_NULL);
|
|
134
|
|
135 list->destroyer = destroyer;
|
|
136
|
|
137 return VISUAL_OK;
|
|
138 }
|
|
139
|
|
140 /**
|
|
141 * Go to the next entry in the list and return it's data element.
|
|
142 * This function will load the next entry in le and return a pointer
|
|
143 * to the data element.
|
|
144 *
|
|
145 * @see visual_list_prev
|
|
146 *
|
|
147 * @param list Pointer to the VisList we're traversing.
|
|
148 * @param le Pointer to a VisListEntry to store the next entry within
|
|
149 * and also to use as a reference to determine at which entry we're
|
|
150 * currently. To begin traversing do: VisListEntry *le = NULL and pass
|
|
151 * it as &le in the argument.
|
|
152 *
|
|
153 * @return The data element of the next entry, or NULL.
|
|
154 */
|
|
155 void *visual_list_next (VisList *list, VisListEntry **le)
|
|
156 {
|
|
157 visual_log_return_val_if_fail (list != NULL, NULL);
|
|
158 visual_log_return_val_if_fail (le != NULL, NULL);
|
|
159
|
|
160 if (*le == NULL)
|
|
161 *le = list->head;
|
|
162 else
|
|
163 *le = (*le)->next;
|
|
164
|
|
165 if (*le != NULL)
|
|
166 return (*le)->data;
|
|
167
|
|
168 return NULL;
|
|
169 }
|
|
170
|
|
171 /**
|
|
172 * Go to the previous entry in the list and return it's data element.
|
|
173 * This function will load the previous entry in le and return a pointer
|
|
174 * to the data element.
|
|
175 *
|
|
176 * @see visual_list_next
|
|
177 *
|
|
178 * @param list Pointer to the VisList we're traversing.
|
|
179 * @param le Pointer to a VisListEntry to store the previous entry within
|
|
180 * and also to use as a reference to determine at which entry we're
|
|
181 * currently. To begin traversing at the end of the list do:
|
|
182 * VisList *le = NULL and pass it as &le in the argument.
|
|
183 *
|
|
184 * @return The data element of the previous entry, or NULL.
|
|
185 */
|
|
186 void *visual_list_prev (VisList *list, VisListEntry **le)
|
|
187 {
|
|
188 visual_log_return_val_if_fail (list != NULL, NULL);
|
|
189 visual_log_return_val_if_fail (le != NULL, NULL);
|
|
190
|
|
191 if (!*le)
|
|
192 *le = list->tail;
|
|
193 else
|
|
194 *le = (*le)->prev;
|
|
195
|
|
196 if (*le)
|
|
197 return (*le)->data;
|
|
198
|
|
199 return NULL;
|
|
200 }
|
|
201
|
|
202 /**
|
|
203 * Get an data entry by index. This will give the pointer to an data
|
|
204 * element based on the index in the list.
|
|
205 *
|
|
206 * @param list Pointer to the VisList of which we want an element.
|
|
207 * @param index Index to determine which entry we want. The index starts at
|
|
208 * 1.
|
|
209 *
|
|
210 * @return The data element of the requested entry, or NULL.
|
|
211 */
|
|
212 void *visual_list_get (VisList *list, int index)
|
|
213 {
|
|
214 VisListEntry *le = NULL;
|
|
215 void *data = NULL;
|
|
216 int i, lc;
|
|
217
|
|
218 visual_log_return_val_if_fail (list != NULL, NULL);
|
|
219 visual_log_return_val_if_fail (index >= 0, NULL);
|
|
220
|
|
221 lc = visual_list_count (list);
|
|
222
|
|
223 if (lc - 1 < index)
|
|
224 return NULL;
|
|
225
|
|
226 for (i = 0; i <= index; i++) {
|
|
227 data = visual_list_next (list, &le);
|
|
228
|
|
229 if (data == NULL)
|
|
230 return NULL;
|
|
231 }
|
|
232
|
|
233 return data;
|
|
234 }
|
|
235
|
|
236 /**
|
|
237 * Adds an entry at the beginning of the list.
|
|
238 *
|
|
239 * @param list Pointer to the VisList to which an entry needs to be added
|
|
240 * at it's head.
|
|
241 * @param data A pointer to the data that needs to be added to the list.
|
|
242 *
|
|
243 * @return VISUAL_OK on succes, -VISUAL_ERROR_LIST_NULL on failure.
|
|
244 */
|
|
245 int visual_list_add_at_begin (VisList *list, void *data)
|
|
246 {
|
|
247 VisListEntry *current, *next;
|
|
248
|
|
249 visual_log_return_val_if_fail (list != NULL, -VISUAL_ERROR_LIST_NULL);
|
|
250
|
|
251 /* Allocate memory for new list entry */
|
|
252 current = visual_mem_new0 (VisListEntry, 1);
|
|
253
|
|
254 /* Assign data element */
|
|
255 current->data = data;
|
|
256
|
|
257 if (list->head == NULL) {
|
|
258 list->head = current;
|
|
259 list->tail = current;
|
|
260 } else {
|
|
261 next = list->head;
|
|
262
|
|
263 current->next = next;
|
|
264 list->head = current;
|
|
265 }
|
|
266
|
|
267 /* Done */
|
|
268 list->count++;
|
|
269
|
|
270 return VISUAL_OK;
|
|
271 }
|
|
272
|
|
273 /**
|
|
274 * Adds an entry at the end of the list.
|
|
275 *
|
|
276 * @param list Pointer to the VisList to which an entry needs to be added
|
|
277 * at it's tail.
|
|
278 * @param data A pointer to the data that needs to be added to the list.
|
|
279 *
|
|
280 * @return VISUAL_OK on succes, -VISUAL_ERROR_LIST_NULL on failure.
|
|
281 */
|
|
282 int visual_list_add (VisList *list, void *data)
|
|
283 {
|
|
284 VisListEntry *current, *prev;
|
|
285
|
|
286 visual_log_return_val_if_fail (list != NULL, -VISUAL_ERROR_LIST_NULL);
|
|
287
|
|
288 current = visual_mem_new0 (VisListEntry, 1);
|
|
289
|
|
290 /* Assign data element */
|
|
291 current->data = data;
|
|
292
|
|
293 /* Add list entry to list */
|
|
294 /* Is this the first entry for this list ? */
|
|
295 if (list->head == NULL) {
|
|
296 list->head = current;
|
|
297 list->tail = current;
|
|
298 } else {
|
|
299 /* Nope, add to tail of this list */
|
|
300 prev = list->tail;
|
|
301
|
|
302 /* Exchange pointers */
|
|
303 prev->next = current;
|
|
304 current->prev = prev;
|
|
305
|
|
306 /* Point tail to new entry */
|
|
307 list->tail = current;
|
|
308 }
|
|
309
|
|
310 /* Done */
|
|
311 list->count++;
|
|
312
|
|
313 return VISUAL_OK;
|
|
314 }
|
|
315
|
|
316 /**
|
|
317 * Insert an entry in the middle of a list. By adding it
|
|
318 * after the le entry.
|
|
319 *
|
|
320 * @param list Pointer to the VisList in which an entry needs to be inserted.
|
|
321 * @param le Pointer to a VisListEntry after which the entry needs to be inserted.
|
|
322 * @param data Pointer to the data the new entry represents.
|
|
323 *
|
|
324 * @return VISUAL_OK on succes, -VISUAL_ERROR_LIST_NULL, -VISUAL_ERROR_LIST_ENTRY_NULL or
|
|
325 * -VISUAL_ERROR_NULL on failure.
|
|
326 */
|
|
327 int visual_list_insert (VisList *list, VisListEntry **le, void *data)
|
|
328 {
|
|
329 VisListEntry *prev, *next, *current;
|
|
330
|
|
331 visual_log_return_val_if_fail (list != NULL, -VISUAL_ERROR_LIST_NULL);
|
|
332 visual_log_return_val_if_fail (le != NULL, -VISUAL_ERROR_LIST_ENTRY_NULL);
|
|
333 visual_log_return_val_if_fail (data != NULL, -VISUAL_ERROR_NULL);
|
|
334
|
|
335 current = visual_mem_new0 (VisListEntry, 1);
|
|
336
|
|
337 /* Assign data element */
|
|
338 current->data = data;
|
|
339
|
|
340 /* Add entry to list */
|
|
341 if (list->head == NULL && *le == NULL) {
|
|
342 /* First entry */
|
|
343 list->head = current;
|
|
344 list->tail = current;
|
|
345 } else if (*le == NULL) {
|
|
346 /* Insert entry at first position */
|
|
347 next = list->head;
|
|
348 /* Exchange pointers */
|
|
349 current->next = next;
|
|
350 next->prev = current;
|
|
351 /* Point head to current pointer */
|
|
352 list->head = current;
|
|
353 } else {
|
|
354 /* Insert entry at *le's position */
|
|
355 prev = *le;
|
|
356 next = prev->next;
|
|
357
|
|
358 current->prev = prev;
|
|
359 current->next = next;
|
|
360
|
|
361 prev->next = current;
|
|
362 if (next != NULL)
|
|
363 next->prev = current;
|
|
364 else
|
|
365 list->tail = current;
|
|
366 }
|
|
367
|
|
368 /* Hop to new entry */
|
|
369 *le = current;
|
|
370
|
|
371 /* Done */
|
|
372 list->count++;
|
|
373
|
|
374 return VISUAL_OK;
|
|
375 }
|
|
376
|
|
377 /**
|
|
378 * Removes an entry from the list.
|
|
379 *
|
|
380 * @param list A pointer to the VisList in which an entry needs to be deleted.
|
|
381 * @param le A pointer to the entry that needs to be deleted.
|
|
382 *
|
|
383 * @return VISUAL_OK on succes, -VISUAL_ERROR_LIST_NULL or -VISUAL_ERROR_LIST_ENTRY_NULL on failure.
|
|
384 */
|
|
385 int visual_list_delete (VisList *list, VisListEntry **le)
|
|
386 {
|
|
387 VisListEntry *prev, *current, *next;
|
|
388
|
|
389 visual_log_return_val_if_fail (list != NULL, -VISUAL_ERROR_LIST_NULL);
|
|
390 visual_log_return_val_if_fail (le != NULL, -VISUAL_ERROR_LIST_ENTRY_NULL);
|
|
391
|
|
392 prev = current = next = NULL;
|
|
393
|
|
394 /* Valid list entry ? */
|
|
395 if (*le == NULL) {
|
|
396 visual_log (VISUAL_LOG_CRITICAL, "There is no list entry to delete");
|
|
397
|
|
398 return -VISUAL_ERROR_LIST_ENTRY_INVALID; /* Nope */
|
|
399 }
|
|
400
|
|
401 /* Point new to le's previous entry */
|
|
402 current = *le;
|
|
403 prev = current->prev;
|
|
404 next = current->next;
|
|
405
|
|
406 /* Does it have a previous entry ? */
|
|
407 if (prev != NULL)
|
|
408 prev->next = next;
|
|
409 else
|
|
410 list->head = next;
|
|
411
|
|
412 if (next != NULL) /* It does have a next entry ? */
|
|
413 next->prev = prev;
|
|
414 else
|
|
415 list->tail = prev;
|
|
416
|
|
417 /* Point current entry to previous one */
|
|
418 *le = prev;
|
|
419
|
|
420 /* Free 'old' pointer */
|
|
421 list->count--;
|
|
422 visual_mem_free (current);
|
|
423
|
|
424 return VISUAL_OK;
|
|
425 }
|
|
426
|
|
427 /**
|
|
428 * Counts the number of entries within the list.
|
|
429 *
|
|
430 * @param list A pointer to the list from which an entry count is needed.
|
|
431 *
|
|
432 * @return The number of elements or -VISUAL_ERROR_LIST_NULL on failure.
|
|
433 */
|
|
434 int visual_list_count (VisList *list)
|
|
435 {
|
|
436 VisListEntry *le = NULL;
|
|
437 int count = 0;
|
|
438
|
|
439 visual_log_return_val_if_fail (list != NULL, -VISUAL_ERROR_LIST_NULL);
|
|
440
|
|
441 /* Walk through list */
|
|
442 while (visual_list_next (list, &le) != NULL)
|
|
443 count++;
|
|
444
|
|
445 list->count = count;
|
|
446
|
|
447 return count;
|
|
448 }
|
|
449
|
|
450 /**
|
|
451 * @}
|
|
452 */
|
|
453
|