# HG changeset patch # User Matti Hamalainen # Date 1065255661 0 # Node ID 9533b78f1a7df61ca47c002bafaeb6a04064ffed # Parent 56d2b7a440639c3aa8d0e496c065a58acd9a6efe Updated diff -r 56d2b7a44063 -r 9533b78f1a7d src/xs_length.c --- a/src/xs_length.c Fri Oct 03 19:41:04 2003 +0000 +++ b/src/xs_length.c Sat Oct 04 08:21:01 2003 +0000 @@ -31,7 +31,7 @@ /* * Pointer to database in memory */ -static t_xs_sldb_node *xs_database = NULL, *xs_dblast = NULL; +static t_xs_sldb_node *xs_database = NULL; static t_xs_sldb_node **xs_dbindex = NULL; static gint xs_dbnodes = 0; @@ -45,7 +45,7 @@ /* Allocate memory for new node */ pResult = (t_xs_sldb_node *) g_malloc0(sizeof(t_xs_sldb_node)); - if (pResult == NULL) return NULL; + if (!pResult) return NULL; return pResult; } @@ -53,22 +53,30 @@ void xs_db_node_free(t_xs_sldb_node *pNode) { - if (pNode) free(pNode); + if (pNode) g_free(pNode); } /* * Insert given node to db linked list */ +#define LPREV (pNode->pPrev) +#define LTHIS (pNode) +#define LNEXT (pNode->pNext) + void xs_db_node_insert(t_xs_sldb_node *pNode) { - if (xs_dblast) + if (xs_database) { - xs_dblast->pNext = pNode; - xs_dblast = pNode; + /* The first node's pPrev points to last node */ + LPREV = xs_database->pPrev; /* New node's prev = Previous last node */ + xs_database->pPrev->pNext = pNode; /* Previous last node's next = New node */ + xs_database->pPrev = pNode; /* New last node = New node */ + LNEXT = NULL; /* But next is NULL! */ } else { - xs_dblast = xs_database = pNode; - pNode->pNext = NULL; + xs_database = pNode; /* First node ... */ + LPREV = pNode; /* ... it's also last */ + LNEXT = NULL; /* But next is NULL! */ } } @@ -101,7 +109,7 @@ t_xs_sldb_node *pResult; /* Check the database pointers */ - if ((xs_database == NULL) || (xs_dbindex == NULL)) + if (!xs_database || !xs_dbindex) return NULL; /* Look-up via index using binary search */ @@ -325,7 +333,7 @@ XSDEBUG("sldb_init()\n"); /* Read the database */ - if (xs_cfg.songlenDBPath == NULL) + if (!xs_cfg.songlenDBPath) return -10; if (xs_db_read(xs_cfg.songlenDBPath) < 0) @@ -347,7 +355,7 @@ { /* Allocate memory for index-table */ xs_dbindex = (t_xs_sldb_node **) g_malloc(sizeof(t_xs_sldb_node *) * xs_dbnodes); - if (xs_dbindex == NULL) return -6; + if (!xs_dbindex) return -6; /* Get node-pointers to table */ i = 0; @@ -385,9 +393,14 @@ pCurr = pNext; } + xs_database = NULL; + /* Free memory allocated for indexes */ if (xs_dbindex) - free(xs_dbindex); + { + g_free(xs_dbindex); + xs_dbindex = NULL; + } } @@ -395,7 +408,7 @@ * Compute md5hash of given SID-file */ typedef struct { - guint8 magicID[4]; /* "PSID" magic identifier */ + gchar magicID[4]; /* "PSID" magic identifier */ guint16 version, /* Version number */ dataOffset, /* Start of actual c64 data in file */ loadAddress, /* Loading address */ @@ -440,7 +453,7 @@ } -gint xs_get_sid_hash(gchar *fileName, t_xs_md5hash hash) +gint xs_get_sid_hash(gchar *pcFilename, t_xs_md5hash hash) { FILE *inFile; t_xs_md5state inState; @@ -450,7 +463,7 @@ gint iIndex, iRes, songDataLen; /* Try to open the file */ - if ((inFile = fopen(fileName, "rb")) == NULL) + if ((inFile = fopen(pcFilename, "rb")) == NULL) return -1; /* Read PSID header in */ @@ -499,7 +512,11 @@ iRes = fread(songData, sizeof(guint8), songDataLen, inFile); fclose(inFile); - if (iRes != songDataLen) return -9; + if (iRes != songDataLen) + { + g_free(songData); + return -9; + } /* Initialize and start MD5-hash calculation */ xs_md5_init(&inState); @@ -509,7 +526,7 @@ else xs_md5_append(&inState, songData, iRes); - free(songData); + g_free(songData); /* Append header data to hash */ #define XSADDHASH(QDATAB) { ib8[0] = (QDATAB & 0xff); ib8[1] = (QDATAB >> 8); xs_md5_append(&inState, (guint8 *) &ib8, sizeof(ib8)); } @@ -552,36 +569,23 @@ /* - * Get song length + * Get song lengths */ -gint32 xs_songlen_get(gchar *fileName, gint subTune) +t_xs_sldb_node * xs_songlen_get(gchar *pcFilename) { - t_xs_sldb_node *dbNode; + t_xs_sldb_node *pResult; t_xs_md5hash dbHash; - gint32 iResult; - iResult = -1; + pResult = NULL; if (xs_cfg.songlenDBEnable) { /* Get the hash and then look up from db */ - if (xs_get_sid_hash(fileName, dbHash) == 0) + if (xs_get_sid_hash(pcFilename, dbHash) == 0) { - dbNode = xs_db_get(dbHash); - - if (dbNode && (subTune >= 1) && (subTune <= dbNode->nLengths)) - { - /* Get the length */ - iResult = dbNode->sLengths[subTune - 1]; - - /* Take off few last seconds */ - if (iResult > 1) - iResult -= 1; - } - } + pResult = xs_db_get(dbHash); + } } - return iResult; + return pResult; } - -