changeset 206:8b896d461fdb

Trivial fixes and added strncpy replacement.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 15 Dec 2004 10:22:05 +0000
parents de47d797ee33
children 7048944d3bca
files src/xs_support.c src/xs_support.h
diffstat 2 files changed, 57 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- 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 <string.h>
 #include "xs_support.h"
-#include <string.h>
-#include <ctype.h>
 
 
-/*
- * 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;
--- 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 *);