Move over to log_assert.
[onak.git] / ll.c
1 /*
2  * ll.c - various things of used for dealing with linked lists.
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2000-2002 Project Purple
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 #include "ll.h"
13 #include "log.h"
14
15 struct ll *lladd(struct ll *curll, void *object)
16 {
17         struct ll *new;
18
19         if ((new = malloc(sizeof(struct ll))) == NULL) {
20                 perror("lladd()");
21                 printf("Got NULL in lladd()\n");
22                 return NULL;
23         }
24
25         new->next = curll;
26         new->object = object;
27
28         return new;
29 }
30
31 struct ll *lldel(struct ll *curll, void *object,
32         int (*objectcmp) (const void *object1, const void *object2))
33 {
34         struct ll *cur = NULL;
35         struct ll *old = NULL;
36
37         log_assert(objectcmp != NULL);
38
39         cur = curll;
40         if (cur == NULL) {
41                 return NULL;
42         } else if (!(*objectcmp)(cur->object, object)) {
43                 old = cur;
44                 cur = cur->next;
45                 free(old);
46                 return cur;
47         } 
48         while (cur->next != NULL) {
49                 if (!(*objectcmp)(cur->next->object, object)) {
50                         old = cur->next;
51                         cur->next = cur->next->next;
52                         free(old);
53                         break;
54                 }
55         }
56         return curll;
57 }
58
59 struct ll *llfind(struct ll *curll, void *object,
60         int (*objectcmp) (const void *object1, const void *object2))
61 {
62         struct ll *cur;
63
64         log_assert(objectcmp != NULL);
65
66         cur = curll;
67         while (cur != NULL && (*objectcmp)(cur->object, object)) {
68                 cur = cur->next;
69         }
70         return cur;
71 }
72
73 unsigned long llsize(struct ll *curll)
74 {
75         unsigned long count = 0;
76
77         while (curll != NULL) {
78                 count++;
79                 curll = curll->next;
80         }
81
82         return count;
83 }
84
85 /**
86  *      llfree - Frees a linked list.
87  *      @curll: The list to free.
88  *      @objectfree: A pointer to a free function for the object.
89  *
90  *      Walks through a list and free it. If a function is provided for
91  *      objectfree then it's called for each element to free them, if it's NULL
92  *      just the list is freed.
93  */
94 void llfree(struct ll *curll, void (*objectfree) (void *object))
95 {
96         struct ll *nextll;
97
98         while (curll != NULL) {
99                 nextll = curll->next;
100                 if (curll->object != NULL && objectfree != NULL) {
101                         objectfree(curll->object);
102                         curll->object = NULL;
103                 }
104                 free(curll);
105                 curll = nextll;
106         }
107         return;
108 }