Annotation of mandoc/compat_stringlist.c, Revision 1.2
1.2 ! schwarze 1: /* $Id$ */
1.1 schwarze 2: /*
1.2 ! schwarze 3: * Copyright (c) 1994 Christos Zoulas <christos@netbsd.org>
1.1 schwarze 4: * All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: * 1. Redistributions of source code must retain the above copyright
10: * notice, this list of conditions and the following disclaimer.
11: * 2. Redistributions in binary form must reproduce the above copyright
12: * notice, this list of conditions and the following disclaimer in the
13: * documentation and/or other materials provided with the distribution.
14: * 4. The name of the author may not be used to endorse or promote products
15: * derived from this software without specific prior written permission.
16: *
17: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18: * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27: * SUCH DAMAGE.
28: */
29:
30: #include <err.h>
31: #include <stdlib.h>
1.2 ! schwarze 32: #include <string.h>
! 33: #include "compat_stringlist.h"
1.1 schwarze 34:
35: #define _SL_CHUNKSIZE 20
36:
37: /*
38: * sl_init(): Initialize a string list
39: */
40: StringList *
41: sl_init(void)
42: {
43: StringList *sl;
44:
45: sl = malloc(sizeof(StringList));
46: if (sl == NULL)
1.2 ! schwarze 47: err(1, "stringlist");
1.1 schwarze 48:
49: sl->sl_cur = 0;
50: sl->sl_max = _SL_CHUNKSIZE;
51: sl->sl_str = malloc(sl->sl_max * sizeof(char *));
52: if (sl->sl_str == NULL)
1.2 ! schwarze 53: err(1, "stringlist");
1.1 schwarze 54: return sl;
55: }
56:
57:
58: /*
59: * sl_add(): Add an item to the string list
60: */
61: int
62: sl_add(StringList *sl, char *name)
63: {
64: if (sl->sl_cur == sl->sl_max - 1) {
65: sl->sl_max += _SL_CHUNKSIZE;
66: sl->sl_str = reallocf(sl->sl_str, sl->sl_max * sizeof(char *));
67: if (sl->sl_str == NULL)
68: return (-1);
69: }
70: sl->sl_str[sl->sl_cur++] = name;
71: return (0);
72: }
73:
74:
75: /*
76: * sl_free(): Free a stringlist
77: */
78: void
79: sl_free(StringList *sl, int all)
80: {
81: size_t i;
82:
83: if (sl == NULL)
84: return;
85: if (sl->sl_str) {
86: if (all)
87: for (i = 0; i < sl->sl_cur; i++)
88: free(sl->sl_str[i]);
89: free(sl->sl_str);
90: }
91: free(sl);
92: }
93:
94:
95: /*
96: * sl_find(): Find a name in the string list
97: */
98: char *
99: sl_find(StringList *sl, const char *name)
100: {
101: size_t i;
102:
103: for (i = 0; i < sl->sl_cur; i++)
104: if (strcmp(sl->sl_str[i], name) == 0)
105: return sl->sl_str[i];
106:
107: return NULL;
108: }
CVSweb