]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/PosixLib/Stringlist/stringlist.c
1 /* $NetBSD: stringlist.c,v 1.13 2008/04/28 20:22:59 martin Exp $
3 * Copyright (c) 1994, 1999 The NetBSD Foundation, Inc.
6 * This code is derived from software contributed to The NetBSD Foundation
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 #if defined(_MSC_VER) /* Handle Microsoft VC++ compiler specifics. */
31 #pragma warning ( disable : 4018 )
34 #include <sys/cdefs.h>
35 #if defined(LIBC_SCCS) && !defined(lint)
36 __RCSID("$NetBSD: stringlist.c,v 1.13 2008/04/28 20:22:59 martin Exp $");
37 #endif /* LIBC_SCCS and not lint */
43 #include <stringlist.h>
46 __weak_alias(sl_add
,_sl_add
)
47 __weak_alias(sl_find
,_sl_find
)
48 __weak_alias(sl_free
,_sl_free
)
49 __weak_alias(sl_init
,_sl_init
)
50 __weak_alias(sl_delete
,_sl_delete
)
53 #define _SL_CHUNKSIZE 20
56 * sl_init(): Initialize a string list
63 sl
= malloc(sizeof(StringList
));
68 sl
->sl_max
= _SL_CHUNKSIZE
;
69 sl
->sl_str
= malloc(sl
->sl_max
* sizeof(char *));
70 if (sl
->sl_str
== NULL
) {
79 * sl_add(): Add an item to the string list
82 sl_add(StringList
*sl
, char *name
)
85 _DIAGASSERT(sl
!= NULL
);
87 if (sl
->sl_cur
== sl
->sl_max
- 1) {
90 new = realloc(sl
->sl_str
,
91 (sl
->sl_max
+ _SL_CHUNKSIZE
) * sizeof(char *));
94 sl
->sl_max
+= _SL_CHUNKSIZE
;
97 sl
->sl_str
[sl
->sl_cur
++] = name
;
103 * sl_free(): Free a stringlist
106 sl_free(StringList
*sl
, int all
)
114 for (i
= 0; i
< sl
->sl_cur
; i
++)
123 * sl_find(): Find a name in the string list
126 sl_find(StringList
*sl
, const char *name
)
130 _DIAGASSERT(sl
!= NULL
);
132 for (i
= 0; i
< sl
->sl_cur
; i
++)
133 if (strcmp(sl
->sl_str
[i
], name
) == 0)
134 return sl
->sl_str
[i
];
140 sl_delete(StringList
*sl
, const char *name
, int all
)
144 for (i
= 0; i
< sl
->sl_cur
; i
++)
145 if (strcmp(sl
->sl_str
[i
], name
) == 0) {
148 for (j
= i
+ 1; j
< sl
->sl_cur
; j
++)
149 sl
->sl_str
[j
- 1] = sl
->sl_str
[j
];
150 sl
->sl_str
[--sl
->sl_cur
] = NULL
;