1 /* Concatenate variable number of strings.
2 Copyright (C) 1991, 1994, 2001, 2011 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., 51 Franklin Street - Fifth Floor,
19 Boston, MA 02110-1301, USA. */
24 @deftypefn Extension char* concat (const char *@var{s1}, const char *@var{s2}, @
27 Concatenate zero or more of strings and return the result in freshly
28 @code{xmalloc}ed memory. Returns @code{NULL} if insufficient memory is
29 available. The argument list is terminated by the first @code{NULL}
30 pointer encountered. Pointers to empty strings are ignored.
36 This function uses xmalloc() which is expected to be a front end
37 function to malloc() that deals with low memory situations. In
38 typical use, if malloc() returns NULL then xmalloc() diverts to an
39 error handler routine which never returns, and thus xmalloc will
40 never return a NULL pointer. If the client application wishes to
41 deal with low memory situations itself, it should supply an xmalloc
42 that just directly invokes malloc and blindly returns whatever
52 #include "libiberty.h"
53 #include <sys/types.h> /* size_t */
69 static inline unsigned long vconcat_length (const char *, va_list);
70 static inline unsigned long
71 vconcat_length (const char *first
, va_list args
)
73 unsigned long length
= 0;
76 for (arg
= first
; arg
; arg
= va_arg (args
, const char *))
77 length
+= strlen (arg
);
83 vconcat_copy (char *dst
, const char *first
, va_list args
)
88 for (arg
= first
; arg
; arg
= va_arg (args
, const char *))
90 unsigned long length
= strlen (arg
);
91 memcpy (end
, arg
, length
);
99 /* @undocumented concat_length */
102 concat_length (const char *first
, ...)
104 unsigned long length
;
106 VA_OPEN (args
, first
);
107 VA_FIXEDARG (args
, const char *, first
);
108 length
= vconcat_length (first
, args
);
114 /* @undocumented concat_copy */
117 concat_copy (char *dst
, const char *first
, ...)
121 VA_OPEN (args
, first
);
122 VA_FIXEDARG (args
, char *, dst
);
123 VA_FIXEDARG (args
, const char *, first
);
124 vconcat_copy (dst
, first
, args
);
125 save_dst
= dst
; /* With K&R C, dst goes out of scope here. */
133 #endif /* __cplusplus */
134 char *libiberty_concat_ptr
;
137 #endif /* __cplusplus */
139 /* @undocumented concat_copy2 */
142 concat_copy2 (const char *first
, ...)
144 VA_OPEN (args
, first
);
145 VA_FIXEDARG (args
, const char *, first
);
146 vconcat_copy (libiberty_concat_ptr
, first
, args
);
149 return libiberty_concat_ptr
;
153 concat (const char *first
, ...)
157 /* First compute the size of the result and get sufficient memory. */
158 VA_OPEN (args
, first
);
159 VA_FIXEDARG (args
, const char *, first
);
160 newstr
= XNEWVEC (char, vconcat_length (first
, args
) + 1);
163 /* Now copy the individual pieces to the result string. */
164 VA_OPEN (args
, first
);
165 VA_FIXEDARG (args
, const char *, first
);
166 vconcat_copy (newstr
, first
, args
);
174 @deftypefn Extension char* reconcat (char *@var{optr}, const char *@var{s1}, @
175 @dots{}, @code{NULL})
177 Same as @code{concat}, except that if @var{optr} is not @code{NULL} it
178 is freed after the string is created. This is intended to be useful
179 when you're extending an existing string or building up a string in a
183 str = reconcat (str, "pre-", str, NULL);
191 reconcat (char *optr
, const char *first
, ...)
195 /* First compute the size of the result and get sufficient memory. */
196 VA_OPEN (args
, first
);
197 VA_FIXEDARG (args
, char *, optr
);
198 VA_FIXEDARG (args
, const char *, first
);
199 newstr
= XNEWVEC (char, vconcat_length (first
, args
) + 1);
202 /* Now copy the individual pieces to the result string. */
203 VA_OPEN (args
, first
);
204 VA_FIXEDARG (args
, char *, optr
);
205 VA_FIXEDARG (args
, const char *, first
);
206 vconcat_copy (newstr
, first
, args
);
207 if (optr
) /* Done before VA_CLOSE so optr stays in scope for K&R C. */
215 #define NULLP (char *)0
217 /* Simple little test driver. */
224 printf ("\"\" = \"%s\"\n", concat (NULLP
));
225 printf ("\"a\" = \"%s\"\n", concat ("a", NULLP
));
226 printf ("\"ab\" = \"%s\"\n", concat ("a", "b", NULLP
));
227 printf ("\"abc\" = \"%s\"\n", concat ("a", "b", "c", NULLP
));
228 printf ("\"abcd\" = \"%s\"\n", concat ("ab", "cd", NULLP
));
229 printf ("\"abcde\" = \"%s\"\n", concat ("ab", "c", "de", NULLP
));
230 printf ("\"abcdef\" = \"%s\"\n", concat ("", "a", "", "bcd", "ef", NULLP
));
This page took 0.034038 seconds and 4 git commands to generate.