Rev 5191 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 5191 | Rev 6324 | ||
---|---|---|---|
1 | /* Concatenate variable number of strings. |
1 | /* Concatenate variable number of strings. |
2 | Copyright (C) 1991, 1994, 2001, 2011 Free Software Foundation, Inc. |
2 | Copyright (C) 1991, 1994, 2001, 2011, 2013 Free Software Foundation, Inc. |
3 | Written by Fred Fish @ Cygnus Support |
3 | Written by Fred Fish @ Cygnus Support |
4 | 4 | ||
5 | This file is part of the libiberty library. |
5 | This file is part of the libiberty library. |
6 | Libiberty is free software; you can redistribute it and/or |
6 | Libiberty is free software; you can redistribute it and/or |
7 | modify it under the terms of the GNU Library General Public |
7 | modify it under the terms of the GNU Library General Public |
8 | License as published by the Free Software Foundation; either |
8 | License as published by the Free Software Foundation; either |
9 | version 2 of the License, or (at your option) any later version. |
9 | version 2 of the License, or (at your option) any later version. |
10 | 10 | ||
11 | Libiberty is distributed in the hope that it will be useful, |
11 | Libiberty is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | Library General Public License for more details. |
14 | Library General Public License for more details. |
15 | 15 | ||
16 | You should have received a copy of the GNU Library General Public |
16 | You should have received a copy of the GNU Library General Public |
17 | License along with libiberty; see the file COPYING.LIB. If |
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, |
18 | not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, |
19 | Boston, MA 02110-1301, USA. */ |
19 | Boston, MA 02110-1301, USA. */ |
20 | 20 | ||
21 | 21 | ||
22 | /* |
22 | /* |
23 | 23 | ||
24 | @deftypefn Extension char* concat (const char *@var{s1}, const char *@var{s2}, @ |
24 | @deftypefn Extension char* concat (const char *@var{s1}, const char *@var{s2}, @ |
25 | @dots{}, @code{NULL}) |
25 | @dots{}, @code{NULL}) |
26 | 26 | ||
27 | Concatenate zero or more of strings and return the result in freshly |
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} |
28 | @code{xmalloc}ed memory. The argument list is terminated by the first |
30 | pointer encountered. Pointers to empty strings are ignored. |
29 | @code{NULL} pointer encountered. Pointers to empty strings are ignored. |
31 | 30 | ||
32 | @end deftypefn |
31 | @end deftypefn |
33 | - | ||
34 | NOTES |
- | |
35 | - | ||
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 |
- | |
43 | malloc returns. |
- | |
44 | 32 | ||
45 | */ |
33 | */ |
46 | 34 | ||
47 | 35 | ||
48 | #ifdef HAVE_CONFIG_H |
36 | #ifdef HAVE_CONFIG_H |
49 | #include "config.h" |
37 | #include "config.h" |
50 | #endif |
38 | #endif |
51 | #include "ansidecl.h" |
39 | #include "ansidecl.h" |
52 | #include "libiberty.h" |
40 | #include "libiberty.h" |
53 | #include |
41 | #include |
54 | 42 | ||
55 | #include |
43 | #include |
56 | 44 | ||
57 | # if HAVE_STRING_H |
45 | # if HAVE_STRING_H |
58 | # include |
46 | # include |
59 | # else |
47 | # else |
60 | # if HAVE_STRINGS_H |
48 | # if HAVE_STRINGS_H |
61 | # include |
49 | # include |
62 | # endif |
50 | # endif |
63 | # endif |
51 | # endif |
64 | 52 | ||
65 | #if HAVE_STDLIB_H |
53 | #if HAVE_STDLIB_H |
66 | #include |
54 | #include |
67 | #endif |
55 | #endif |
68 | 56 | ||
69 | static inline unsigned long vconcat_length (const char *, va_list); |
57 | static inline unsigned long vconcat_length (const char *, va_list); |
70 | static inline unsigned long |
58 | static inline unsigned long |
71 | vconcat_length (const char *first, va_list args) |
59 | vconcat_length (const char *first, va_list args) |
72 | { |
60 | { |
73 | unsigned long length = 0; |
61 | unsigned long length = 0; |
74 | const char *arg; |
62 | const char *arg; |
75 | 63 | ||
76 | for (arg = first; arg ; arg = va_arg (args, const char *)) |
64 | for (arg = first; arg ; arg = va_arg (args, const char *)) |
77 | length += strlen (arg); |
65 | length += strlen (arg); |
78 | 66 | ||
79 | return length; |
67 | return length; |
80 | } |
68 | } |
81 | 69 | ||
82 | static inline char * |
70 | static inline char * |
83 | vconcat_copy (char *dst, const char *first, va_list args) |
71 | vconcat_copy (char *dst, const char *first, va_list args) |
84 | { |
72 | { |
85 | char *end = dst; |
73 | char *end = dst; |
86 | const char *arg; |
74 | const char *arg; |
87 | 75 | ||
88 | for (arg = first; arg ; arg = va_arg (args, const char *)) |
76 | for (arg = first; arg ; arg = va_arg (args, const char *)) |
89 | { |
77 | { |
90 | unsigned long length = strlen (arg); |
78 | unsigned long length = strlen (arg); |
91 | memcpy (end, arg, length); |
79 | memcpy (end, arg, length); |
92 | end += length; |
80 | end += length; |
93 | } |
81 | } |
94 | *end = '\000'; |
82 | *end = '\000'; |
95 | 83 | ||
96 | return dst; |
84 | return dst; |
97 | } |
85 | } |
98 | 86 | ||
99 | /* @undocumented concat_length */ |
87 | /* @undocumented concat_length */ |
100 | 88 | ||
101 | unsigned long |
89 | unsigned long |
102 | concat_length (const char *first, ...) |
90 | concat_length (const char *first, ...) |
103 | { |
91 | { |
104 | unsigned long length; |
92 | unsigned long length; |
- | 93 | va_list args; |
|
105 | 94 | ||
106 | VA_OPEN (args, first); |
- | |
107 | VA_FIXEDARG (args, const char *, first); |
95 | va_start (args, first); |
108 | length = vconcat_length (first, args); |
96 | length = vconcat_length (first, args); |
109 | VA_CLOSE (args); |
97 | va_end (args); |
110 | 98 | ||
111 | return length; |
99 | return length; |
112 | } |
100 | } |
113 | 101 | ||
114 | /* @undocumented concat_copy */ |
102 | /* @undocumented concat_copy */ |
115 | 103 | ||
116 | char * |
104 | char * |
117 | concat_copy (char *dst, const char *first, ...) |
105 | concat_copy (char *dst, const char *first, ...) |
118 | { |
106 | { |
119 | char *save_dst; |
107 | char *save_dst; |
- | 108 | va_list args; |
|
120 | 109 | ||
121 | VA_OPEN (args, first); |
- | |
122 | VA_FIXEDARG (args, char *, dst); |
- | |
123 | VA_FIXEDARG (args, const char *, first); |
110 | va_start (args, first); |
124 | vconcat_copy (dst, first, args); |
111 | vconcat_copy (dst, first, args); |
125 | save_dst = dst; /* With K&R C, dst goes out of scope here. */ |
112 | save_dst = dst; /* With K&R C, dst goes out of scope here. */ |
126 | VA_CLOSE (args); |
113 | va_end (args); |
127 | 114 | ||
128 | return save_dst; |
115 | return save_dst; |
129 | } |
116 | } |
130 | 117 | ||
131 | #ifdef __cplusplus |
118 | #ifdef __cplusplus |
132 | extern "C" { |
119 | extern "C" { |
133 | #endif /* __cplusplus */ |
120 | #endif /* __cplusplus */ |
134 | char *libiberty_concat_ptr; |
121 | char *libiberty_concat_ptr; |
135 | #ifdef __cplusplus |
122 | #ifdef __cplusplus |
136 | } |
123 | } |
137 | #endif /* __cplusplus */ |
124 | #endif /* __cplusplus */ |
138 | 125 | ||
139 | /* @undocumented concat_copy2 */ |
126 | /* @undocumented concat_copy2 */ |
140 | 127 | ||
141 | char * |
128 | char * |
142 | concat_copy2 (const char *first, ...) |
129 | concat_copy2 (const char *first, ...) |
143 | { |
130 | { |
144 | VA_OPEN (args, first); |
131 | va_list args; |
145 | VA_FIXEDARG (args, const char *, first); |
132 | va_start (args, first); |
146 | vconcat_copy (libiberty_concat_ptr, first, args); |
133 | vconcat_copy (libiberty_concat_ptr, first, args); |
147 | VA_CLOSE (args); |
134 | va_end (args); |
148 | 135 | ||
149 | return libiberty_concat_ptr; |
136 | return libiberty_concat_ptr; |
150 | } |
137 | } |
151 | 138 | ||
152 | char * |
139 | char * |
153 | concat (const char *first, ...) |
140 | concat (const char *first, ...) |
154 | { |
141 | { |
155 | char *newstr; |
142 | char *newstr; |
- | 143 | va_list args; |
|
156 | 144 | ||
157 | /* First compute the size of the result and get sufficient memory. */ |
145 | /* First compute the size of the result and get sufficient memory. */ |
158 | VA_OPEN (args, first); |
- | |
159 | VA_FIXEDARG (args, const char *, first); |
146 | va_start (args, first); |
160 | newstr = XNEWVEC (char, vconcat_length (first, args) + 1); |
147 | newstr = XNEWVEC (char, vconcat_length (first, args) + 1); |
161 | VA_CLOSE (args); |
148 | va_end (args); |
162 | 149 | ||
163 | /* Now copy the individual pieces to the result string. */ |
- | |
164 | VA_OPEN (args, first); |
150 | /* Now copy the individual pieces to the result string. */ |
165 | VA_FIXEDARG (args, const char *, first); |
151 | va_start (args, first); |
166 | vconcat_copy (newstr, first, args); |
152 | vconcat_copy (newstr, first, args); |
167 | VA_CLOSE (args); |
153 | va_end (args); |
168 | 154 | ||
169 | return newstr; |
155 | return newstr; |
170 | } |
156 | } |
171 | 157 | ||
172 | /* |
158 | /* |
173 | 159 | ||
174 | @deftypefn Extension char* reconcat (char *@var{optr}, const char *@var{s1}, @ |
160 | @deftypefn Extension char* reconcat (char *@var{optr}, const char *@var{s1}, @ |
175 | @dots{}, @code{NULL}) |
161 | @dots{}, @code{NULL}) |
176 | 162 | ||
177 | Same as @code{concat}, except that if @var{optr} is not @code{NULL} it |
163 | 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 |
164 | 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 |
165 | when you're extending an existing string or building up a string in a |
180 | loop: |
166 | loop: |
181 | 167 | ||
182 | @example |
168 | @example |
183 | str = reconcat (str, "pre-", str, NULL); |
169 | str = reconcat (str, "pre-", str, NULL); |
184 | @end example |
170 | @end example |
185 | 171 | ||
186 | @end deftypefn |
172 | @end deftypefn |
187 | 173 | ||
188 | */ |
174 | */ |
189 | 175 | ||
190 | char * |
176 | char * |
191 | reconcat (char *optr, const char *first, ...) |
177 | reconcat (char *optr, const char *first, ...) |
192 | { |
178 | { |
193 | char *newstr; |
179 | char *newstr; |
- | 180 | va_list args; |
|
194 | 181 | ||
195 | /* First compute the size of the result and get sufficient memory. */ |
182 | /* 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); |
183 | va_start (args, first); |
199 | newstr = XNEWVEC (char, vconcat_length (first, args) + 1); |
184 | newstr = XNEWVEC (char, vconcat_length (first, args) + 1); |
200 | VA_CLOSE (args); |
185 | va_end (args); |
201 | 186 | ||
202 | /* Now copy the individual pieces to the result string. */ |
- | |
203 | VA_OPEN (args, first); |
- | |
204 | VA_FIXEDARG (args, char *, optr); |
187 | /* Now copy the individual pieces to the result string. */ |
205 | VA_FIXEDARG (args, const char *, first); |
188 | va_start (args, first); |
206 | vconcat_copy (newstr, first, args); |
189 | vconcat_copy (newstr, first, args); |
207 | if (optr) /* Done before VA_CLOSE so optr stays in scope for K&R C. */ |
190 | if (optr) /* Done before VA_CLOSE so optr stays in scope for K&R C. */ |
208 | free (optr); |
191 | free (optr); |
209 | VA_CLOSE (args); |
192 | va_end (args); |
210 | 193 | ||
211 | return newstr; |
194 | return newstr; |
212 | } |
195 | } |
213 | 196 | ||
214 | #ifdef MAIN |
197 | #ifdef MAIN |
215 | #define NULLP (char *)0 |
198 | #define NULLP (char *)0 |
216 | 199 | ||
217 | /* Simple little test driver. */ |
200 | /* Simple little test driver. */ |
218 | 201 | ||
219 | #include |
202 | #include |
220 | 203 | ||
221 | int |
204 | int |
222 | main (void) |
205 | main (void) |
223 | { |
206 | { |
224 | printf ("\"\" = \"%s\"\n", concat (NULLP)); |
207 | printf ("\"\" = \"%s\"\n", concat (NULLP)); |
225 | printf ("\"a\" = \"%s\"\n", concat ("a", NULLP)); |
208 | printf ("\"a\" = \"%s\"\n", concat ("a", NULLP)); |
226 | printf ("\"ab\" = \"%s\"\n", concat ("a", "b", NULLP)); |
209 | printf ("\"ab\" = \"%s\"\n", concat ("a", "b", NULLP)); |
227 | printf ("\"abc\" = \"%s\"\n", concat ("a", "b", "c", NULLP)); |
210 | printf ("\"abc\" = \"%s\"\n", concat ("a", "b", "c", NULLP)); |
228 | printf ("\"abcd\" = \"%s\"\n", concat ("ab", "cd", NULLP)); |
211 | printf ("\"abcd\" = \"%s\"\n", concat ("ab", "cd", NULLP)); |
229 | printf ("\"abcde\" = \"%s\"\n", concat ("ab", "c", "de", NULLP)); |
212 | printf ("\"abcde\" = \"%s\"\n", concat ("ab", "c", "de", NULLP)); |
230 | printf ("\"abcdef\" = \"%s\"\n", concat ("", "a", "", "bcd", "ef", NULLP)); |
213 | printf ("\"abcdef\" = \"%s\"\n", concat ("", "a", "", "bcd", "ef", NULLP)); |
231 | return 0; |
214 | return 0; |
232 | } |
215 | } |
233 | 216 | ||
234 | #endif |
217 | #endif |