chiark / gitweb /
Fix memory management of argument to open_input_file
[inn-innduct.git] / lib / list.c
1 /*  $Id: list.c 6168 2003-01-21 06:27:32Z alexk $
2 **
3 */
4
5 #include "config.h"
6 #include "clibrary.h"
7
8 #include "inn/list.h"
9
10 void
11 list_new(struct list *list)
12 {
13     list->head = (struct node *)&list->tail;
14     list->tailpred = (struct node *)&list->head;
15     list->tail = NULL;
16 }
17
18 struct node *
19 list_addhead(struct list *list, struct node *node)
20 {
21     node->succ = list->head;
22     node->pred = (struct node *)&list->head;
23     list->head->pred = node;
24     list->head = node;
25     return node;
26 }
27
28 struct node *
29 list_addtail(struct list *list, struct node *node)
30 {
31     node->succ = (struct node *)&list->tail;
32     node->pred = list->tailpred;
33     list->tailpred->succ = node;
34     list->tailpred = node;
35     return node;
36 }
37
38 struct node *
39 list_remhead(struct list *list)
40 {
41     struct node *node;
42
43     node = list->head->succ;
44     if (node) {
45         node->pred = (struct node *)&list->head;
46         node = list->head;
47         list->head = node->succ;
48     }
49     return node;
50 }
51
52 struct node *
53 list_head(struct list *list)
54 {
55     if (list->head->succ)
56         return list->head;
57     return NULL;
58 }
59
60 struct node *
61 list_tail(struct list *list)
62 {
63     if (list->tailpred->pred)
64         return list->tailpred;
65     return NULL;
66 }
67
68 struct node *
69 list_succ(struct node *node)
70 {
71     if (node->succ->succ)
72         return node->succ;
73     return NULL;
74 }
75
76 struct node *
77 list_pred(struct node *node)
78 {
79     if (node->pred->pred)
80         return node->pred;
81     return NULL;
82 }
83
84 struct node *
85 list_remove(struct node *node)
86 {
87     node->pred->succ = node->succ;
88     node->succ->pred = node->pred;
89     return node;
90 }
91
92 struct node *
93 list_remtail(struct list *list)
94 {
95     struct node *node;
96
97     node = list_tail(list);
98     if (node)
99         list_remove(node);
100     return node;
101 }
102
103 bool
104 list_isempty(struct list *list)
105 {
106     return list->tailpred == (struct node *)list;
107 }
108
109 struct node *
110 list_insert(struct list *list, struct node *node, struct node *pred)
111 {
112     if (pred) {
113         if (pred->succ) {
114             node->succ = pred->succ;
115             node->pred = pred;
116             pred->succ->pred = node;
117             pred->succ = node;
118         } else {
119             list_addtail(list, node);
120         }
121     } else {
122         list_addhead(list, node);
123     }
124     return node;
125 }