| iMatix home page | << | < | > | >> |
![]() Version 1.91 |
#include "sflstr.h" char * xstrcpy ( char *dest, const char *src, ...)
Concatenates multiple strings into a single result. Eg. xstrcpy (buffer, "A", "B", NULL) stores "AB" in buffer. Returns dest. Any existing contents of dest are cleared. If the dest buffer is NULL, allocates a new buffer with the required length and returns that. The buffer is allocated using mem_alloc(), and should eventually be freed using mem_free() or mem_strfree(). Returns NULL if the was insufficient memory to allocate the new string.
{ const char *src_ptr; va_list va; size_t dest_size; /* Size of concatenated strings */ /* Allocate new buffer if necessary */ if (dest == NULL) { va_start (va, src); /* Start variable args processing */ src_ptr = src; dest_size = 1; /* Allow for trailing null char */ while (src_ptr) { dest_size += strlen (src_ptr); src_ptr = va_arg (va, char *); } va_end (va); /* End variable args processing */ if ((dest = mem_alloc (dest_size)) == NULL) return (NULL); /* Not enough memory */ } /* Now copy strings into destination buffer */ va_start (va, src); /* Start variable args processing */ src_ptr = src; dest [0] = '\0'; while (src_ptr) { strcat (dest, src_ptr); src_ptr = va_arg (va, char *); } va_end (va); /* End variable args processing */ return (dest); }
| << | < | > | >> |
![]() |