table: fixed bug when field length > 128.
[libastrodb.git] / src / list.c
blob9b402b662a28a276de6ecaa1dcf71049c57f5fc8
1 /*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2 of the License, or (at your option) any later version.
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 * Copyright (C) 2008 Liam Girdwood
20 #include <stdlib.h>
21 #include <libastrodb/astrodb.h>
23 /*! \defgroup list Lists
25 * Single and double linked lists
29 /*! \fn astrodb_slist* astrodb_slist_prepend(astrodb_slist* slist, void* object)
30 * \param slist slist
31 * \param object object
32 * \return list head
34 * Prepends an object to the list head
36 struct astrodb_slist *astrodb_slist_prepend(struct astrodb_slist *slist,
37 void *object)
39 struct astrodb_slist *list = malloc(sizeof(struct astrodb_slist));
41 if (list == NULL)
42 return NULL;
44 list->data = object;
45 list->tail = slist;
46 return list;
49 /*! \fn astrodb_slist* astrodb_slist_append(astrodb_slist* slist, void* object)
50 * \param slist slist
51 * \param object object
52 * \return list tail
54 * Appends an object to the list tail
56 struct astrodb_slist *astrodb_slist_append(struct astrodb_slist *slist,
57 void *object)
59 struct astrodb_slist *list = malloc(sizeof(struct astrodb_slist));
61 if (list == NULL)
62 return NULL;
65 list->data = object;
66 list->tail = NULL;
68 if (slist) {
69 while (slist->tail)
70 slist = slist->tail;
71 slist->tail = list;
74 return list;
77 /*! \fn void astrodb_slist_foreach(astrodb_slist* slist, astrodb_func func, void* user)
78 * \param slist slist
79 * \param func Function to be called
80 * \param user User data
82 * Calls a function for every object in slist.
84 void astrodb_slist_foreach(struct astrodb_slist *slist, astrodb_func func,
85 void *user_data)
87 if (func == NULL)
88 return;
90 while (slist) {
91 func(slist->data, user_data);
92 slist = slist->tail;
96 /*! \fn void astrodb_slist_foreach_free(astrodb_slist* slist, astrodb_func func, void* user)
97 * \param slist slist
98 * \param func Function to be called
99 * \param user User data
101 * Calls a function for every object in slist and then frees object.
103 void astrodb_slist_foreach_free(struct astrodb_slist *slist,
104 astrodb_func func, void *user_data)
106 struct astrodb_slist *slist_;
108 if (func) {
109 while (slist) {
110 func(slist->data, user_data);
111 slist_ = slist;
112 slist = slist->tail;
113 free(slist_);
115 } else {
116 while (slist) {
117 slist_ = slist;
118 slist = slist->tail;
119 free(slist_->data);
120 free(slist_);
125 /*! \fn astrodb_dlist* astrodb_dlist_prepend(astrodb_dlist* dlist, void* object)
126 * \param dlist dlist
127 * \param object object
128 * \return list head
130 * Prepends an object to the list head
132 struct astrodb_dlist *astrodb_dlist_prepend(struct astrodb_dlist *dlist,
133 void *object)
135 struct astrodb_dlist *list = malloc(sizeof(struct astrodb_dlist));
137 if (list == NULL)
138 return NULL;
140 list->data = object;
141 list->tail = dlist;
142 list->head = NULL;
143 return list;
146 /*! \fn astrodb_dlist* astrodb_dlist_prepend(astrodb_dlist* dlist, void* object)
147 * \param dlist dlist
148 * \param object object
149 * \return list head
151 * Appends an object to the list head
153 struct astrodb_dlist *astrodb_dlist_append(struct astrodb_dlist *dlist,
154 void *object)
156 struct astrodb_dlist *list = malloc(sizeof(struct astrodb_dlist));
158 if (list == NULL)
159 return NULL;
161 list->data = object;
162 list->tail = NULL;
164 if (dlist) {
165 while (dlist->tail)
166 dlist = dlist->tail;
167 dlist->tail = list;
170 list->head = dlist;
171 return list;
174 /*! \fn astrodb_dlist* astrodb_dlist_head(astrodb_dlist* dlist)
175 * \param dlist dlist
176 * \return head
178 * Get the head of a dlist
180 struct astrodb_dlist *astrodb_dlist_head(struct astrodb_dlist *dlist)
182 while (dlist->head)
183 dlist = dlist->head;
184 return dlist;
187 /*! \fn astrodb_dlist* astrodb_dlist_free_object(astrodb_dlist* dlist, void* object)
188 * \param dlist dlist
189 * \param object object
190 * \return dlist object before object
192 * Free a single object in dlist
194 struct astrodb_dlist *astrodb_dlist_free_object(struct astrodb_dlist *dlist,
195 void *object)
197 struct astrodb_dlist *head, *tail;
199 /* find object */
200 dlist = astrodb_dlist_head(dlist);
201 while (dlist->data != object && dlist)
202 dlist = dlist->tail;
204 if (dlist == NULL)
205 return NULL;
207 head = dlist->head;
208 tail = dlist->tail;
210 if (head)
211 head->tail = tail;
212 if (tail)
213 tail->head = head;
215 free(dlist->data);
216 free(dlist);
218 if (head)
219 return head;
220 if (tail) /* LIAM - remove */
221 return tail;
222 return NULL;
225 /*! \fn void astrodb_dlist_foreach(astrodb_dlist* dlist , astrodb_func func, void* user)
226 * \param dlist dlist
227 * \param func Function to be called
228 * \param user User data
230 * Call function for every object in dlist
232 void astrodb_dlist_foreach(struct astrodb_dlist *dlist, astrodb_func func,
233 void *user_data)
235 dlist = astrodb_dlist_head(dlist);
237 while (dlist) {
238 func(dlist->data, user_data);
239 dlist = dlist->tail;
243 /*! \fn void astrodb_dlist_foreach_free(astrodb_dlist* dlist, astrodb_func func, void* user)
244 * \param dlist dlist
245 * \param func Function to be called
246 * \param user User data
248 * Call function for every object in dlist and free object
250 void astrodb_dlist_foreach_free(struct astrodb_dlist *dlist,
251 astrodb_func func, void *user_data)
253 struct astrodb_dlist *dlist_;
255 dlist = astrodb_dlist_head(dlist);
257 if (func) {
258 while (dlist) {
259 func(dlist->data, user_data);
260 dlist_ = dlist;
261 dlist = dlist->tail;
262 free(dlist_->data);
263 free(dlist_);
265 } else {
266 while (dlist) {
267 dlist_ = dlist;
268 dlist = dlist->tail;
269 free(dlist_->data);
270 free(dlist_);