1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
/***************************************************************************
list.c - description
-------------------
begin : Sun Sep 2 2001
copyright : (C) 2001 by Michael Speck
email : kulkanie@gmx.net
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <stdlib.h>
#include "list.h"
/*
====================================================================
Create a new list
auto_delete: Free memory of data pointer when deleting entry
callback: Use this callback to free memory of data including
the data pointer itself.
Return Value: List pointer
====================================================================
*/
List *list_create( int auto_delete, void (*callback)(void*) )
{
List *list = calloc( 1, sizeof( List ) );
list->head = calloc( 1, sizeof( ListEntry ) );
list->tail = calloc( 1, sizeof( ListEntry ) );
list->head->next = list->tail;
list->head->prev = list->head;
list->tail->next = list->tail;
list->tail->prev = list->head;
list->auto_delete = auto_delete;
list->callback = callback;
list->cur_entry = list->head;
return list;
}
/*
====================================================================
Delete list and entries.
====================================================================
*/
void list_delete( List *list )
{
list_clear( list );
free( list->head );
free( list->tail );
free( list );
}
/*
====================================================================
Delete all entries but keep the list. Reset current_entry to head
pointer.
====================================================================
*/
void list_clear( List *list )
{
while( !list_empty( list ) ) list_delete_pos( list, 0 );
}
/*
====================================================================
Insert new item at position.
Return Value: True if successful else False.
====================================================================
*/
int list_insert( List *list, void *item, int pos )
{
int i;
ListEntry *cur = list->head;
ListEntry *new_entry = 0;
/* check if insertion possible */
if ( pos < 0 || pos > list->count ) return 0;
if ( item == 0 ) return 0;
/* get to previous entry */
for (i = 0; i < pos; i++) cur = cur->next;
/* create and anchor new entry */
new_entry = calloc( 1, sizeof( ListEntry ) );
new_entry->item = item;
new_entry->next = cur->next;
new_entry->prev = cur;
cur->next->prev = new_entry;
cur->next = new_entry;
list->count++;
return 1;
}
/*
====================================================================
Add new item at the end of the list.
====================================================================
*/
int list_add( List *list, void *item )
{
ListEntry *new_entry = 0;
/* check if insertion possible */
if ( item == 0 ) return 0;
/* create and anchor new entry */
new_entry = calloc( 1, sizeof( ListEntry ) );
new_entry->item = item;
new_entry->next = list->tail;
new_entry->prev = list->tail->prev;
list->tail->prev->next = new_entry;
list->tail->prev = new_entry;
list->count++;
return 1;
}
/*
====================================================================
Delete item at position. If this was the current entry update
current_entry to valid previous pointer.
Return Value: True if successful else False.
====================================================================
*/
int list_delete_pos( List *list, int pos )
{
int i;
ListEntry *cur = list->head;
/* check if deletion possbile */
if ( list_empty( list ) ) return 0;
if ( pos < 0 || pos >= list->count ) return 0;
/* get to correct entry */
for ( i = 0; i <= pos; i++ ) cur = cur->next;
/* modify anchors */
cur->next->prev = cur->prev;
cur->prev->next = cur->next;
/* decrease counter */
list->count--;
/* check current_entry */
if ( list->cur_entry == cur )
list->cur_entry = list->cur_entry->prev;
/* free memory */
if ( list->auto_delete ) {
if ( list->callback )
(list->callback)( cur->item );
else
free( cur->item );
}
free( cur );
return 1;
}
/*
====================================================================
Delete item if in list. If this was the current entry update
current_entry to valid previous pointer.
Return Value: True if successful else False.
====================================================================
*/
int list_delete_item( List *list, void *item )
{
return list_delete_pos( list, list_check( list, item ) );
}
/*
====================================================================
Delete entry.
Return Value: True if successful else False.
====================================================================
*/
int list_delete_entry( List *list, ListEntry *entry )
{
/* delete possible? */
if ( entry == 0 ) return 0;
if ( list->count == 0 ) return 0;
if ( entry == list->head || entry == list->tail ) return 0;
/* adjust anchor and counter */
entry->prev->next = entry->next;
entry->next->prev = entry->prev;
list->count--;
/* check current_entry */
if ( list->cur_entry == entry )
list->cur_entry = list->cur_entry->prev;
/* free memory */
if ( list->auto_delete ) {
if ( list->callback )
(list->callback)( entry->item );
else
free( entry->item );
}
free( entry );
return 1;
}
/*
====================================================================
Get item from position if in list.
Return Value: Item pointer if found else Null pointer.
====================================================================
*/
void* list_get( List *list, int pos )
{
int i;
ListEntry *cur = list->head;
if ( pos < 0 || pos >= list->count ) return 0;
for ( i = 0; i <= pos; i++ ) cur = cur->next;
return cur->item;
}
/*
====================================================================
Check if item's in list.
Return Value: Position of item else -1.
====================================================================
*/
int list_check( List *list, void *item )
{
int pos = -1;
ListEntry *cur = list->head->next;
while ( cur != list->tail ) {
pos++;
if ( cur->item == item ) break;
cur = cur->next;
}
if ( cur == list->tail ) pos = -1; /* item not found */
return pos;
}
/*
====================================================================
Return first item stored in list and set current_entry to this
entry.
Return Value: Item pointer if found else Null pointer.
====================================================================
*/
void* list_first( List *list )
{
list->cur_entry = list->head->next;
return list->head->next->item;
}
/*
====================================================================
Return last item stored in list and set current_entry to this
entry.
Return Value: Item pointer if found else Null pointer.
====================================================================
*/
void* list_last( List *list )
{
list->cur_entry = list->tail->prev;
return list->tail->prev->item;
}
/*
====================================================================
Return item in current_entry.
Return Value: Item pointer if found else Null pointer.
====================================================================
*/
void* list_current( List *list )
{
return list->cur_entry->item;
}
/*
====================================================================
Reset current_entry to head of list.
====================================================================
*/
void list_reset( List *list )
{
list->cur_entry = list->head;
}
/*
====================================================================
Get next item and update current_entry (reset if tail reached)
Return Value: Item pointer if found else Null (if tail of list).
====================================================================
*/
void* list_next( List *list )
{
list->cur_entry = list->cur_entry->next;
if ( list->cur_entry == list->tail ) list_reset( list );
return list->cur_entry->item;
}
/*
====================================================================
Get previous item and update current_entry.
Return Value: Item pointer if found else Null (if head of list).
====================================================================
*/
void* list_prev( List *list )
{
list->cur_entry = list->cur_entry->prev;
return list->cur_entry->item;
}
/*
====================================================================
Delete the current entry if not tail or head. This is the entry
that contains the last returned item by list_next/prev().
Return Value: True if it was a valid deleteable entry.
====================================================================
*/
int list_delete_current( List *list )
{
if ( list->cur_entry == 0 || list->cur_entry == list->head || list->cur_entry == list->tail ) return 0;
list_delete_entry( list, list->cur_entry );
return 1;
}
/*
====================================================================
Check if list is empty.
Return Value: True if list counter is 0 else False.
====================================================================
*/
int list_empty( List *list )
{
return list->count == 0;
}
/*
====================================================================
Return entry containing the passed item.
Return Value: True if entry found else False.
====================================================================
*/
ListEntry *list_entry( List *list, void *item )
{
ListEntry *entry = list->head->next;
while ( entry != list->tail ) {
if ( entry->item == item ) return entry;
entry = entry->next;
}
return 0;
}
/*
====================================================================
Transfer an entry from one list to another list by removing from
'source' and adding to 'dest' thus if source does not contain
the item this is equvalent to list_add( dest, item ).
====================================================================
*/
void list_transfer( List *source, List *dest, void *item )
{
int old_auto_flag;
/* add to destination */
list_add( dest, item );
/* as the pointer is added to dest without changes only the empty
entry must be deleted in source */
old_auto_flag = source->auto_delete;
source->auto_delete = LIST_NO_AUTO_DELETE;
list_delete_item( source, item );
source->auto_delete = old_auto_flag;
}
/*
====================================================================
Deqeue the first list entry. (must not use auto_delete therefore)
====================================================================
*/
void *list_dequeue( List *list )
{
void *item;
if ( list->count > 0 ) {
item = list->head->next->item;
list_delete_pos( list, 0 );
return item;
}
else
return 0;
}
|