diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2011-04-30 12:06:47 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2011-04-30 12:06:47 +0300 |
commit | bd4a203ab453d78e87e29d11017b35248c9babca (patch) | |
tree | 739ddb735fdfb6aaf668ca1b2ff994004e63d6a0 /src/list.c | |
parent | 3f02a6139ea4ed7635453cbcdd600e815a5c2208 (diff) | |
download | grecs-bd4a203ab453d78e87e29d11017b35248c9babca.tar.gz grecs-bd4a203ab453d78e87e29d11017b35248c9babca.tar.bz2 |
Remove dependency on xlists.
* gnulib.modules (linked-list, xlist): Remove.
* src/list.c: New file.
* src/Makefile.am (libgrecs_a_SOURCES): Add list.c
* src/grecs-gram.y: Use grecs_list functions.
* src/preproc.c: Likewise.
* src/grecs.h (grecs_list_entry, grecs_list): New structs.
(grecs_value): use struct grecs_list* for the v.list member.
(grecs_log_to_stderr): Change type to int.
(grecs_list_create, grecs_list_size, grecs_list_push)
(grecs_list_pop, grecs_list_locate, grecs_list_index)
(grecs_list_remove_tail): New protos.
Diffstat (limited to 'src/list.c')
-rw-r--r-- | src/list.c | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/src/list.c b/src/list.c new file mode 100644 index 0000000..d2edf73 --- /dev/null +++ b/src/list.c @@ -0,0 +1,151 @@ +/* grecs - Gray's Extensible Configuration System + Copyright (C) 2007, 2008, 2009, 2010 Sergey Poznyakoff + + Grecs is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 3 of the License, or (at your + option) any later version. + + Grecs is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with Grecs. If not, see <http://www.gnu.org/licenses/>. */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif +#include <grecs.h> +#include <grecs-gram.h> +#include <stdlib.h> +#include <xalloc.h> + +struct grecs_list * +grecs_list_create () +{ + return xcalloc (1, sizeof (struct grecs_list)); +} + +size_t +grecs_list_size (struct grecs_list *lp) +{ + return lp ? lp->count : 0; +} + +void +grecs_list_append (struct grecs_list *lp, void *val) +{ + struct grecs_list_entry *ep = xmalloc (sizeof (*ep)); + ep->data = val; + ep->next = NULL; + if (lp->tail) + lp->tail->next = ep; + else + lp->head = ep; + lp->tail = ep; + lp->count++; +} + +void +grecs_list_push (struct grecs_list *lp, void *val) +{ + struct grecs_list_entry *ep = xmalloc (sizeof (*ep)); + ep->data = val; + ep->next = lp->head; + lp->head = ep; + lp->count++; +} + +void * +grecs_list_pop (struct grecs_list *lp) +{ + void *data; + struct grecs_list_entry *ep = lp->head; + if (ep) + { + data = ep->data; + lp->head = ep->next; + if (!lp->head) + lp->tail = NULL; + lp->count--; + free (ep); + } + else + data = NULL; + return data; +} + +void +grecs_list_free (struct grecs_list *lp) +{ + struct grecs_list_entry *ep = lp->head; + + while (ep) + { + struct grecs_list_entry *next = ep->next; + if (lp->free_entry) + lp->free_entry (ep->data); + free (ep); + ep = next; + } + free (lp); +} + +static int +_ptrcmp (const void *a, const void *b) +{ + return a != b; +} + +void * +grecs_list_locate (struct grecs_list *lp, void *data) +{ + struct grecs_list_entry *ep; + int (*cmp)(const void *, const void *) = lp->cmp ? lp->cmp : _ptrcmp; + + for (ep = lp->head; ep; ep = ep->next) + { + if (cmp (ep->data, data) == 0) + return ep->data; + } + return NULL; +} + +void * +grecs_list_index (struct grecs_list *lp, size_t idx) +{ + struct grecs_list_entry *ep; + + for (ep = lp->head; ep && idx; ep = ep->next, idx--) + ; + return ep ? ep->data : NULL; +} + +void * +grecs_list_remove_tail (struct grecs_list *lp) +{ + void *data; + + if (!lp->head) + return NULL; + data = lp->tail; + if (lp->head == lp->tail) + { + free (lp->tail); + lp->head = lp->tail = NULL; + lp->count = 0; + } + else + { + struct grecs_list_entry *ep; + + for (ep = lp->head; ep->next != lp->tail; ep = ep->next) + ; + free (lp->tail); + ep->next = NULL; + lp->tail = ep; + } + return data; +} |