tizen 2.3 release
[external/buxton.git] / src / shared / buxtonlist.c
1 /*
2  * This file is part of buxton.
3  *
4  * Copyright (C) 2014 Intel Corporation
5  *
6  * buxton is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation; either version 2.1
9  * of the License, or (at your option) any later version.
10  */
11
12 #ifdef HAVE_CONFIG_H
13         #include "config.h"
14 #endif
15
16 #include <assert.h>
17 #include <stdbool.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20
21 #include "buxtonlist.h"
22
23 bool buxton_list_append(BuxtonList **list, void *data)
24 {
25         BuxtonList *head = *list;
26
27         BuxtonList *parent = NULL;
28         BuxtonList *next = NULL;
29
30         if (!head) {
31                 /* New head generation */
32                 head = calloc(1, sizeof(BuxtonList));
33                 if (!head) {
34                         return false;
35                 }
36                 next = parent = head;
37                 head->size = 0;
38                 next->tail = NULL;
39         }
40
41         if (!next) {
42                 next = calloc(1, sizeof(BuxtonList));
43                 if (!next) {
44                         return false;
45                 }
46                 if (head->tail) {
47                         parent = head->tail;
48                 } else {
49                         parent = head;
50                 }
51                 parent->next = next;
52                 head->tail = next;
53         }
54         head->size += 1;
55         next->data = data;
56         *list = head;
57         return true;
58 }
59
60 bool buxton_list_prepend(BuxtonList **list, void *data)
61 {
62         BuxtonList *head = *list;
63         BuxtonList *prev = NULL;
64
65         if (!head) {
66                 /* New head generation */
67                 head = calloc(1, sizeof(BuxtonList));
68                 if (!head) {
69                         return false;
70                 }
71                 prev = head;
72                 head->size = 0;
73                 prev->tail = head;
74                 prev->next = NULL;
75         } else {
76                 /* New item */
77                 prev = calloc(1, sizeof(BuxtonList));
78                 if (!prev) {
79                         return false;
80                 }
81                 prev->size = head->size+1;
82                 head->size = 0;
83                 prev->next = head;
84                 prev->tail = head->tail;
85                 head->tail = NULL;
86         }
87         /* Previous item is now the head */
88         prev->data = data;
89         *list = prev;
90
91         return true;
92 }
93
94 bool buxton_list_remove(BuxtonList **list, void *data, bool do_free)
95 {
96         BuxtonList *head = *list;
97         BuxtonList *current = head;
98         BuxtonList *prev = head;
99
100         /* Determine the node inside the list */
101         while ((current != NULL) && (current->data != data)) {
102                 prev = current;
103                 current = current->next;
104         }
105         /* Not found */
106         if (!current) {
107                 return false;
108         }
109
110         /* Data on the head (head removal)*/
111         if (current == head) {
112                 if (head->next) {
113                         head->next->size = head->size -1;
114                         head->size = 0;
115                 }
116                 head = head->next;
117         } else {
118                 /* Middle or end */
119                 prev->next = current->next;
120                 head->size--;
121         }
122
123         /* Update tail pointer */
124         if (head && head->tail == current) {
125                 head->tail = prev;
126                 head->tail->next = NULL;
127         }
128
129         /* Should free? */
130         if (do_free) {
131                 free(current->data);
132         }
133         free(current);
134
135         *list = head;
136         return true;
137 }
138
139 /*
140  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
141  *
142  * Local variables:
143  * c-basic-offset: 8
144  * tab-width: 8
145  * indent-tabs-mode: t
146  * End:
147  *
148  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
149  * :indentSize=8:tabSize=8:noTabs=false:
150  */