# HG changeset patch # User Matti Hamalainen # Date 1169996944 0 # Node ID 81756f412b439d12ac262ace148a869dfe96b4e7 # Parent 338332aeb5a452d143f85a0a295696e059cb50c2 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(). diff -r 338332aeb5a4 -r 81756f412b43 src/xs_length.c --- 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; -} + diff -r 338332aeb5a4 -r 81756f412b43 src/xs_length.h --- a/src/xs_length.h Sun Jan 28 15:07:00 2007 +0000 +++ b/src/xs_length.h Sun Jan 28 15:09:04 2007 +0000 @@ -21,7 +21,7 @@ typedef struct { t_xs_sldb_node *pNodes, **ppIndex; - gint n; + size_t n; } t_xs_sldb; diff -r 338332aeb5a4 -r 81756f412b43 src/xs_stil.c --- 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; } diff -r 338332aeb5a4 -r 81756f412b43 src/xs_stil.h --- a/src/xs_stil.h Sun Jan 28 15:07:00 2007 +0000 +++ b/src/xs_stil.h Sun Jan 28 15:09:04 2007 +0000 @@ -30,7 +30,7 @@ typedef struct { t_xs_stil_node *pNodes, **ppIndex; - gint n; + size_t n; } t_xs_stildb;