libfreerdp-utils/list: add add/remove function.

This commit is contained in:
Vic Lee 2011-07-19 11:01:42 +08:00
parent e58181dda2
commit fd928afb09
2 changed files with 39 additions and 0 deletions

View File

@ -62,6 +62,8 @@ void test_list(void)
{
struct my_list* list;
struct my_list_item* item;
struct my_list_item* item1;
struct my_list_item* item2;
int i;
list = my_list_new();
@ -81,5 +83,16 @@ void test_list(void)
/*printf("%d %d\n", item->a, item->b);*/
}
item1 = my_list_item_new();
my_list_add(list, item1);
item2 = my_list_item_new();
my_list_add(list, item2);
CU_ASSERT(my_list_remove(list, item1) == item1);
my_list_item_free(item1);
CU_ASSERT(my_list_remove(list, item2) == item2);
CU_ASSERT(my_list_remove(list, item2) == NULL);
my_list_item_free(item2);
my_list_free(list);
}

View File

@ -94,6 +94,32 @@ static struct _item_type* _list_type##_dequeue(struct _list_type* list) \
return item; \
} \
\
static void _list_type##_add(struct _list_type* list, struct _item_type* item) \
{ \
_list_type##_enqueue(list, item); \
} \
\
static struct _item_type* _list_type##_remove(struct _list_type* list, struct _item_type* item) \
{ \
struct _item_type* prev; \
struct _item_type* curr; \
\
for (prev = NULL, curr = (struct _item_type*)list->head; curr; prev = curr, curr = ((struct _item_type##_full*)curr)->next) \
{ \
if (curr == item) \
{ \
if (prev) \
((struct _item_type##_full*)prev)->next = ((struct _item_type##_full*)curr)->next; \
if (list->head == item) \
list->head = ((struct _item_type##_full*)curr)->next; \
if (list->tail == item) \
list->tail = prev; \
return item; \
} \
} \
return NULL; \
} \
\
void _list_type##_free(struct _list_type* list) \
{ \
struct _item_type* item; \