diff src/xs_length.c @ 509:81756f412b43

Changes based on patch from Heikki 'shd' Orsila: Fix a crash on 64-bit big-endian platforms due to inproper mixing of guint and size_t types; Use bsearch() instead of my own implementation of binary search in xs_length.c and xs_stil.c; Remove useless(?) use of feof().
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 28 Jan 2007 15:09:04 +0000
parents c67a7f2fd586
children 20cb21c4cb3c
line wrap: on
line diff
--- a/src/xs_length.c	Sun Jan 28 15:07:00 2007 +0000
+++ b/src/xs_length.c	Sun Jan 28 15:09:04 2007 +0000
@@ -208,9 +208,8 @@
 	/* Read and parse the data */
 	lineNum = 0;
 
-	while (!feof(inFile) && fgets(inLine, XS_BUF_SIZE, inFile)) {
+	while (fgets(inLine, XS_BUF_SIZE, inFile) != NULL) {
 		size_t linePos;
-		inLine[XS_BUF_SIZE - 1] = 0;
 		linePos = 0;
 		lineNum++;
 		
@@ -265,64 +264,6 @@
 }
 
 
-/* Get node from db index via binary search
- */
-static t_xs_sldb_node *xs_sldb_get_node(t_xs_sldb * db, t_xs_md5hash pHash)
-{
-	gint iStartNode, iEndNode, iQNode, r, i;
-	gboolean iFound;
-	t_xs_sldb_node *pResult;
-
-	/* Check the database pointers */
-	if (!db || !db->pNodes || !db->ppIndex)
-		return NULL;
-
-	/* Look-up via index using binary search */
-	pResult = NULL;
-	iStartNode = 0;
-	iEndNode = (db->n - 1);
-	iQNode = (iEndNode / 2);
-	iFound = FALSE;
-
-	while ((!iFound) && ((iEndNode - iStartNode) > XS_BIN_BAILOUT)) {
-		r = xs_sldb_cmphash(pHash, db->ppIndex[iQNode]->md5Hash);
-		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 (xs_sldb_cmphash(pHash, db->ppIndex[i]->md5Hash) == 0)
-				iFound = TRUE;
-			else
-				i++;
-		}
-
-		/* Check the result */
-		if (iFound)
-			pResult = db->ppIndex[i];
-
-	} else {
-		/* Found via binary search */
-		pResult = db->ppIndex[iQNode];
-	}
-
-	return pResult;
-}
-
-
 /* Compare two nodes
  */
 static gint xs_sldb_cmp(const void *pNode1, const void *pNode2)
@@ -339,7 +280,7 @@
 gint xs_sldb_index(t_xs_sldb * db)
 {
 	t_xs_sldb_node *pCurr;
-	gint i;
+	size_t i;
 	assert(db);
 
 	/* Free old index */
@@ -550,18 +491,28 @@
 }
 
 
-/* Get song lengths
+/* Get node from db index via binary search
  */
 t_xs_sldb_node *xs_sldb_get(t_xs_sldb *db, const gchar *pcFilename)
 {
-	t_xs_sldb_node *pResult;
-	t_xs_md5hash dbHash;
+	t_xs_sldb_node keyItem, *key, **item;
+
+	/* Check the database pointers */
+	if (!db || !db->pNodes || !db->ppIndex)
+		return NULL;
 
 	/* Get the hash and then look up from db */
-	if (xs_get_sid_hash(pcFilename, dbHash) == 0)
-		pResult = xs_sldb_get_node(db, dbHash);
-	else
-		pResult = NULL;
+	if (xs_get_sid_hash(pcFilename, keyItem.md5Hash) == 0) {
+		key = &keyItem;
+		item = bsearch(&key, db->ppIndex, db->n,
+			sizeof(db->ppIndex[0]), xs_sldb_cmp);
+		
+		if (item)
+			return *item;
+		else
+			return NULL;
+	} else
+		return NULL;
+}
 
-	return pResult;
-}
+