strlist.c revision 256281
1295016Sjkim/*	$NetBSD: strlist.c,v 1.4 2009/01/24 11:59:39 dsl Exp $	*/
2238384Sjkim
3238384Sjkim/*-
4238384Sjkim * Copyright (c) 2008 - 2009 The NetBSD Foundation, Inc.
5238384Sjkim * All rights reserved.
6238384Sjkim *
7238384Sjkim * This code is derived from software contributed to The NetBSD Foundation
8238384Sjkim * by David Laight.
9238384Sjkim *
10280304Sjkim * Redistribution and use in source and binary forms, with or without
11238384Sjkim * modification, are permitted provided that the following conditions
12238384Sjkim * are met:
13238384Sjkim * 1. Redistributions of source code must retain the above copyright
14238384Sjkim *    notice, this list of conditions and the following disclaimer.
15238384Sjkim * 2. Redistributions in binary form must reproduce the above copyright
16238384Sjkim *    notice, this list of conditions and the following disclaimer in the
17238384Sjkim *    documentation and/or other materials provided with the distribution.
18238384Sjkim * 3. Neither the name of The NetBSD Foundation nor the names of its
19238384Sjkim *    contributors may be used to endorse or promote products derived
20238384Sjkim *    from this software without specific prior written permission.
21238384Sjkim *
22238384Sjkim * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23238384Sjkim * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24238384Sjkim * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25238384Sjkim * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26238384Sjkim * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27238384Sjkim * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28238384Sjkim * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29238384Sjkim * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30238384Sjkim * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31238384Sjkim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32238384Sjkim * POSSIBILITY OF SUCH DAMAGE.
33238384Sjkim */
34238384Sjkim
35238384Sjkim#ifndef MAKE_NATIVE
36238384Sjkimstatic char rcsid[] = "$NetBSD: strlist.c,v 1.4 2009/01/24 11:59:39 dsl Exp $";
37238384Sjkim#else
38238384Sjkim#include <sys/cdefs.h>
39238384Sjkim#ifndef lint
40238384Sjkim__RCSID("$NetBSD: strlist.c,v 1.4 2009/01/24 11:59:39 dsl Exp $");
41238384Sjkim#endif /* not lint */
42238384Sjkim#endif
43238384Sjkim
44238384Sjkim#include <stddef.h>
45238384Sjkim#include <stdlib.h>
46238384Sjkim#include "strlist.h"
47238384Sjkim#include "make_malloc.h"
48238384Sjkim
49238384Sjkimvoid
50238384Sjkimstrlist_init(strlist_t *sl)
51280304Sjkim{
52238384Sjkim	sl->sl_num = 0;
53238384Sjkim	sl->sl_max = 0;
54238384Sjkim	sl->sl_items = NULL;
55238384Sjkim}
56238384Sjkim
57238384Sjkimvoid
58280304Sjkimstrlist_clean(strlist_t *sl)
59280304Sjkim{
60238384Sjkim	char *str;
61280304Sjkim	int i;
62238384Sjkim
63280304Sjkim	STRLIST_FOREACH(str, sl, i)
64280304Sjkim		free(str);
65	free(sl->sl_items);
66
67	sl->sl_num = 0;
68	sl->sl_max = 0;
69	sl->sl_items = NULL;
70}
71
72void
73strlist_add_str(strlist_t *sl, char *str, unsigned int info)
74{
75	unsigned int n;
76	strlist_item_t *items;
77
78	if (str == NULL)
79	    return;
80
81	n = sl->sl_num + 1;
82	sl->sl_num = n;
83	items = sl->sl_items;
84	if (n >= sl->sl_max) {
85	    items = bmake_realloc(items, (n + 7) * sizeof *sl->sl_items);
86	    sl->sl_items = items;
87	    sl->sl_max = n + 6;
88	}
89	items += n - 1;
90	items->si_str = str;
91	items->si_info = info;
92	items[1].si_str = NULL;         /* STRLIST_FOREACH() terminator */
93}
94