changeset 920:560cdf7f1fec

Fix a silly bug in the SLDB parser caused by recent changes. Cleaned up the code a bit as well.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 10 Nov 2012 19:06:55 +0200
parents 8ca7f33635fc
children 03c3b7696187
files src/xs_length.c
diffstat 1 files changed, 59 insertions(+), 72 deletions(-) [+]
line wrap: on
line diff
--- a/src/xs_length.c	Sat Nov 10 15:18:11 2012 +0200
+++ b/src/xs_length.c	Sat Nov 10 19:06:55 2012 +0200
@@ -105,14 +105,14 @@
  */
 XSSLDBNode * xs_sldb_read_entry(gchar *inLine)
 {
-    size_t linePos;
+    XSSLDBNode *tmnode = NULL;
+    size_t linePos, tmpLen, savePos;
+    gboolean isOK;
     gint i;
-    gboolean isOK;
-    XSSLDBNode *tmnode;
 
     /* Allocate new node */
     tmnode = (XSSLDBNode *) g_malloc0(sizeof(XSSLDBNode));
-    if (!tmnode)
+    if (tmnode == NULL)
     {
         xs_error("Error allocating new node. Fatal error.\n");
         return NULL;
@@ -128,78 +128,58 @@
     }
         
     /* Get playtimes */
-    if (inLine[linePos] != 0)
+    xs_findnext(inLine, &linePos);
+    if (inLine[linePos] != '=')
     {
-        if (inLine[linePos] != '=')
-        {
-            xs_error("'=' expected on column #%d.\n", linePos);
-            xs_sldb_node_free(tmnode);
-            return NULL;
-        }
-        else
-        {
-            size_t tmpLen, savePos;
-            
-            /* First playtime is after '=' */
-            savePos = ++linePos;
-            tmpLen = strlen(inLine);
-                        
-            /* Get number of sub-tune lengths */                        
-            isOK = TRUE;
-            while (linePos < tmpLen && isOK)
-            {
-                xs_findnext(inLine, &linePos);
+        xs_error("'=' expected on column #%d.\n", linePos);
+        goto error;
+    }
+
+    /* First playtime is after '=' */
+    savePos = ++linePos;
+    tmpLen = strlen(inLine);
 
-                if (xs_sldb_gettime(inLine, &linePos) >= 0)
-                    tmnode->nlengths++;
-                else
-                    isOK = FALSE;
-            }
-            
-            /* Allocate memory for lengths */
-            if (tmnode->nlengths > 0)
-            {
-                tmnode->lengths = (gint *) g_malloc0(tmnode->nlengths * sizeof(gint));
-                if (!tmnode->lengths)
-                {
-                    xs_error("Could not allocate memory for node.\n");
-                    xs_sldb_node_free(tmnode);
-                    return NULL;
-                }
-            }
-            else
-            {
-                xs_sldb_node_free(tmnode);
-                return NULL;
-            }
-            
-            /* Read lengths in */
-            for (i = 0, linePos = savePos, isOK = TRUE; 
-                linePos < tmpLen && i < tmnode->nlengths && isOK; i++)
-            {
-                gint l;
-                
-                xs_findnext(inLine, &linePos);
+    /* Get number of sub-tune lengths */
+    isOK = TRUE;
+    while (linePos < tmpLen && isOK)
+    {
+        xs_findnext(inLine, &linePos);
 
-                l = xs_sldb_gettime(inLine, &linePos);
-                if (l >= 0)
-                    tmnode->lengths[i] = l;
-                else
-                    isOK = FALSE;
-
-                i++;
-            }
-
-            if (!isOK)
-            {
-                xs_sldb_node_free(tmnode);
-                return NULL;
-            }
-            else
-                return tmnode;
-        }
+        if (xs_sldb_gettime(inLine, &linePos) >= 0)
+            tmnode->nlengths++;
+        else
+            isOK = FALSE;
     }
 
+    /* Allocate memory for lengths */
+    if (tmnode->nlengths == 0)
+        goto error;
+
+    tmnode->lengths = (gint *) g_malloc0(tmnode->nlengths * sizeof(gint));
+    if (tmnode->lengths == NULL)
+    {
+        xs_error("Could not allocate memory for node.\n");
+        goto error;
+    }
+
+    /* Read lengths in */
+    for (i = 0, linePos = savePos, isOK = TRUE; 
+        linePos < tmpLen && i < tmnode->nlengths && isOK; i++)
+    {
+        gint l;
+        xs_findnext(inLine, &linePos);
+
+        l = xs_sldb_gettime(inLine, &linePos);
+        if (l >= 0)
+            tmnode->lengths[i] = l;
+        else
+            isOK = FALSE;
+    }
+
+    return tmnode;
+
+error:
+    xs_sldb_node_free(tmnode);
     return NULL;
 }
 
@@ -263,7 +243,6 @@
             xs_error("Invalid line in SongLengthDB file '%s' line #%d:\n%s\n",
                 dbFilename, lineNum, inLine);
         }
-
     }
 
     fclose(inFile);
@@ -535,6 +514,14 @@
         item = bsearch(&key, db->pindex, db->n,
             sizeof(db->pindex[0]), xs_sldb_cmp);
         
+        if (item == NULL)
+        {
+            gint i;
+            xs_error("No matching hash in SLDB: %s\n");
+            for (i = 0; i < XS_MD5HASH_LENGTH; i++)
+                fprintf(stderr, "%02x", keyItem.md5Hash[i]);
+            fprintf(stderr, "\n");
+        }
         return (item != NULL) ? *item : NULL;
     }
     else