| iMatix home page
| << | < | > | >>
SFL Logo SFL
Version 2.11

 

xstrcpy

#include "sflstr.h"
char *
xstrcpy (
    char *dest,
    const char *src, ...)

Synopsis

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 there was too little memory to allocate the new string. We can't define macros with variable argument lists, so we pass the file and line number through two globals, xstrcpy_file and xstrcpy_line, which are reset to empty values after each call to xstrcpy.

Source Code - (sflstr.c)

{
    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     */

        /*  Allocate by going directly to mem_alloc_ function                */
        dest = mem alloc  (NULL, dest_size, xstrcpy_file, xstrcpy_line);
        xstrcpy_file = "";
        xstrcpy_line = 0;
        if (dest == 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);
}

| << | < | > | >> iMatix Copyright © 1996-2000 iMatix Corporation