diff src/xs_stil.c @ 109:578b71b62eeb

Fileinfo now basically working, STIL COMMENT-fields are not yet parsed or shown. Some bugs probably remain.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 12 Jan 2004 01:45:04 +0000
parents fe83646e6baa
children 582d32db824b
line wrap: on
line diff
--- a/src/xs_stil.c	Mon Jan 12 00:58:01 2004 +0000
+++ b/src/xs_stil.c	Mon Jan 12 01:45:04 2004 +0000
@@ -57,11 +57,12 @@
  
  if (pNode)
  	{
- 	for (i = 0; i < pNode->nsubTunes; i++)
+ 	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);
  		}
  	
@@ -96,72 +97,6 @@
 
 
 /*
- * Get STIL information from database
- */
-t_xs_stil_node * xs_stildb_get(gchar *pcFilename)
-{
- gint iStartNode, iEndNode, iQNode, r, i;
- gboolean iFound;
- t_xs_stil_node *pResult;
-
- /* Check the database pointers */
- if (!xs_stildb || !xs_stildbi)
- 	return NULL;
- 	
- /* Look-up via index using binary search */
- pResult = NULL;
- iStartNode = 0;
- iEndNode = (xs_stildbn - 1);
- iQNode = (iEndNode / 2);
- iFound = FALSE;
-
- while ((!iFound) && ((iEndNode - iStartNode) > 128))
- 	{
- 	r = strcmp(pcFilename, xs_stildbi[iQNode]->pcFilename);
- 	if (r < 0)
- 		{
- 		/* Hash was in the <- LEFT side */
- 		iEndNode = iQNode;
- 		iQNode = iStartNode + ((iEndNode - iStartNode) / 2);
- 		} else
- 	if (r > 0)
- 		{
- 		/* Hash was in the RIGHT -> side */
- 		iStartNode = iQNode;
- 		iQNode = iStartNode + ((iEndNode - iStartNode) / 2);
- 		} else
- 		iFound = TRUE;
- 	}
-
- /* If not found already */
- if (!iFound)
- 	{
-	/* Search the are linearly */
-	iFound = FALSE;
-	i = iStartNode;
-	while ((i <= iEndNode) && (!iFound))
-		{
-		if (strcmp(pcFilename, xs_stildbi[iQNode]->pcFilename) == 0)
-			iFound = TRUE;
-			else
-			i++;
-		}
-	
-	/* Check the result */
-	if (iFound)
-		pResult = xs_stildbi[i];
-
- 	} else {
-	/* Found via binary search */
- 	pResult = xs_stildbi[iQNode];
-	}
-
-
- return pResult;
-}
-
-
-/*
  * Read database to memory
  */
 gint xs_stildb_read(gchar *dbFilename)
@@ -169,8 +104,10 @@
  FILE *inFile;
  gchar inLine[XS_BUFSIZE];
  guint lineNum, linePos;
- gboolean iOK;
  t_xs_stil_node *tmpNode;
+ t_xs_stil_subnode *qNode;
+ gboolean isError;
+ gint subEntry, i;
  
  /* Try to open the file */
  if ((inFile = fopen(dbFilename, "ra")) == NULL)
@@ -181,34 +118,117 @@
  
  /* Read and parse the data */
  lineNum = 0;
-
- while (!feof(inFile))
+ isError = FALSE;
+ tmpNode = NULL;
+  
+ while (!feof(inFile) && !isError)
  {
  fgets(inLine, sizeof(inLine), inFile);
+ linePos = 0;
+ xs_findeol(inLine, &linePos);
+ inLine[linePos] = 0;
+ linePos = 0;
  lineNum++;
 
-#if 0
  switch (inLine[0]) {
  case '/':
- 	/* A new entry */
- 	
- 	isEntry = TRUE;
+ 	/* Check if we are already parsing entry */
+ 	if (tmpNode)
+ 		{
+ 		XSERR("New entry ('%s') before end of current ('%s')! Possibly malformed STIL-file!\n",
+ 			inLine, tmpNode->pcFilename);
+
+ 		xs_stildb_node_free(tmpNode);
+ 		}
+ 
+ 	/* A new node */
+ 	tmpNode = xs_stildb_node_new();
+ 	if (tmpNode)
+ 		{
+ 		/* Initialize the node */
+ 		tmpNode->pcFilename = g_strdup(inLine);
+	 	subEntry = 0;
+	 	} else
+ 		{
+ 		/* Allocation failed */
+ 		XSERR("Could not allocate new STILdb-node for '%s'!\n", inLine);
+ 		isError = TRUE;
+ 		}
 	break;
 
  case '(':
- 	subEntry = 
+ 	/* A new sub-entry */
+ 	linePos++;
+ 	if (inLine[linePos] == '#')
+ 		{
+ 		linePos++;
+ 		if (inLine[linePos])
+ 			{
+		 	xs_findnum(inLine, &linePos);
+		 	inLine[linePos] = 0;
+	 		subEntry = atol(&inLine[2]);
+
+		 	/* Sanity check */
+		 	if ((subEntry < 1) || (subEntry >= XS_STIL_MAXENTRY))
+ 				{
+ 				XSERR("Number of subEntry (%i) for '%s' is invalid\n", subEntry, tmpNode->pcFilename);
+ 				subEntry = 0;
+ 				}
+	 		}
+	 	}
+ 	
+ 	break;
+
+ case 0:
+ case '#':
+ case '\n':
+ case '\r':
+ 	/* End of entry/field */
+ 	if (tmpNode)
+ 		{
+ 		/* Insert to database */
+ 		xs_stildb_node_insert(tmpNode);
+		tmpNode = NULL;
+		}
  	break;
  
  default:
- 	/* End of entry */
- 	isEntry = FALSE;
+ 	/* Some other type */
+ 	if (strncmp(inLine, "  TITLE:", 8) == 0)
+ 		{
+ 		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
+ 	if (strncmp(inLine, "   NAME:", 8) == 0)
+ 		{
+ 		g_free(tmpNode->subTune[subEntry].pName);
+ 		tmpNode->subTune[subEntry].pName = g_strdup(&inLine[9]);
+ 		} else
+ 	if (strncmp(inLine, " AUTHOR:", 8) == 0)
+ 		{
+ 		g_free(tmpNode->subTune[subEntry].pAuthor);
+ 		tmpNode->subTune[subEntry].pAuthor = g_strdup(&inLine[9]);
+ 		} else
+ 	if (strncmp(inLine, "COMMENT:", 8) == 0)
+ 		{
+ 		} else
+ 	if (strncmp(inLine, "        ", 8) == 0)
+ 		{
+ 		}
  	break;
- 
  }
-#endif
  
  } /* while */
 
+ /* Check if there is one remaining node */
+ if (tmpNode)
+	xs_stildb_node_free(tmpNode);
+
  /* Close the file */  
  fclose(inFile);
  return 0;
@@ -309,11 +329,79 @@
 
 
 /*
- * Get STIL information
+ * Get STIL information from database
  */
 t_xs_stil_node * xs_stil_get(gchar *pcFilename)
 {
+ gint iStartNode, iEndNode, iQNode, r, i;
+ gchar *tmpFilename;
+ gboolean iFound;
  t_xs_stil_node *pResult;
 
+ /* Check the database pointers */
+ if (!xs_stildb || !xs_stildbi || !xs_cfg.stilDBEnable || !xs_cfg.hvscPath)
+ 	return NULL;
+ 
+ /* Remove HVSC location-prefix from filename */
+ tmpFilename = strstr(pcFilename, xs_cfg.hvscPath);
+ if (tmpFilename)
+ 	{
+	tmpFilename += strlen(xs_cfg.hvscPath);
+	} else
+	tmpFilename = pcFilename; 
+
+ XSDEBUG("'%s', '%s' -> '%s'\n", pcFilename, xs_cfg.hvscPath, tmpFilename);
+ 	
+ /* Look-up via index using binary search */
+ pResult = NULL;
+ iStartNode = 0;
+ iEndNode = (xs_stildbn - 1);
+ iQNode = (iEndNode / 2);
+ iFound = FALSE;
+
+ while ((!iFound) && ((iEndNode - iStartNode) > 128))
+ 	{
+ 	r = strcmp(tmpFilename, xs_stildbi[iQNode]->pcFilename);
+ 	if (r < 0)
+ 		{
+ 		/* Hash was in the <- LEFT side */
+ 		iEndNode = iQNode;
+ 		iQNode = iStartNode + ((iEndNode - iStartNode) / 2);
+ 		} else
+ 	if (r > 0)
+ 		{
+ 		/* Hash was in the RIGHT -> side */
+ 		iStartNode = iQNode;
+ 		iQNode = iStartNode + ((iEndNode - iStartNode) / 2);
+ 		} else
+ 		iFound = TRUE;
+ 	}
+
+ /* If not found already */
+ if (!iFound)
+ 	{
+	/* Search the are linearly */
+	iFound = FALSE;
+	i = iStartNode;
+	while ((i <= iEndNode) && (!iFound))
+		{
+		if (strcmp(tmpFilename, xs_stildbi[i]->pcFilename) == 0)
+			iFound = TRUE;
+			else
+			i++;
+		}
+	
+	/* Check the result */
+	if (iFound)
+		pResult = xs_stildbi[i];
+
+ 	} else {
+	/* Found via binary search */
+ 	pResult = xs_stildbi[iQNode];
+	}
+
+XSDEBUG("found: %p\n", pResult);
+
  return pResult;
 }
+