1 /* Concatenate variable number of strings.
2 Copyright (C) 1991, 1994, 2001 Free Software Foundation, Inc.
3 Written by Fred Fish @ Cygnus Support
5 This file is part of the libiberty library.
6 Libiberty is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 Libiberty is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with libiberty; see the file COPYING.LIB. If
18 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
24 @deftypefn Extension char* concat (const char *@var{s1}, const char *@var{s2}, @dots{}, @code{NULL})
26 Concatenate zero or more of strings and return the result in freshly
27 @code{xmalloc}ed memory. Returns @code{NULL} if insufficient memory is
28 available. The argument list is terminated by the first @code{NULL}
29 pointer encountered. Pointers to empty strings are ignored.
35 This function uses xmalloc() which is expected to be a front end
36 function to malloc() that deals with low memory situations. In
37 typical use, if malloc() returns NULL then xmalloc() diverts to an
38 error handler routine which never returns, and thus xmalloc will
39 never return a NULL pointer. If the client application wishes to
40 deal with low memory situations itself, it should supply an xmalloc
41 that just directly invokes malloc and blindly returns whatever
51 #include "libiberty.h"
52 #include <sys/types.h> /* size_t */
54 #ifdef ANSI_PROTOTYPES
72 static inline unsigned long vconcat_length PARAMS ((const char *, va_list));
73 static inline unsigned long
74 vconcat_length (first, args)
78 unsigned long length = 0;
81 for (arg = first; arg ; arg = va_arg (args, const char *))
82 length += strlen (arg);
87 static inline char *vconcat_copy PARAMS ((char *, const char *, va_list));
89 vconcat_copy (dst, first, args)
97 for (arg = first; arg ; arg = va_arg (args, const char *))
99 unsigned long length = strlen (arg);
100 memcpy (end, arg, length);
108 /* @undocumented concat_length */
111 concat_length VPARAMS ((const char *first, ...))
113 unsigned long length;
115 VA_OPEN (args, first);
116 VA_FIXEDARG (args, const char *, first);
117 length = vconcat_length (first, args);
123 /* @undocumented concat_copy */
126 concat_copy VPARAMS ((char *dst, const char *first, ...))
130 VA_OPEN (args, first);
131 VA_FIXEDARG (args, char *, dst);
132 VA_FIXEDARG (args, const char *, first);
133 vconcat_copy (dst, first, args);
134 save_dst = dst; /* With K&R C, dst goes out of scope here. */
140 char *libiberty_concat_ptr;
142 /* @undocumented concat_copy2 */
145 concat_copy2 VPARAMS ((const char *first, ...))
147 VA_OPEN (args, first);
148 VA_FIXEDARG (args, const char *, first);
149 vconcat_copy (libiberty_concat_ptr, first, args);
152 return libiberty_concat_ptr;
156 concat VPARAMS ((const char *first, ...))
160 /* First compute the size of the result and get sufficient memory. */
161 VA_OPEN (args, first);
162 VA_FIXEDARG (args, const char *, first);
163 newstr = (char *) xmalloc (vconcat_length (first, args) + 1);
166 /* Now copy the individual pieces to the result string. */
167 VA_OPEN (args, first);
168 VA_FIXEDARG (args, const char *, first);
169 vconcat_copy (newstr, first, args);
177 @deftypefn Extension char* reconcat (char *@var{optr}, const char *@var{s1}, @dots{}, @code{NULL})
179 Same as @code{concat}, except that if @var{optr} is not @code{NULL} it
180 is freed after the string is created. This is intended to be useful
181 when you're extending an existing string or building up a string in a
185 str = reconcat (str, "pre-", str, NULL);
193 reconcat VPARAMS ((char *optr, const char *first, ...))
197 /* First compute the size of the result and get sufficient memory. */
198 VA_OPEN (args, first);
199 VA_FIXEDARG (args, char *, optr);
200 VA_FIXEDARG (args, const char *, first);
201 newstr = (char *) xmalloc (vconcat_length (first, args) + 1);
204 /* Now copy the individual pieces to the result string. */
205 VA_OPEN (args, first);
206 VA_FIXEDARG (args, char *, optr);
207 VA_FIXEDARG (args, const char *, first);
208 vconcat_copy (newstr, first, args);
209 if (optr) /* Done before VA_CLOSE so optr stays in scope for K&R C. */
217 #define NULLP (char *)0
219 /* Simple little test driver. */
226 printf ("\"\" = \"%s\"\n", concat (NULLP));
227 printf ("\"a\" = \"%s\"\n", concat ("a", NULLP));
228 printf ("\"ab\" = \"%s\"\n", concat ("a", "b", NULLP));
229 printf ("\"abc\" = \"%s\"\n", concat ("a", "b", "c", NULLP));
230 printf ("\"abcd\" = \"%s\"\n", concat ("ab", "cd", NULLP));
231 printf ("\"abcde\" = \"%s\"\n", concat ("ab", "c", "de", NULLP));
232 printf ("\"abcdef\" = \"%s\"\n", concat ("", "a", "", "bcd", "ef", NULLP));