Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* Concatenate variable number of strings. |
e495212d | 2 | Copyright (C) 1991-2017 Free Software Foundation, Inc. |
252b5132 RH |
3 | Written by Fred Fish @ Cygnus Support |
4 | ||
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. | |
10 | ||
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. | |
15 | ||
16 | You should have received a copy of the GNU Library General Public | |
17 | License along with libiberty; see the file COPYING.LIB. If | |
979c05d3 NC |
18 | not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, |
19 | Boston, MA 02110-1301, USA. */ | |
252b5132 RH |
20 | |
21 | ||
22 | /* | |
23 | ||
d4d868a2 RW |
24 | @deftypefn Extension char* concat (const char *@var{s1}, const char *@var{s2}, @ |
25 | @dots{}, @code{NULL}) | |
252b5132 | 26 | |
ba19b94f | 27 | Concatenate zero or more of strings and return the result in freshly |
689a1abf | 28 | @code{xmalloc}ed memory. The argument list is terminated by the first |
29 | @code{NULL} pointer encountered. Pointers to empty strings are ignored. | |
252b5132 | 30 | |
ba19b94f | 31 | @end deftypefn |
252b5132 | 32 | |
252b5132 RH |
33 | */ |
34 | ||
35 | ||
65a4f13c DD |
36 | #ifdef HAVE_CONFIG_H |
37 | #include "config.h" | |
38 | #endif | |
252b5132 RH |
39 | #include "ansidecl.h" |
40 | #include "libiberty.h" | |
926150e2 | 41 | #include <sys/types.h> /* size_t */ |
252b5132 | 42 | |
252b5132 | 43 | #include <stdarg.h> |
252b5132 | 44 | |
d42dae6c DD |
45 | # if HAVE_STRING_H |
46 | # include <string.h> | |
47 | # else | |
48 | # if HAVE_STRINGS_H | |
49 | # include <strings.h> | |
50 | # endif | |
51 | # endif | |
252b5132 | 52 | |
916aaa12 DD |
53 | #if HAVE_STDLIB_H |
54 | #include <stdlib.h> | |
55 | #endif | |
56 | ||
9334f9c6 | 57 | static inline unsigned long vconcat_length (const char *, va_list); |
54c20242 | 58 | static inline unsigned long |
9334f9c6 | 59 | vconcat_length (const char *first, va_list args) |
252b5132 | 60 | { |
54c20242 DD |
61 | unsigned long length = 0; |
62 | const char *arg; | |
252b5132 | 63 | |
d42dae6c DD |
64 | for (arg = first; arg ; arg = va_arg (args, const char *)) |
65 | length += strlen (arg); | |
66 | ||
54c20242 DD |
67 | return length; |
68 | } | |
252b5132 | 69 | |
54c20242 | 70 | static inline char * |
9334f9c6 | 71 | vconcat_copy (char *dst, const char *first, va_list args) |
54c20242 DD |
72 | { |
73 | char *end = dst; | |
74 | const char *arg; | |
d42dae6c | 75 | |
d42dae6c DD |
76 | for (arg = first; arg ; arg = va_arg (args, const char *)) |
77 | { | |
54c20242 | 78 | unsigned long length = strlen (arg); |
d42dae6c DD |
79 | memcpy (end, arg, length); |
80 | end += length; | |
252b5132 | 81 | } |
d42dae6c | 82 | *end = '\000'; |
54c20242 DD |
83 | |
84 | return dst; | |
85 | } | |
86 | ||
ba19b94f DD |
87 | /* @undocumented concat_length */ |
88 | ||
54c20242 | 89 | unsigned long |
9334f9c6 | 90 | concat_length (const char *first, ...) |
54c20242 DD |
91 | { |
92 | unsigned long length; | |
281e9aa6 | 93 | va_list args; |
54c20242 | 94 | |
281e9aa6 | 95 | va_start (args, first); |
54c20242 | 96 | length = vconcat_length (first, args); |
281e9aa6 | 97 | va_end (args); |
54c20242 DD |
98 | |
99 | return length; | |
100 | } | |
101 | ||
ba19b94f DD |
102 | /* @undocumented concat_copy */ |
103 | ||
54c20242 | 104 | char * |
9334f9c6 | 105 | concat_copy (char *dst, const char *first, ...) |
54c20242 DD |
106 | { |
107 | char *save_dst; | |
281e9aa6 | 108 | va_list args; |
54c20242 | 109 | |
281e9aa6 | 110 | va_start (args, first); |
54c20242 DD |
111 | vconcat_copy (dst, first, args); |
112 | save_dst = dst; /* With K&R C, dst goes out of scope here. */ | |
281e9aa6 | 113 | va_end (args); |
54c20242 DD |
114 | |
115 | return save_dst; | |
116 | } | |
117 | ||
abf6a75b DD |
118 | #ifdef __cplusplus |
119 | extern "C" { | |
120 | #endif /* __cplusplus */ | |
54c20242 | 121 | char *libiberty_concat_ptr; |
abf6a75b DD |
122 | #ifdef __cplusplus |
123 | } | |
124 | #endif /* __cplusplus */ | |
54c20242 | 125 | |
ba19b94f DD |
126 | /* @undocumented concat_copy2 */ |
127 | ||
54c20242 | 128 | char * |
9334f9c6 | 129 | concat_copy2 (const char *first, ...) |
54c20242 | 130 | { |
281e9aa6 | 131 | va_list args; |
132 | va_start (args, first); | |
54c20242 | 133 | vconcat_copy (libiberty_concat_ptr, first, args); |
281e9aa6 | 134 | va_end (args); |
54c20242 DD |
135 | |
136 | return libiberty_concat_ptr; | |
137 | } | |
138 | ||
139 | char * | |
9334f9c6 | 140 | concat (const char *first, ...) |
54c20242 DD |
141 | { |
142 | char *newstr; | |
281e9aa6 | 143 | va_list args; |
54c20242 DD |
144 | |
145 | /* First compute the size of the result and get sufficient memory. */ | |
281e9aa6 | 146 | va_start (args, first); |
abf6a75b | 147 | newstr = XNEWVEC (char, vconcat_length (first, args) + 1); |
281e9aa6 | 148 | va_end (args); |
54c20242 DD |
149 | |
150 | /* Now copy the individual pieces to the result string. */ | |
281e9aa6 | 151 | va_start (args, first); |
54c20242 | 152 | vconcat_copy (newstr, first, args); |
281e9aa6 | 153 | va_end (args); |
252b5132 | 154 | |
d42dae6c | 155 | return newstr; |
252b5132 RH |
156 | } |
157 | ||
ba19b94f DD |
158 | /* |
159 | ||
d4d868a2 RW |
160 | @deftypefn Extension char* reconcat (char *@var{optr}, const char *@var{s1}, @ |
161 | @dots{}, @code{NULL}) | |
ba19b94f DD |
162 | |
163 | Same as @code{concat}, except that if @var{optr} is not @code{NULL} it | |
164 | is freed after the string is created. This is intended to be useful | |
165 | when you're extending an existing string or building up a string in a | |
166 | loop: | |
167 | ||
168 | @example | |
169 | str = reconcat (str, "pre-", str, NULL); | |
170 | @end example | |
171 | ||
172 | @end deftypefn | |
173 | ||
174 | */ | |
175 | ||
99ee3a8f | 176 | char * |
9334f9c6 | 177 | reconcat (char *optr, const char *first, ...) |
99ee3a8f DD |
178 | { |
179 | char *newstr; | |
281e9aa6 | 180 | va_list args; |
99ee3a8f DD |
181 | |
182 | /* First compute the size of the result and get sufficient memory. */ | |
281e9aa6 | 183 | va_start (args, first); |
abf6a75b | 184 | newstr = XNEWVEC (char, vconcat_length (first, args) + 1); |
281e9aa6 | 185 | va_end (args); |
99ee3a8f DD |
186 | |
187 | /* Now copy the individual pieces to the result string. */ | |
281e9aa6 | 188 | va_start (args, first); |
99ee3a8f | 189 | vconcat_copy (newstr, first, args); |
66c94e19 | 190 | if (optr) /* Done before VA_CLOSE so optr stays in scope for K&R C. */ |
99ee3a8f | 191 | free (optr); |
281e9aa6 | 192 | va_end (args); |
99ee3a8f DD |
193 | |
194 | return newstr; | |
195 | } | |
196 | ||
252b5132 | 197 | #ifdef MAIN |
d42dae6c | 198 | #define NULLP (char *)0 |
252b5132 RH |
199 | |
200 | /* Simple little test driver. */ | |
201 | ||
202 | #include <stdio.h> | |
203 | ||
204 | int | |
9334f9c6 | 205 | main (void) |
252b5132 RH |
206 | { |
207 | printf ("\"\" = \"%s\"\n", concat (NULLP)); | |
208 | printf ("\"a\" = \"%s\"\n", concat ("a", NULLP)); | |
209 | printf ("\"ab\" = \"%s\"\n", concat ("a", "b", NULLP)); | |
210 | printf ("\"abc\" = \"%s\"\n", concat ("a", "b", "c", NULLP)); | |
211 | printf ("\"abcd\" = \"%s\"\n", concat ("ab", "cd", NULLP)); | |
212 | printf ("\"abcde\" = \"%s\"\n", concat ("ab", "c", "de", NULLP)); | |
213 | printf ("\"abcdef\" = \"%s\"\n", concat ("", "a", "", "bcd", "ef", NULLP)); | |
214 | return 0; | |
215 | } | |
216 | ||
217 | #endif |