# HG changeset patch # User Matti Hamalainen # Date 1103106125 0 # Node ID 8b896d461fdb142ef4b4d8b716cff048e8f6e8d9 # Parent de47d797ee332bcae8d99ba9e9611ef9037d03c2 Trivial fixes and added strncpy replacement. diff -r de47d797ee33 -r 8b896d461fdb src/xs_support.c --- a/src/xs_support.c Wed Dec 15 10:18:00 2004 +0000 +++ b/src/xs_support.c Wed Dec 15 10:22:05 2004 +0000 @@ -20,46 +20,80 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +#include #include "xs_support.h" -#include -#include -/* - * Utility routines +/* Copy a string */ -gint xs_strcalloc(gchar **ppcResult, const gchar *pcStr) +gchar *xs_strncpy(gchar *pDest, gchar *pSource, size_t n) { - assert(ppcResult); - assert(pcStr); + gchar *s, *d; + size_t i; - /* Allocate memory for destination */ - *ppcResult = (gchar *) g_realloc(*ppcResult, strlen(pcStr) + 1); - if (*ppcResult == NULL) return -1; + /* Check the string pointers */ + if (!pSource || !pDest) return pDest; /* Copy to the destination */ - strcpy(*ppcResult, pcStr); + i = n; s = pSource; d = pDest; + while (*s && (i > 0)) { *(d++) = *(s++); i--; } + + /* Fill rest of space with zeros */ + while (i > 0) { *(d++) = 0; i--; } + + /* Ensure that last is always zero */ + pDest[n - 1] = 0; + + return pDest; +} + + +/* Copy a given string over in *ppResult. + */ +gint xs_pstrcpy(gchar **ppResult, const gchar *pStr) +{ + /* Check the string pointers */ + if (!ppResult || !pStr) return -1; + + /* Allocate memory for destination */ + if (*ppResult) g_free(*ppResult); + *ppResult = (gchar *) g_malloc(strlen(pStr) + 1); + if (!*ppResult) return -2; + + /* Copy to the destination */ + strcpy(*ppResult, pStr); return 0; } -int xs_strcat(gchar **ppcResult, const gchar *pcStr) +/* Concatenates a given string into string pointed by *ppResult. + */ +gint th_pstrcat(t_char **ppResult, const gchar *pStr) { - assert(ppcResult); - assert(pcStr); - - /* Re-allocate memory for destination */ - *ppcResult = (gchar *) g_realloc(*ppcResult, strlen(*ppcResult) + strlen(pcStr) + 1); - if (*ppcResult == NULL) return -1; + /* Check the string pointers */ + if (!ppResult || !pStr) return -1; + + if (*ppResult != NULL) + { + *ppResult = g_realloc(*ppResult, strlen(*ppResult) + strlen(pStr) + 1); + if (*ppResult == NULL) return -1; - /* Cat to the destination */ - strcat(*ppcResult, pcStr); + strcat(*ppResult, pStr); + } else + { + *ppResult = g_malloc(strlen(pStr) + 1); + if (*ppResult == NULL) return -1; + + strcpy(*ppResult, pStr); + } return 0; } +/* Locate character in string + */ gchar *xs_strrchr(gchar *pcStr, gchar ch) { gchar *lastPos = NULL; diff -r de47d797ee33 -r 8b896d461fdb src/xs_support.h --- a/src/xs_support.h Wed Dec 15 10:18:00 2004 +0000 +++ b/src/xs_support.h Wed Dec 15 10:22:05 2004 +0000 @@ -10,8 +10,9 @@ /* * Functions */ -gint xs_strcalloc(gchar **, const gchar *); -gint xs_strcat(gchar **, const gchar *); +gchar xs_strncpy(gchar *, gchar *, size_t); +gint xs_pstrcpy(gchar **, const gchar *); +gint xs_pstrcat(gchar **, const gchar *); gchar *xs_strrchr(gchar *, gchar); inline void xs_findnext(gchar *, guint *); inline void xs_findeol(gchar *, guint *);