| 1 | /* $Id: list.c,v 1.76 2011/06/28 00:13:48 sbajic Exp $ */ |
|---|
| 2 | |
|---|
| 3 | /* |
|---|
| 4 | DSPAM |
|---|
| 5 | COPYRIGHT (C) 2002-2012 DSPAM PROJECT |
|---|
| 6 | |
|---|
| 7 | This program is free software: you can redistribute it and/or modify |
|---|
| 8 | it under the terms of the GNU Affero General Public License as |
|---|
| 9 | published by the Free Software Foundation, either version 3 of the |
|---|
| 10 | License, or (at your option) any later version. |
|---|
| 11 | |
|---|
| 12 | This program is distributed in the hope that it will be useful, |
|---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | GNU Affero General Public License for more details. |
|---|
| 16 | |
|---|
| 17 | You should have received a copy of the GNU Affero General Public License |
|---|
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 19 | |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | #include <stdlib.h> |
|---|
| 23 | #include <sys/types.h> |
|---|
| 24 | #include <errno.h> |
|---|
| 25 | |
|---|
| 26 | #include "list.h" |
|---|
| 27 | #include "bnr.h" |
|---|
| 28 | |
|---|
| 29 | /* list_node_create (used internally) to allocate space for a new node */ |
|---|
| 30 | |
|---|
| 31 | static struct bnr_list_node * |
|---|
| 32 | bnr_list_node_create (void *data) |
|---|
| 33 | { |
|---|
| 34 | struct bnr_list_node *node; |
|---|
| 35 | if ((node = (struct bnr_list_node *) malloc (sizeof (struct bnr_list_node))) == NULL) |
|---|
| 36 | { |
|---|
| 37 | perror("list_node_create: memory allocation error"); |
|---|
| 38 | return NULL; |
|---|
| 39 | } |
|---|
| 40 | node->ptr = data; |
|---|
| 41 | node->next = (struct bnr_list_node *) NULL; |
|---|
| 42 | return (node); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | /* list_create allocates space for and initializes a nodetree */ |
|---|
| 46 | |
|---|
| 47 | struct bnr_list * |
|---|
| 48 | bnr_list_create (int nodetype) |
|---|
| 49 | { |
|---|
| 50 | struct bnr_list *list = (struct bnr_list *) malloc (sizeof (struct bnr_list)); |
|---|
| 51 | if (list == NULL) |
|---|
| 52 | { |
|---|
| 53 | perror("bnr_list_create: memory allocation error"); |
|---|
| 54 | return NULL; |
|---|
| 55 | } |
|---|
| 56 | list->first = (struct bnr_list_node *) NULL; |
|---|
| 57 | list->insert = (struct bnr_list_node *) NULL; |
|---|
| 58 | list->items = 0; |
|---|
| 59 | list->nodetype = nodetype; |
|---|
| 60 | return (list); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | /* list_destroy methodically destroys a nodetree, freeing resources */ |
|---|
| 64 | |
|---|
| 65 | void |
|---|
| 66 | bnr_list_destroy (struct bnr_list *list) |
|---|
| 67 | { |
|---|
| 68 | struct bnr_list_node *cur, *next; |
|---|
| 69 | int i; |
|---|
| 70 | if (list == NULL) |
|---|
| 71 | { |
|---|
| 72 | return; |
|---|
| 73 | } |
|---|
| 74 | cur = list->first; |
|---|
| 75 | for (i = 0; i < list->items; i++) |
|---|
| 76 | { |
|---|
| 77 | next = cur->next; |
|---|
| 78 | if (list->nodetype != BNR_INDEX) |
|---|
| 79 | free (cur->ptr); |
|---|
| 80 | free (cur); |
|---|
| 81 | cur = next; |
|---|
| 82 | } |
|---|
| 83 | free (list); |
|---|
| 84 | /* list = (struct bnr_list *) NULL; */ |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | /* list_insert adds an item to the nodetree */ |
|---|
| 88 | |
|---|
| 89 | struct bnr_list_node * |
|---|
| 90 | bnr_list_insert (struct bnr_list *list, void *data, float value) |
|---|
| 91 | { |
|---|
| 92 | struct bnr_list_node *prev; |
|---|
| 93 | struct bnr_list_c c; |
|---|
| 94 | struct bnr_list_node *node = c_bnr_list_first (list, &c); |
|---|
| 95 | void *vptr; |
|---|
| 96 | |
|---|
| 97 | if (list->insert) { |
|---|
| 98 | prev = list->insert; |
|---|
| 99 | } else { |
|---|
| 100 | prev = 0; |
|---|
| 101 | while (node) |
|---|
| 102 | { |
|---|
| 103 | prev = node; |
|---|
| 104 | node = node->next; |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | list->items++; |
|---|
| 108 | |
|---|
| 109 | if (list->nodetype == BNR_CHAR) |
|---|
| 110 | { |
|---|
| 111 | long size = strlen ((char *) data) + 1; |
|---|
| 112 | vptr = malloc (size); |
|---|
| 113 | if (vptr == NULL) |
|---|
| 114 | { |
|---|
| 115 | perror("bnr_list_insert: memory allocation error"); |
|---|
| 116 | return NULL; |
|---|
| 117 | } |
|---|
| 118 | strcpy (vptr, data); |
|---|
| 119 | } |
|---|
| 120 | else |
|---|
| 121 | { |
|---|
| 122 | vptr = data; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | if (prev) |
|---|
| 126 | { |
|---|
| 127 | node = bnr_list_node_create (vptr); |
|---|
| 128 | if (node == NULL) |
|---|
| 129 | return NULL; |
|---|
| 130 | node->value = value; |
|---|
| 131 | node->eliminated = 0; |
|---|
| 132 | prev->next = node; |
|---|
| 133 | list->insert = node; |
|---|
| 134 | return (node); |
|---|
| 135 | } |
|---|
| 136 | else |
|---|
| 137 | { |
|---|
| 138 | node = bnr_list_node_create (vptr); |
|---|
| 139 | if (node == NULL) |
|---|
| 140 | return NULL; |
|---|
| 141 | node->value = value; |
|---|
| 142 | node->eliminated = 0; |
|---|
| 143 | list->first = node; |
|---|
| 144 | list->insert = node; |
|---|
| 145 | return (node); |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | /* c_bnr_list_next returns the next item in a nodetree */ |
|---|
| 150 | |
|---|
| 151 | struct bnr_list_node * |
|---|
| 152 | c_bnr_list_next (struct bnr_list *list, struct bnr_list_c *c) |
|---|
| 153 | { |
|---|
| 154 | struct bnr_list_node *node = c->iter_index; |
|---|
| 155 | if (node) |
|---|
| 156 | { |
|---|
| 157 | c->iter_index = node->next; |
|---|
| 158 | return (node->next); |
|---|
| 159 | } |
|---|
| 160 | else |
|---|
| 161 | { |
|---|
| 162 | if (list->items > 0) |
|---|
| 163 | { |
|---|
| 164 | c->iter_index = list->first; |
|---|
| 165 | return list->first; |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | return ((struct bnr_list_node *) NULL); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | /* list_first returns the first item in a nodetree */ |
|---|
| 173 | |
|---|
| 174 | struct bnr_list_node * |
|---|
| 175 | c_bnr_list_first (struct bnr_list *list, struct bnr_list_c *c) |
|---|
| 176 | { |
|---|
| 177 | c->iter_index = list->first; |
|---|
| 178 | return (list->first); |
|---|
| 179 | } |
|---|