su_strdup.c 4.59 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@ingroup su_alloc
 *
 * @CFILE su_strdup.c  Home-based string duplication functions
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 * 
 * @date Created: Wed Jul 19 10:06:14 2000 ppessi
 */

#include "config.h"

#include <string.h>
37
#include "sofia-sip/su_alloc.h"
Pekka Pessi's avatar
Pekka Pessi committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

/** Duplicate a string, allocate memory from @a home.
 *
 * The function su_strdup() duplicates the string @a s.  It allocates @c
 * strlen(s)+1 bytes from @a home, copies the contents of @a s to the newly
 * allocated memory, and returns pointer to the duplicated string.
 *
 * @param home  pointer to memory home
 * @param s     string to be duplicated
 * 
 * @return The function su_strdup() returns pointer to the newly created
 *         string, or @c NULL upon an error.
 */
char *su_strdup(su_home_t *home, char const *s)
{
  if (s) {
    int n = strlen(s);
    char *retval = su_alloc(home, n + 1);
    if (retval)
      strncpy(retval, s, n)[n] = 0;
    return retval;
  }
  return NULL;
}

/**Concate two strings, allocate memory for result from @a home.
 *
65 66 67 68
 * Concatenate the strings @a s1 and @a s2. The @c strlen(s1)+strlen(s2)+1
 * bytes is allocated from @a home, the contents of @a s1 and @a s2 is
 * copied to the newly allocated memory area, and pointer to the
 * concatenated string is returned.
Pekka Pessi's avatar
Pekka Pessi committed
69 70 71 72 73
 *
 * @param home  pointer to memory home
 * @param s1    string to be first string
 * @param s2    string to be first string
 * 
74 75
 * @return Pointer to the newly created string is returned, or @c NULL upon
 * an error.
Pekka Pessi's avatar
Pekka Pessi committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
 */
char *su_strcat(su_home_t *home, char const *s1, char const *s2)
{
  int n1, n2;
  char *retval;

  if (s1 == NULL)
    return su_strdup(home, s2);
  else if (s2 == NULL)
    return su_strdup(home, s1);
    
  n1 = strlen(s1); n2 = strlen(s2);
  retval = su_alloc(home, n1 + n2 + 1);
  if (retval) {
    memcpy(retval, s1, n1);
    memcpy(retval + n1, s2, n2);
    retval[n1 + n2] = '\0';
  }

  return retval;
}

98 99 100 101 102 103 104 105
/**Concate multiple strings, allocate memory for result from @a home.
 *
 * Concatenate the strings in list. The lenght of result is calculate,
 * result is allocated from @a home, the contents of strings is copied to
 * the newly allocated memory arex, and pointer to the concatenated string is
 * returned.
 *
 * @param home  pointer to memory home
106
 * @param ...  NULL-terminated list of strings to be concatenated
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
 * 
 * @return Pointer to the newly created string is returned, or @c NULL upon
 * an error.
 */
char *su_strcat_all(su_home_t *home, ...)
{
  int i, n;
  size_t size = 0;
  va_list va;
  char *s, *retval, *end;

  /* Count number arguments and their size */
  va_start(va, home);
  s = va_arg(va, char *);
  for (n = 0; s; s = va_arg(va, char *), n++)
    size += strlen(s);
  va_end(va);

  retval = su_alloc(home, size + 1);
  if (retval) {
    s = retval;
    end = s + size + 1;

    va_start(va, home);

    for (i = 0; i < n; i++)
133
      s = (char *)memccpy(s, va_arg(va, char const *), '\0', end - s) - 1;
134 135 136 137 138 139 140 141 142 143 144

    va_end(va);

    retval[size] = '\0';
  }

  return retval;
}



Pekka Pessi's avatar
Pekka Pessi committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
/** Duplicate a string with given size, allocate memory from @a home.
 *
 * The function su_strndup() duplicates the string @a s.  It allocates @c n+1
 * bytes from @a home, copies the contents of @a s to the newly allocated
 * memory, and returns pointer to the duplicated string.  The duplicated
 * string is always NUL-terminated.
 *
 * @param home  pointer to memory home
 * @param s     string to be duplicated
 * @param n     size of the resulting string
 * 
 * @return The function su_strndup() returns pointer to the newly created
 *         string, or @c NULL upon an error.
 */
char *su_strndup(su_home_t *home, char const *s, int n)
{
  if (s) {
    char *retval = su_alloc(home, n + 1);
    if (retval)
      strncpy(retval, s, n)[n] = 0;
    return retval;
  }
  return NULL;
}