Annotation of mandoc/compat_stringlist.c, Revision 1.3
1.3 ! schwarze 1: /* $Id: compat_stringlist.c,v 1.2 2015/05/20 22:22:59 schwarze Exp $ */
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;
1.3 ! schwarze 51: sl->sl_str = reallocarray(NULL, sl->sl_max, sizeof(char *));
1.1 schwarze 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;
1.3 ! schwarze 66: sl->sl_str = reallocarray(sl->sl_str,
! 67: sl->sl_max, sizeof(char *));
1.1 schwarze 68: if (sl->sl_str == NULL)
69: return (-1);
70: }
71: sl->sl_str[sl->sl_cur++] = name;
72: return (0);
73: }
74:
75:
76: /*
77: * sl_free(): Free a stringlist
78: */
79: void
80: sl_free(StringList *sl, int all)
81: {
82: size_t i;
83:
84: if (sl == NULL)
85: return;
86: if (sl->sl_str) {
87: if (all)
88: for (i = 0; i < sl->sl_cur; i++)
89: free(sl->sl_str[i]);
90: free(sl->sl_str);
91: }
92: free(sl);
93: }
94:
95:
96: /*
97: * sl_find(): Find a name in the string list
98: */
99: char *
100: sl_find(StringList *sl, const char *name)
101: {
102: size_t i;
103:
104: for (i = 0; i < sl->sl_cur; i++)
105: if (strcmp(sl->sl_str[i], name) == 0)
106: return sl->sl_str[i];
107:
108: return NULL;
109: }
CVSweb