]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/PosixLib/Stringlist/stringlist.c
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / StdLib / PosixLib / Stringlist / stringlist.c
CommitLineData
76beedc0 1/* $NetBSD: stringlist.c,v 1.13 2008/04/28 20:22:59 martin Exp $\r
d7ce7006 2\r
3 * Copyright (c) 1994, 1999 The NetBSD Foundation, Inc.\r
4 * All rights reserved.\r
5 *\r
6 * This code is derived from software contributed to The NetBSD Foundation\r
7 * by Christos Zoulas.\r
8 *\r
9 * Redistribution and use in source and binary forms, with or without\r
10 * modification, are permitted provided that the following conditions\r
11 * are met:\r
12 * 1. Redistributions of source code must retain the above copyright\r
13 * notice, this list of conditions and the following disclaimer.\r
14 * 2. Redistributions in binary form must reproduce the above copyright\r
15 * notice, this list of conditions and the following disclaimer in the\r
16 * documentation and/or other materials provided with the distribution.\r
17 *\r
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS\r
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED\r
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS\r
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
28 * POSSIBILITY OF SUCH DAMAGE.\r
29 */\r
76beedc0 30#if defined(_MSC_VER) /* Handle Microsoft VC++ compiler specifics. */\r
31 #pragma warning ( disable : 4018 )\r
32#endif\r
d7ce7006 33\r
34#include <sys/cdefs.h>\r
35#if defined(LIBC_SCCS) && !defined(lint)\r
36__RCSID("$NetBSD: stringlist.c,v 1.13 2008/04/28 20:22:59 martin Exp $");\r
37#endif /* LIBC_SCCS and not lint */\r
38\r
39#include <assert.h>\r
40#include <stdio.h>\r
41#include <stdlib.h>\r
42#include <string.h>\r
43#include <stringlist.h>\r
44\r
45#ifdef __weak_alias\r
46__weak_alias(sl_add,_sl_add)\r
47__weak_alias(sl_find,_sl_find)\r
48__weak_alias(sl_free,_sl_free)\r
49__weak_alias(sl_init,_sl_init)\r
50__weak_alias(sl_delete,_sl_delete)\r
51#endif\r
52\r
76beedc0 53#define _SL_CHUNKSIZE 20\r
d7ce7006 54\r
55/*\r
56 * sl_init(): Initialize a string list\r
57 */\r
58StringList *\r
59sl_init(void)\r
60{\r
76beedc0 61 StringList *sl;\r
62\r
63 sl = malloc(sizeof(StringList));\r
64 if (sl == NULL)\r
65 return NULL;\r
66\r
67 sl->sl_cur = 0;\r
68 sl->sl_max = _SL_CHUNKSIZE;\r
69 sl->sl_str = malloc(sl->sl_max * sizeof(char *));\r
70 if (sl->sl_str == NULL) {\r
71 free(sl);\r
72 sl = NULL;\r
73 }\r
74 return sl;\r
d7ce7006 75}\r
76\r
77\r
78/*\r
79 * sl_add(): Add an item to the string list\r
80 */\r
81int\r
82sl_add(StringList *sl, char *name)\r
83{\r
84\r
76beedc0 85 _DIAGASSERT(sl != NULL);\r
d7ce7006 86\r
76beedc0 87 if (sl->sl_cur == sl->sl_max - 1) {\r
88 char **new;\r
d7ce7006 89\r
76beedc0 90 new = realloc(sl->sl_str,\r
91 (sl->sl_max + _SL_CHUNKSIZE) * sizeof(char *));\r
92 if (new == NULL)\r
93 return -1;\r
94 sl->sl_max += _SL_CHUNKSIZE;\r
95 sl->sl_str = new;\r
96 }\r
97 sl->sl_str[sl->sl_cur++] = name;\r
98 return 0;\r
d7ce7006 99}\r
100\r
101\r
102/*\r
103 * sl_free(): Free a stringlist\r
104 */\r
105void\r
106sl_free(StringList *sl, int all)\r
107{\r
76beedc0 108 size_t i;\r
109\r
110 if (sl == NULL)\r
111 return;\r
112 if (sl->sl_str) {\r
113 if (all)\r
114 for (i = 0; i < sl->sl_cur; i++)\r
115 free(sl->sl_str[i]);\r
116 free(sl->sl_str);\r
117 }\r
118 free(sl);\r
d7ce7006 119}\r
120\r
121\r
122/*\r
123 * sl_find(): Find a name in the string list\r
124 */\r
125char *\r
126sl_find(StringList *sl, const char *name)\r
127{\r
76beedc0 128 size_t i;\r
d7ce7006 129\r
76beedc0 130 _DIAGASSERT(sl != NULL);\r
d7ce7006 131\r
76beedc0 132 for (i = 0; i < sl->sl_cur; i++)\r
133 if (strcmp(sl->sl_str[i], name) == 0)\r
134 return sl->sl_str[i];\r
d7ce7006 135\r
76beedc0 136 return NULL;\r
d7ce7006 137}\r
138\r
139int\r
140sl_delete(StringList *sl, const char *name, int all)\r
141{\r
76beedc0 142 size_t i, j;\r
143\r
144 for (i = 0; i < sl->sl_cur; i++)\r
145 if (strcmp(sl->sl_str[i], name) == 0) {\r
146 if (all)\r
147 free(sl->sl_str[i]);\r
148 for (j = i + 1; j < sl->sl_cur; j++)\r
149 sl->sl_str[j - 1] = sl->sl_str[j];\r
150 sl->sl_str[--sl->sl_cur] = NULL;\r
151 return 0;\r
152 }\r
153 return -1;\r
d7ce7006 154}\r
155\r