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