changeset 239:7833df935239

Added xs_pnstrcat() to ease forming of limited size strings.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 21 Dec 2004 15:03:05 +0000
parents 5a390af358fc
children f436e16fa6d9
files src/xs_fileinfo.c src/xs_support.c src/xs_support.h
diffstat 3 files changed, 41 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/xs_fileinfo.c	Tue Dec 21 13:44:03 2004 +0000
+++ b/src/xs_fileinfo.c	Tue Dec 21 15:03:05 2004 +0000
@@ -156,7 +156,7 @@
  GtkWidget *tmpMenuItem, *tmpMenu, *tmpOptionMenu;
  t_xs_tuneinfo *tmpInfo;
  t_xs_stil_subnode *tmpNode;
- gchar tmpStr[32], *tmpS;
+ gchar tmpStr[64];
  gint n;
 
  /* Current implementation leaves old fileinfo window untouched if
@@ -217,20 +217,21 @@
  /* Other menu items */
  for (n = 1; n <= tmpInfo->nsubTunes; n++)
 	{
+	
 	if (xs_fileinfostil)
 		{
+		snprintf(tmpStr, sizeof(tmpStr), "Tune #%i: ", n);
 		tmpNode = &xs_fileinfostil->subTune[n];
 		if (tmpNode->pName)
-			tmpS = tmpNode->pName;
+			xs_pnstrcat(tmpStr, sizeof(tmpStr), tmpNode->pName);
 			else
 		if (tmpNode->pInfo)
-			tmpS = tmpNode->pInfo;
+			xs_pnstrcat(tmpStr, sizeof(tmpStr), tmpNode->pInfo);
 			else
-			tmpS = "---";
-
-		snprintf(tmpStr, sizeof(tmpStr), "Tune #%i: %s", n, tmpS);
-		} else
+			xs_pnstrcat(tmpStr, sizeof(tmpStr), "---");
+		} else {
 		snprintf(tmpStr, sizeof(tmpStr), "Tune #%i", n);
+		}
 
 	tmpMenuItem = gtk_menu_item_new_with_label(tmpStr);
 	gtk_widget_show (tmpMenuItem);
--- a/src/xs_support.c	Tue Dec 21 13:44:03 2004 +0000
+++ b/src/xs_support.c	Tue Dec 21 15:03:05 2004 +0000
@@ -114,6 +114,38 @@
 }
 
 
+/* Concatenate a given string up to given dest size or \n.
+ * If size max is reached, change the end to "..."
+ */
+void xs_pnstrcat(gchar *pDest, size_t iSize, gchar *pStr)
+{
+ size_t i, n;
+ gchar *s, *d;
+ 
+ d = pDest; i = 0;
+ while (*d && (i < iSize)) { i++; d++; }
+ 
+ s = pStr;
+ while (*s && (*s != '\n') && (i < iSize))
+ 	{
+ 	*d = *s;
+ 	d++; s++; i++;
+ 	}
+
+ *d = 0;
+
+ if (i >= iSize)
+ 	{
+ 	i--; d--; n = 3;
+ 	while ((i > 0) && (n > 0))
+		{
+		*d = '.';
+		d--; i--; n--;
+		}
+ 	}
+}
+
+
 /* Locate character in string
  */
 gchar *xs_strrchr(gchar *pcStr, gchar ch)
--- a/src/xs_support.h	Tue Dec 21 13:44:03 2004 +0000
+++ b/src/xs_support.h	Tue Dec 21 15:03:05 2004 +0000
@@ -34,6 +34,7 @@
 gchar	*xs_strncpy(gchar *, gchar *, size_t);
 gint	xs_pstrcpy(gchar **, const gchar *);
 gint	xs_pstrcat(gchar **, const gchar *);
+void	xs_pnstrcat(gchar *, size_t, gchar *);
 gchar	*xs_strrchr(gchar *, gchar);
 inline 	void xs_findnext(gchar *, guint *);
 inline 	void xs_findeol(gchar *, guint *);