su_strdup.c 3.6 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
 * 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
Pekka Pessi's avatar
Pekka Pessi committed
32
 * @date Last modified: Wed Jul 20 20:35:58 2005 kaiv
Pekka Pessi's avatar
Pekka Pessi committed
33 34 35 36 37 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
 */

#include "config.h"

#include <string.h>
#include "su_alloc.h"

/** 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.
 *
 * The function su_strcat() concatenates the strings @a s1 and @a s2. It
 * allocates @c strlen(s1)+strlen(s2)+1 bytes from @a home, copies the
 * contents of @a s1 and @a s2 to the newly allocated memory, and returns
 * pointer to the concatenated string.
 *
 * @param home  pointer to memory home
 * @param s1    string to be first string
 * @param s2    string to be first string
 * 
 * @return The function su_strcat() returns pointer to the newly created
 *         string, or @c NULL upon an error.
 */
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;
}

/** 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;
}