Home | History | Annotate | Line # | Download | only in gen
stringlist.c revision 1.7
      1  1.7     lukem /*	$NetBSD: stringlist.c,v 1.7 1999/11/27 06:19:32 lukem Exp $	*/
      2  1.2     lukem 
      3  1.7     lukem /*-
      4  1.7     lukem  * Copyright (c) 1994, 1999 The NetBSD Foundation, Inc.
      5  1.2     lukem  * All rights reserved.
      6  1.2     lukem  *
      7  1.7     lukem  * This code is derived from software contributed to The NetBSD Foundation
      8  1.7     lukem  * by Christos Zoulas.
      9  1.7     lukem  *
     10  1.2     lukem  * Redistribution and use in source and binary forms, with or without
     11  1.2     lukem  * modification, are permitted provided that the following conditions
     12  1.2     lukem  * are met:
     13  1.2     lukem  * 1. Redistributions of source code must retain the above copyright
     14  1.2     lukem  *    notice, this list of conditions and the following disclaimer.
     15  1.2     lukem  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2     lukem  *    notice, this list of conditions and the following disclaimer in the
     17  1.2     lukem  *    documentation and/or other materials provided with the distribution.
     18  1.2     lukem  * 3. All advertising materials mentioning features or use of this software
     19  1.2     lukem  *    must display the following acknowledgement:
     20  1.7     lukem  *        This product includes software developed by the NetBSD
     21  1.7     lukem  *        Foundation, Inc. and its contributors.
     22  1.7     lukem  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.7     lukem  *    contributors may be used to endorse or promote products derived
     24  1.7     lukem  *    from this software without specific prior written permission.
     25  1.2     lukem  *
     26  1.7     lukem  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.7     lukem  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.7     lukem  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.7     lukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.7     lukem  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.7     lukem  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.7     lukem  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.7     lukem  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.7     lukem  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.7     lukem  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.7     lukem  * POSSIBILITY OF SUCH DAMAGE.
     37  1.2     lukem  */
     38  1.2     lukem 
     39  1.3  christos #include <sys/cdefs.h>
     40  1.2     lukem #if defined(LIBC_SCCS) && !defined(lint)
     41  1.7     lukem __RCSID("$NetBSD: stringlist.c,v 1.7 1999/11/27 06:19:32 lukem Exp $");
     42  1.2     lukem #endif /* LIBC_SCCS and not lint */
     43  1.2     lukem 
     44  1.3  christos #include "namespace.h"
     45  1.6     lukem 
     46  1.6     lukem #include <assert.h>
     47  1.6     lukem #include <err.h>
     48  1.2     lukem #include <stdio.h>
     49  1.6     lukem #include <stdlib.h>
     50  1.2     lukem #include <string.h>
     51  1.2     lukem #include <stringlist.h>
     52  1.4       jtc 
     53  1.4       jtc #ifdef __weak_alias
     54  1.4       jtc __weak_alias(sl_add,_sl_add);
     55  1.4       jtc __weak_alias(sl_find,_sl_find);
     56  1.4       jtc __weak_alias(sl_free,_sl_free);
     57  1.4       jtc __weak_alias(sl_init,_sl_init);
     58  1.4       jtc #endif
     59  1.2     lukem 
     60  1.2     lukem #define _SL_CHUNKSIZE	20
     61  1.2     lukem 
     62  1.2     lukem /*
     63  1.2     lukem  * sl_init(): Initialize a string list
     64  1.2     lukem  */
     65  1.2     lukem StringList *
     66  1.2     lukem sl_init()
     67  1.2     lukem {
     68  1.6     lukem 	StringList *sl;
     69  1.6     lukem 
     70  1.6     lukem 	sl = malloc(sizeof(StringList));
     71  1.2     lukem 	if (sl == NULL)
     72  1.5     enami 		err(1, "stringlist");
     73  1.2     lukem 
     74  1.2     lukem 	sl->sl_cur = 0;
     75  1.2     lukem 	sl->sl_max = _SL_CHUNKSIZE;
     76  1.2     lukem 	sl->sl_str = malloc(sl->sl_max * sizeof(char *));
     77  1.2     lukem 	if (sl->sl_str == NULL)
     78  1.5     enami 		err(1, "stringlist");
     79  1.2     lukem 	return sl;
     80  1.2     lukem }
     81  1.2     lukem 
     82  1.2     lukem 
     83  1.2     lukem /*
     84  1.2     lukem  * sl_add(): Add an item to the string list
     85  1.2     lukem  */
     86  1.2     lukem void
     87  1.2     lukem sl_add(sl, name)
     88  1.2     lukem 	StringList *sl;
     89  1.2     lukem 	char *name;
     90  1.2     lukem {
     91  1.6     lukem 
     92  1.6     lukem 	_DIAGASSERT(sl != NULL);
     93  1.6     lukem 
     94  1.2     lukem 	if (sl->sl_cur == sl->sl_max - 1) {
     95  1.2     lukem 		sl->sl_max += _SL_CHUNKSIZE;
     96  1.2     lukem 		sl->sl_str = realloc(sl->sl_str, sl->sl_max * sizeof(char *));
     97  1.2     lukem 		if (sl->sl_str == NULL)
     98  1.5     enami 			err(1, "stringlist");
     99  1.2     lukem 	}
    100  1.2     lukem 	sl->sl_str[sl->sl_cur++] = name;
    101  1.2     lukem }
    102  1.2     lukem 
    103  1.2     lukem 
    104  1.2     lukem /*
    105  1.2     lukem  * sl_free(): Free a stringlist
    106  1.2     lukem  */
    107  1.2     lukem void
    108  1.2     lukem sl_free(sl, all)
    109  1.2     lukem 	StringList *sl;
    110  1.2     lukem 	int all;
    111  1.2     lukem {
    112  1.2     lukem 	size_t i;
    113  1.2     lukem 
    114  1.2     lukem 	if (sl == NULL)
    115  1.2     lukem 		return;
    116  1.2     lukem 	if (sl->sl_str) {
    117  1.2     lukem 		if (all)
    118  1.2     lukem 			for (i = 0; i < sl->sl_cur; i++)
    119  1.2     lukem 				free(sl->sl_str[i]);
    120  1.2     lukem 		free(sl->sl_str);
    121  1.2     lukem 	}
    122  1.2     lukem 	free(sl);
    123  1.2     lukem }
    124  1.2     lukem 
    125  1.2     lukem 
    126  1.2     lukem /*
    127  1.2     lukem  * sl_find(): Find a name in the string list
    128  1.2     lukem  */
    129  1.2     lukem char *
    130  1.2     lukem sl_find(sl, name)
    131  1.2     lukem 	StringList *sl;
    132  1.2     lukem 	char *name;
    133  1.2     lukem {
    134  1.2     lukem 	size_t i;
    135  1.2     lukem 
    136  1.6     lukem 	_DIAGASSERT(sl != NULL);
    137  1.6     lukem 
    138  1.2     lukem 	for (i = 0; i < sl->sl_cur; i++)
    139  1.6     lukem 			/*
    140  1.6     lukem 			 * XXX check sl->sl_str[i] != NULL?
    141  1.6     lukem 			 */
    142  1.2     lukem 		if (strcmp(sl->sl_str[i], name) == 0)
    143  1.2     lukem 			return sl->sl_str[i];
    144  1.2     lukem 
    145  1.2     lukem 	return NULL;
    146  1.2     lukem }
    147