David Kiliani | 3fedd14 | 2008-11-01 00:39:12 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file me_dlist.c |
| 3 | * |
| 4 | * @brief Implements the device list class. |
| 5 | * @note Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de) |
| 6 | * @author Guenter Gebhardt |
| 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de) |
| 11 | * |
| 12 | * This file is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 25 | */ |
| 26 | |
| 27 | #include "meerror.h" |
| 28 | #include "medefines.h" |
| 29 | |
| 30 | #include "medlist.h" |
| 31 | #include "medebug.h" |
| 32 | |
| 33 | int me_dlist_query_number_devices(struct me_dlist *dlist, int *number) |
| 34 | { |
| 35 | PDEBUG_LOCKS("called.\n"); |
| 36 | *number = dlist->n; |
| 37 | return ME_ERRNO_SUCCESS; |
| 38 | } |
| 39 | |
| 40 | unsigned int me_dlist_get_number_devices(struct me_dlist *dlist) |
| 41 | { |
| 42 | PDEBUG_LOCKS("called.\n"); |
| 43 | return dlist->n; |
| 44 | } |
| 45 | |
| 46 | me_device_t *me_dlist_get_device(struct me_dlist * dlist, unsigned int index) |
| 47 | { |
| 48 | |
| 49 | struct list_head *pos; |
| 50 | me_device_t *device = NULL; |
| 51 | unsigned int i = 0; |
| 52 | |
| 53 | PDEBUG_LOCKS("called.\n"); |
| 54 | |
| 55 | if (index >= dlist->n) { |
| 56 | PERROR("Index out of range.\n"); |
| 57 | return NULL; |
| 58 | } |
| 59 | |
| 60 | list_for_each(pos, &dlist->head) { |
| 61 | if (i == index) { |
| 62 | device = list_entry(pos, me_device_t, list); |
| 63 | break; |
| 64 | } |
| 65 | |
| 66 | ++i; |
| 67 | } |
| 68 | |
| 69 | return device; |
| 70 | } |
| 71 | |
| 72 | void me_dlist_add_device_tail(struct me_dlist *dlist, me_device_t * device) |
| 73 | { |
| 74 | PDEBUG_LOCKS("called.\n"); |
| 75 | |
| 76 | list_add_tail(&device->list, &dlist->head); |
| 77 | ++dlist->n; |
| 78 | } |
| 79 | |
| 80 | me_device_t *me_dlist_del_device_tail(struct me_dlist *dlist) |
| 81 | { |
| 82 | |
| 83 | struct list_head *last; |
| 84 | me_device_t *device; |
| 85 | |
| 86 | PDEBUG_LOCKS("called.\n"); |
| 87 | |
| 88 | if (list_empty(&dlist->head)) |
| 89 | return NULL; |
| 90 | |
| 91 | last = dlist->head.prev; |
| 92 | |
| 93 | device = list_entry(last, me_device_t, list); |
| 94 | |
| 95 | list_del(last); |
| 96 | |
| 97 | --dlist->n; |
| 98 | |
| 99 | return device; |
| 100 | } |
| 101 | |
| 102 | int me_dlist_init(me_dlist_t * dlist) |
| 103 | { |
| 104 | PDEBUG_LOCKS("called.\n"); |
| 105 | |
| 106 | INIT_LIST_HEAD(&dlist->head); |
| 107 | dlist->n = 0; |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | void me_dlist_deinit(me_dlist_t * dlist) |
| 112 | { |
| 113 | |
| 114 | struct list_head *s; |
| 115 | me_device_t *device; |
| 116 | |
| 117 | PDEBUG_LOCKS("called.\n"); |
| 118 | |
| 119 | while (!list_empty(&dlist->head)) { |
| 120 | s = dlist->head.next; |
| 121 | list_del(s); |
| 122 | device = list_entry(s, me_device_t, list); |
| 123 | device->me_device_destructor(device); |
| 124 | } |
| 125 | |
| 126 | dlist->n = 0; |
| 127 | } |