Annotation of mandoc/compat_stringlist.c, Revision 1.7
1.7 ! schwarze 1: /* $Id: compat_stringlist.c,v 1.6 2015/11/07 14:22:29 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: *
15: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16: * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25: * SUCH DAMAGE.
26: */
1.7 ! schwarze 27: #include "config.h"
1.1 schwarze 28:
1.6 schwarze 29: #if HAVE_ERR
1.1 schwarze 30: #include <err.h>
1.6 schwarze 31: #endif
1.1 schwarze 32: #include <stdlib.h>
1.2 schwarze 33: #include <string.h>
34: #include "compat_stringlist.h"
1.1 schwarze 35:
36: #define _SL_CHUNKSIZE 20
37:
38: /*
39: * sl_init(): Initialize a string list
40: */
41: StringList *
42: sl_init(void)
43: {
44: StringList *sl;
45:
46: sl = malloc(sizeof(StringList));
47: if (sl == NULL)
1.2 schwarze 48: err(1, "stringlist");
1.1 schwarze 49:
50: sl->sl_cur = 0;
51: sl->sl_max = _SL_CHUNKSIZE;
1.3 schwarze 52: sl->sl_str = reallocarray(NULL, sl->sl_max, sizeof(char *));
1.1 schwarze 53: if (sl->sl_str == NULL)
1.2 schwarze 54: err(1, "stringlist");
1.1 schwarze 55: return sl;
56: }
57:
58:
59: /*
60: * sl_add(): Add an item to the string list
61: */
62: int
63: sl_add(StringList *sl, char *name)
64: {
65: if (sl->sl_cur == sl->sl_max - 1) {
66: sl->sl_max += _SL_CHUNKSIZE;
1.3 schwarze 67: sl->sl_str = reallocarray(sl->sl_str,
68: sl->sl_max, sizeof(char *));
1.1 schwarze 69: if (sl->sl_str == NULL)
70: return (-1);
71: }
72: sl->sl_str[sl->sl_cur++] = name;
73: return (0);
74: }
75:
76:
77: /*
78: * sl_free(): Free a stringlist
79: */
80: void
81: sl_free(StringList *sl, int all)
82: {
83: size_t i;
84:
85: if (sl == NULL)
86: return;
87: if (sl->sl_str) {
88: if (all)
89: for (i = 0; i < sl->sl_cur; i++)
90: free(sl->sl_str[i]);
91: free(sl->sl_str);
92: }
93: free(sl);
94: }
95:
96:
97: /*
98: * sl_find(): Find a name in the string list
99: */
100: char *
101: sl_find(StringList *sl, const char *name)
102: {
103: size_t i;
104:
105: for (i = 0; i < sl->sl_cur; i++)
106: if (strcmp(sl->sl_str[i], name) == 0)
107: return sl->sl_str[i];
108:
109: return NULL;
110: }
CVSweb