| 1 |
/* concatn.c: concatenate an arbitrary number of strings. |
| 2 |
|
| 3 |
Copyright 1993, 1995, 2008, 2009 Karl Berry. |
| 4 |
Copyright 1999, 2005 Olaf Weber. |
| 5 |
|
| 6 |
This library is free software; you can redistribute it and/or |
| 7 |
modify it under the terms of the GNU Lesser General Public |
| 8 |
License as published by the Free Software Foundation; either |
| 9 |
version 2.1 of the License, or (at your option) any later version. |
| 10 |
|
| 11 |
This library 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 |
Lesser General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU Lesser General Public License |
| 17 |
along with this library; if not, see <http://www.gnu.org/licenses/>. */ |
| 18 |
|
| 19 |
#include <kpathsea/config.h> |
| 20 |
|
| 21 |
#include <kpathsea/concatn.h> |
| 22 |
|
| 23 |
|
| 24 |
/* OK, it would be epsilon more efficient to compute the total length |
| 25 |
and then do the copying ourselves, but I doubt it matters in reality. */ |
| 26 |
|
| 27 |
string |
| 28 |
concatn (const_string str1, ...) |
| 29 |
{ |
| 30 |
string arg; |
| 31 |
string ret; |
| 32 |
va_list ap; |
| 33 |
|
| 34 |
if (!str1) |
| 35 |
return NULL; |
| 36 |
|
| 37 |
ret = xstrdup (str1); |
| 38 |
|
| 39 |
va_start (ap, str1); |
| 40 |
while ((arg = va_arg (ap, string)) != NULL) |
| 41 |
{ |
| 42 |
string temp = concat (ret, arg); |
| 43 |
free (ret); |
| 44 |
ret = temp; |
| 45 |
} |
| 46 |
va_end (ap); |
| 47 |
|
| 48 |
return ret; |
| 49 |
} |
| 50 |
|
| 51 |
#ifdef TEST |
| 52 |
int |
| 53 |
main () |
| 54 |
{ |
| 55 |
printf ("null = \"%s\"\n", concatn (NULL)); |
| 56 |
printf ("\"a\" = \"%s\"\n", concatn ("a", NULL)); |
| 57 |
printf ("\"ab\" = \"%s\"\n", concatn ("a", "b", NULL)); |
| 58 |
printf ("\"abc\" = \"%s\"\n", concatn ("a", "b", "c", NULL)); |
| 59 |
printf ("\"abcd\" = \"%s\"\n", concatn ("ab", "cd", NULL)); |
| 60 |
printf ("\"abcde\" = \"%s\"\n", concatn ("ab", "c", "de", NULL)); |
| 61 |
printf ("\"abcdef\" = \"%s\"\n", concatn ("", "a", "", "bcd", "ef", NULL)); |
| 62 |
return 0; |
| 63 |
} |
| 64 |
|
| 65 |
#endif /* TEST */ |
| 66 |
|
| 67 |
|
| 68 |
/* |
| 69 |
Local variables: |
| 70 |
standalone-compile-command: "gcc -posix -g -I. -I.. -DTEST concatn.c kpathsea.a" |
| 71 |
End: |
| 72 |
*/ |