diff src/xs_stil.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 f1a997715ef5
children 4333288a0168
line wrap: on
line diff
--- a/src/xs_stil.c	Sun Jan 28 15:07:00 2007 +0000
+++ b/src/xs_stil.c	Sun Jan 28 15:09:04 2007 +0000
@@ -176,9 +176,8 @@
 	tmpNode = NULL;
 	subEntry = 0;
 
-	while (!isError && !feof(inFile) && fgets(inLine, XS_BUF_SIZE, inFile)) {
+	while (!isError && fgets(inLine, XS_BUF_SIZE, inFile) != NULL) {
 		size_t linePos, eolPos;
-		inLine[XS_BUF_SIZE - 1] = 0;
 		linePos = eolPos = 0;
 		xs_findeol(inLine, &eolPos);
 		inLine[eolPos] = 0;
@@ -327,7 +326,7 @@
 gint xs_stildb_index(t_xs_stildb *db)
 {
 	t_xs_stil_node *pCurr;
-	gint i;
+	size_t i;
 
 	/* Free old index */
 	if (db->ppIndex) {
@@ -401,55 +400,18 @@
  */
 t_xs_stil_node *xs_stildb_get_node(t_xs_stildb *db, gchar *pcFilename)
 {
-	gint iStartNode, iEndNode, iQNode, r, i;
-	gboolean iFound;
-	t_xs_stil_node *pResult;
+	t_xs_stil_node keyItem, *key, **item;
 
 	/* 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 = strcmp(pcFilename, db->ppIndex[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, db->ppIndex[i]->pcFilename) == 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;
+	/* Look-up index using binary search */
+	keyItem.pcFilename = pcFilename;
+	key = &keyItem;
+	item = bsearch(&key, db->ppIndex, db->n, sizeof(t_xs_stil_node *), xs_stildb_cmp);
+	if (item)
+		return *item;
+	else
+		return NULL;
 }