diff --git a/common/str.c b/common/str.c index e6976ca9ef..fc737ccd9d 100644 --- a/common/str.c +++ b/common/str.c @@ -34,6 +34,8 @@ #include /* for strncasecmp() and strcasecmp() */ #endif +#include /* get the va_* routines */ + #include "nut_stdint.h" #include "str.h" @@ -628,6 +630,43 @@ int str_ends_with(const char *s, const char *suff) { return (slen >= sufflen) && (!memcmp(s + slen - sufflen, suff, sufflen)); } +/* Based on code by "mmdemirbas" posted "Jul 9 '12 at 11:41" to forum page + * http://stackoverflow.com/questions/8465006/how-to-concatenate-2-strings-in-c + * This concatenates the given number of strings into one freshly allocated + * heap object; NOTE that it is up to the caller to free the object afterwards. + */ +char * str_concat(size_t count, ...) +{ + va_list ap; + size_t i, len, null_pos; + char* merged = NULL; + + /* Find required length to store merged string */ + va_start(ap, count); + len = 1; /* room for '\0' in the end */ + for(i=0 ; i # include @@ -654,4 +693,4 @@ float strtof(const char *nptr, char **endptr) return (float)d; } -#endif +#endif /* HAVE_STRTOF */ diff --git a/include/str.h b/include/str.h index 44cd3a667d..26ac16b540 100644 --- a/include/str.h +++ b/include/str.h @@ -141,6 +141,10 @@ char *strsep(char **stringp, const char *delim); #define HAVE_STRSEP 1 #endif +/* Concatenates "count" strings into a dynamically allocated object which + * the caller can use and must free() later on */ +char * str_concat(size_t count, ...); + #ifdef __cplusplus /* *INDENT-OFF* */ }