1 /* $Id: genlist.c,v 1.2 2004/07/12 20:43:50 ludvigm Exp $ */
4 * Copyright (C) 2004 SuSE Linux AG, Nuernberg, Germany.
5 * Contributed by: Michal Ludvig <mludvig@suse.cz>, SUSE Labs
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/queue.h>
42 struct genlist *new = calloc(sizeof(struct genlist), 1);
47 struct genlist_entry *
48 genlist_insert (struct genlist *head, void *data)
50 struct genlist_entry *entry = calloc(sizeof(struct genlist_entry), 1);
52 TAILQ_INSERT_HEAD(head, entry, chain);
56 struct genlist_entry *
57 genlist_append (struct genlist *head, void *data)
59 struct genlist_entry *entry = calloc(sizeof(struct genlist_entry), 1);
61 TAILQ_INSERT_TAIL(head, entry, chain);
66 genlist_foreach (struct genlist *head, genlist_func_t func, void *arg)
68 struct genlist_entry *p;
70 TAILQ_FOREACH(p, head, chain) {
71 ret = (*func)(p->data, arg);
80 genlist_next (struct genlist *head, struct genlist_entry **buf)
82 struct genlist_entry *p;
85 p = TAILQ_FIRST(head);
87 p = (buf && *buf) ? TAILQ_NEXT(*buf, chain) : NULL;
90 return (p ? p->data : NULL);
94 genlist_free (struct genlist *head, genlist_freedata_t func)
96 struct genlist_entry *p;
98 while ((p = TAILQ_LAST(head, genlist)) != NULL) {
99 TAILQ_REMOVE(head, p, chain);
109 /* Here comes the example... */
111 struct genlist *l1, *l2;
115 print_entry(void *entry, void *arg)
119 printf("%s\n", (char *)entry);
124 dump_list(struct genlist *head)
126 genlist_foreach(head, print_entry, NULL);
130 free_data(void *data)
132 printf ("removing %s\n", (char *)data);
139 struct genlist_entry *gpb;
141 cf = calloc(sizeof(struct conf), 1);
142 cf->l1 = genlist_init();
143 cf->l2 = genlist_init();
145 genlist_insert(cf->l1, "Ahoj");
146 genlist_insert(cf->l1, "Cau");
147 genlist_insert(cf->l1, "Nazdar");
148 genlist_insert(cf->l1, "Te buch");
150 genlist_append(cf->l2, "Curak");
151 genlist_append(cf->l2, "Kozy");
152 genlist_append(cf->l2, "Pica");
153 genlist_append(cf->l2, "Prdel");
157 printf("\nList 1\n");
160 printf("\nList 2 - using genlist_next()\n");
161 for (cp = genlist_next (cf->l2, &gpb); cp; cp = genlist_next (0, &gpb))
164 printf("\nFreeing List 1\n");
165 /* the data here isn't actually alloc'd so we would really call
166 * genlist_free (cf->l1, 0); but to illustrate the idea */
167 genlist_free (cf->l1, free_data);