changeset 117:453ac1293f6a

Fixed severe bug in xs_strmcat() Finalized the STIL-parser, should now work if not for any unfound bug(s).
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 14 Jan 2004 01:55:04 +0000
parents 01496c6f85c3
children 5bddcad25ef7
files src/xs_stil.c src/xs_stil.h
diffstat 2 files changed, 57 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/src/xs_stil.c	Wed Jan 14 01:54:02 2004 +0000
+++ b/src/xs_stil.c	Wed Jan 14 01:55:04 2004 +0000
@@ -45,8 +45,6 @@
  
  /* Allocate memory for new node */
  pResult = (t_xs_stil_node *) g_malloc0(sizeof(t_xs_stil_node));
- if (!pResult) return NULL;
- 
  return pResult;
 }
 
@@ -60,10 +58,8 @@
  	for (i = 0; i < XS_STIL_MAXENTRY; i++)
  		{
  		g_free(pNode->subTune[i].pName);
- 		g_free(pNode->subTune[i].pTitle);
- 		g_free(pNode->subTune[i].pArtist);
  		g_free(pNode->subTune[i].pAuthor);
- 		g_free(pNode->subTune[i].pComment);
+ 		g_free(pNode->subTune[i].pInfo);
  		}
  	
  	g_free(pNode->pcFilename);
@@ -95,6 +91,28 @@
  	}
 }
 
+gint xs_strmcat(gchar **ppResult, gchar *pStr)
+{
+ assert(ppResult);
+
+ /* Check the string pointers */
+ if (!pStr) return -1;
+
+ if (*ppResult != NULL)
+ 	{
+ 	*ppResult = (gchar *) g_realloc(*ppResult, strlen(*ppResult) + strlen(pStr) + 1);
+	if (*ppResult == NULL) return -1;
+	strcat(*ppResult, pStr);
+	} else
+	{
+	*ppResult = (gchar *) g_malloc(strlen(pStr) + 1);
+	if (*ppResult == NULL) return -1;
+	strcpy(*ppResult, pStr);
+	}
+
+ return 0;
+}
+
 
 /*
  * Read database to memory
@@ -102,8 +120,8 @@
 gint xs_stildb_read(gchar *dbFilename)
 {
  FILE *inFile;
- gchar inLine[XS_BUFSIZE];
- guint lineNum, linePos;
+ gchar inLine[XS_BUFSIZE + 10];
+ guint lineNum, linePos, eolPos;
  t_xs_stil_node *tmpNode;
  gboolean isError;
  gint subEntry;
@@ -122,11 +140,10 @@
   
  while (!feof(inFile) && !isError)
  {
- fgets(inLine, sizeof(inLine), inFile);
- linePos = 0;
- xs_findeol(inLine, &linePos);
- inLine[linePos] = 0;
- linePos = 0;
+ fgets(inLine, XS_BUFSIZE, inFile);
+ linePos = eolPos = 0;
+ xs_findeol(inLine, &eolPos);
+ inLine[eolPos] = 0;
  lineNum++;
 
  switch (inLine[0]) {
@@ -192,17 +209,14 @@
  	break;
  
  default:
- 	/* Some other type */
- 	if (strncmp(inLine, "  TITLE:", 8) == 0)
+ 	/* Check if we are parsing an entry */
+ 	if (!tmpNode)
  		{
- 		g_free(tmpNode->subTune[subEntry].pTitle);
- 		tmpNode->subTune[subEntry].pTitle = g_strdup(&inLine[9]);
- 		} else
- 	if (strncmp(inLine, " ARTIST:", 8) == 0)
- 		{
- 		g_free(tmpNode->subTune[subEntry].pArtist);
- 		tmpNode->subTune[subEntry].pArtist = g_strdup(&inLine[9]);
- 		} else
+ 		XSERR("Entry data encountered outside of entry!\n");
+ 		break;
+ 		}
+
+ 	/* Some other type */
  	if (strncmp(inLine, "   NAME:", 8) == 0)
  		{
  		g_free(tmpNode->subTune[subEntry].pName);
@@ -213,12 +227,23 @@
  		g_free(tmpNode->subTune[subEntry].pAuthor);
  		tmpNode->subTune[subEntry].pAuthor = g_strdup(&inLine[9]);
  		} else
- 	if (strncmp(inLine, "COMMENT:", 8) == 0)
+ 	if (strncmp(inLine, "  TITLE:", 8) == 0)
  		{
+ 		inLine[eolPos++] = '\n';
+ 		inLine[eolPos++] = 0;
+ 		xs_strmcat(&(tmpNode->subTune[subEntry].pInfo), &inLine[2]);
+		} else
+ 	if (strncmp(inLine, " ARTIST:", 8) == 0)
+ 		{
+ 		inLine[eolPos++] = '\n';
+ 		inLine[eolPos++] = 0;
+ 		xs_strmcat(&(tmpNode->subTune[subEntry].pInfo), &inLine[1]);
  		} else
+ 	if (strncmp(inLine, "COMMENT:", 8) == 0)
+ 		xs_strmcat(&(tmpNode->subTune[subEntry].pInfo), inLine);
+ 		else
  	if (strncmp(inLine, "        ", 8) == 0)
- 		{
- 		}
+ 		xs_strmcat(&(tmpNode->subTune[subEntry].pInfo), &inLine[8]);
  	break;
  }
  
@@ -341,6 +366,11 @@
  if (!xs_stildb || !xs_stildbi || !xs_cfg.stilDBEnable || !xs_cfg.hvscPath)
  	return NULL;
  
+ /* Remove postfixed directory separator from HVSC-path */
+ tmpFilename = strrchr(xs_cfg.hvscPath, '/');
+ if (tmpFilename && (tmpFilename[1] == 0))
+	tmpFilename[0] = 0;
+ 
  /* Remove HVSC location-prefix from filename */
  tmpFilename = strstr(pcFilename, xs_cfg.hvscPath);
  if (tmpFilename)
@@ -349,7 +379,7 @@
 	} else
 	tmpFilename = pcFilename; 
 
- XSDEBUG("'%s', '%s' -> '%s'\n", pcFilename, xs_cfg.hvscPath, tmpFilename);
+ XSDEBUG("'%s', '%s'\n", xs_cfg.hvscPath, tmpFilename);
  	
  /* Look-up via index using binary search */
  pResult = NULL;
@@ -399,8 +429,6 @@
  	pResult = xs_stildbi[iQNode];
 	}
 
-XSDEBUG("found: %p\n", pResult);
-
  return pResult;
 }
 
--- a/src/xs_stil.h	Wed Jan 14 01:54:02 2004 +0000
+++ b/src/xs_stil.h	Wed Jan 14 01:55:04 2004 +0000
@@ -12,10 +12,8 @@
  */
 typedef struct {
 	gchar	*pName,
-		*pTitle,
-		*pArtist,
 		*pAuthor,
-		*pComment;
+		*pInfo;
 } t_xs_stil_subnode;
 
 typedef struct _t_xs_stil_node {