Formatting changes.
[libradsec.git] / list.c
1 /* Copyright (c) 2006-2010, UNINETT AS
2  * Copyright (c) 2010-2012, NORDUnet A/S */
3 /* See LICENSE for licensing information. */
4
5 #include <stdlib.h>
6 #include <string.h>
7 #include "list.h"
8
9 /* allocates and initialises list structure; returns NULL if malloc fails */
10 struct list *list_create() {
11     struct list *list = malloc(sizeof(struct list));
12     if (list)
13         memset(list, 0, sizeof(struct list));
14     return list;
15 }
16
17 /* frees all memory associated with the list */
18 void list_destroy(struct list *list) {
19     struct list_node *node, *next;
20
21     if (!list)
22         return;
23
24     for (node = list->first; node; node = next) {
25         free(node->data);
26         next = node->next;
27         free(node);
28     }
29     free(list);
30 }
31
32 /* appends entry to list; returns 1 if ok, 0 if malloc fails */
33 int list_push(struct list *list, void *data) {
34     struct list_node *node;
35
36     node = malloc(sizeof(struct list_node));
37     if (!node)
38         return 0;
39
40     node->next = NULL;
41     node->data = data;
42
43     if (list->first)
44         list->last->next = node;
45     else
46         list->first = node;
47     list->last = node;
48
49     list->count++;
50     return 1;
51 }
52
53 /* removes first entry from list and returns data */
54 void *list_shift(struct list *list) {
55     struct list_node *node;
56     void *data;
57
58     if (!list || !list->first)
59         return NULL;
60
61     node = list->first;
62     list->first = node->next;
63     if (!list->first)
64         list->last = NULL;
65     data = node->data;
66     free(node);
67     list->count--;
68     return data;
69 }
70
71 /* removes all entries with matching data pointer */
72 void list_removedata(struct list *list, void *data) {
73     struct list_node *node, *t;
74
75     if (!list || !list->first)
76         return;
77
78     node = list->first;
79     while (node->data == data) {
80         list->first = node->next;
81         free(node);
82         list->count--;
83         node = list->first;
84         if (!node) {
85             list->last = NULL;
86             return;
87         }
88     }
89     for (; node->next; node = node->next)
90         if (node->next->data == data) {
91             t = node->next;
92             node->next = t->next;
93             free(t);
94             list->count--;
95             if (!node->next) { /* we removed the last one */
96                 list->last = node;
97                 return;
98             }
99         }
100 }
101
102 /* returns first node */
103 struct list_node *list_first(struct list *list) {
104     return list ? list->first : NULL;
105 }
106
107 /* returns the next node after the argument */
108 struct list_node *list_next(struct list_node *node) {
109     return node->next;
110 }
111
112 /* returns number of nodes */
113 uint32_t list_count(struct list *list) {
114     return list->count;
115 }
116
117 /* Local Variables: */
118 /* c-file-style: "stroustrup" */
119 /* End: */