Initialize the gmime for upstream
[platform/upstream/gmime.git] / util / list.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*  GMime
3  *  Copyright (C) 2000-2012 Jeffrey Stedfast
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Lesser General Public License
7  *  as published by the Free Software Foundation; either version 2.1
8  *  of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public
16  *  License along with this library; if not, write to the Free
17  *  Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
18  *  02110-1301, USA.
19  */
20
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include "list.h"
27
28 void
29 list_init (List *list)
30 {
31         list->head = (ListNode *) &list->tail;
32         list->tail = NULL;
33         list->tailpred = (ListNode *) &list->head;
34 }
35
36 int
37 list_is_empty (List *list)
38 {
39         return list->head == (ListNode *) &list->tail;
40 }
41
42 int
43 list_length (List *list)
44 {
45         ListNode *node;
46         int n = 0;
47         
48         node = list->head;
49         while (node->next) {
50                 node = node->next;
51                 n++;
52         }
53         
54         return n;
55 }
56
57 ListNode *
58 list_unlink_head (List *list)
59 {
60         ListNode *n, *nn;
61         
62         n = list->head;
63         nn = n->next;
64         if (nn) {
65                 nn->prev = n->prev;
66                 list->head = nn;
67                 return n;
68         }
69         
70         return NULL;
71 }
72
73 ListNode *
74 list_unlink_tail (List *list)
75 {
76         ListNode *n, *np;
77         
78         n = list->tailpred;
79         np = n->prev;
80         if (np) {
81                 np->next = n->next;
82                 list->tailpred = np;
83                 return n;
84         }
85         
86         return NULL;
87 }
88
89 ListNode *
90 list_prepend (List *list, ListNode *node)
91 {
92         node->next = list->head;
93         node->prev = (ListNode *) &list->head;
94         list->head->prev = node;
95         list->head = node;
96         
97         return node;
98 }
99
100 ListNode *
101 list_append (List *list, ListNode *node)
102 {
103         node->next = (ListNode *) &list->tail;
104         node->prev = list->tailpred;
105         list->tailpred->next = node;
106         list->tailpred = node;
107         
108         return node;
109 }
110
111 ListNode *
112 list_unlink (ListNode *node)
113 {
114         node->next->prev = node->prev;
115         node->prev->next = node->next;
116         
117         return node;
118 }