# HG changeset patch # User Matti Hamalainen # Date 1148946364 0 # Node ID 3c239e4160d52444d59dcf3e1ff612dec6586ccc # Parent 52f71f323555a3ae599325f4b388c5f8dd03b247 Cleanups. diff -r 52f71f323555 -r 3c239e4160d5 src/xs_length.c --- a/src/xs_length.c Sun May 28 13:20:02 2006 +0000 +++ b/src/xs_length.c Mon May 29 23:46:04 2006 +0000 @@ -75,9 +75,9 @@ /* Parses a time-entry in SLDB format */ -static gint32 xs_sldb_gettime(gchar * pcStr, size_t * piPos) +static gint xs_sldb_gettime(gchar * pcStr, size_t * piPos) { - gint32 iResult, iTemp; + gint iResult, iTemp; /* Check if it starts with a digit */ if (isdigit(pcStr[*piPos])) { @@ -111,6 +111,92 @@ } +t_xs_sldb_node * xs_sldb_read_entry(gchar *inLine) +{ + size_t linePos, savePos, tmpI, tmpLen; + t_xs_sldb_node *tmpNode; + + /* Allocate new node */ + if ((tmpNode = xs_sldb_node_new()) == NULL) { + XSERR("Error allocating new node. Fatal error.\n"); + return NULL; + } + + /* Get hash value */ +#if (XS_MD5HASH_LENGTH != 16) +#error Mismatch in hashcode length. Check against xs_md5.h and fix here. +#endif + sscanf(&inLine[0], "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", + (guint *) & (tmpNode->md5Hash[0]), (guint *) & (tmpNode->md5Hash[1]), + (guint *) & (tmpNode->md5Hash[2]), (guint *) & (tmpNode->md5Hash[3]), + (guint *) & (tmpNode->md5Hash[4]), (guint *) & (tmpNode->md5Hash[5]), + (guint *) & (tmpNode->md5Hash[6]), (guint *) & (tmpNode->md5Hash[7]), + (guint *) & (tmpNode->md5Hash[8]), (guint *) & (tmpNode->md5Hash[9]), + (guint *) & (tmpNode->md5Hash[10]), (guint *) & (tmpNode->md5Hash[11]), + (guint *) & (tmpNode->md5Hash[12]), (guint *) & (tmpNode->md5Hash[13]), + (guint *) & (tmpNode->md5Hash[14]), (guint *) & (tmpNode->md5Hash[15])); + + /* Get playtimes */ + if (inLine[linePos] != 0) { + if (inLine[linePos] != '=') { + XSERR("'=' expected in SongLengthDB file '%s' line #%d, column #%d\n", + dbFilename, lineNum, linePos); + + xs_sldb_node_free(tmpNode); + return NULL; + } else { + /* First playtime is after '=' */ + savePos = ++linePos; + tmpLen = strlen(inLine); + + /* Get number of sub-tune lengths */ + iOK = TRUE; + while ((linePos < tmpLen) && iOK) { + xs_findnext(inLine, &linePos); + if (xs_sldb_gettime(inLine, &linePos) >= 0) + tmpNode->nLengths++; + else + iOK = FALSE; + } + + if (!iOK) { + XSERR("Invalid plaaplaa\n"); + xs_sldb_node_free(tmpNode); + return NULL; + } + + /* Allocate memory for lengths */ + tmpNode->sLengths = (gint *) g_malloc0(tmpNode->nLengths * sizeof(gint)); + if (!tmpNode->sLengths) { + XSERR("Could not allocate memory for SongLengthDB entry.\n"); + xs_sldb_node_free(tmpNode); + return NULL; + } + + /* Read lengths in */ + tmpI = 0; linePos = savePos; iOK = TRUE; + while ((linePos < tmpLen) && iOK) { + xs_findnext(inLine, &linePos); + + if (tmpI < tmpNode->nLengths) { + tmpNode->sLengths[tmpI] = xs_sldb_gettime(inLine, &linePos); + tmpI++; + } else + iOK = FALSE; + } + + if (!iOK) { + xs_sldb_node_free(tmpNode); + return NULL; + } else + return tmpNode; + } + } + + return NULL; +} + + /* Read database to memory */ gint xs_sldb_read(t_xs_sldb * db, gchar * dbFilename) @@ -134,74 +220,28 @@ while (!feof(inFile)) { fgets(inLine, XS_BUF_SIZE, inFile); inLine[XS_BUF_SIZE - 1] = 0; + linePos = 0; lineNum++; /* Check if it is datafield */ - if (isxdigit(inLine[0])) { + if (isxdigit(inLine[linePos])) { + size_t tmpI; + /* Check the length of the hash */ - linePos = 0; - while (isxdigit(inLine[linePos])) - linePos++; + for (tmpI = 0; isxdigit(inLine[linePos]); linePos++, tmpI++); - if (linePos != XS_MD5HASH_LENGTH_CH) { + if (tmpI != XS_MD5HASH_LENGTH_CH) { XSERR("Invalid hash in SongLengthDB file '%s' line #%d!\n", dbFilename, lineNum); } else { - /* Allocate new node */ - if ((tmpNode = xs_sldb_node_new()) == NULL) { - XSERR("Error allocating new node. Fatal error.\n"); - exit(5); - } - - /* Get hash value */ -#if (XS_MD5HASH_LENGTH != 16) -#error Mismatch in hashcode length. Fix here. -#endif - sscanf(&inLine[0], "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", - (guint *) & (tmpNode->md5Hash[0]), (guint *) & (tmpNode->md5Hash[1]), - (guint *) & (tmpNode->md5Hash[2]), (guint *) & (tmpNode->md5Hash[3]), - (guint *) & (tmpNode->md5Hash[4]), (guint *) & (tmpNode->md5Hash[5]), - (guint *) & (tmpNode->md5Hash[6]), (guint *) & (tmpNode->md5Hash[7]), - (guint *) & (tmpNode->md5Hash[8]), (guint *) & (tmpNode->md5Hash[9]), - (guint *) & (tmpNode->md5Hash[10]), (guint *) & (tmpNode->md5Hash[11]), - (guint *) & (tmpNode->md5Hash[12]), (guint *) & (tmpNode->md5Hash[13]), - (guint *) & (tmpNode->md5Hash[14]), (guint *) & (tmpNode->md5Hash[15])); - - /* Get playtimes */ - if (inLine[linePos] != 0) { - if (inLine[linePos] != '=') { - XSERR("'=' expected in SongLengthDB file '%s' line #%d, column #%d\n", - dbFilename, lineNum, linePos); - - xs_sldb_node_free(tmpNode); - } else { - /* First playtime is after '=' */ - linePos++; - iOK = TRUE; - - while ((linePos < strlen(inLine)) && iOK) { - xs_findnext(inLine, &linePos); - - if (tmpNode->nLengths < XS_STIL_MAXENTRY) { - tmpNode->sLengths[tmpNode->nLengths] = - xs_sldb_gettime(inLine, &linePos); - tmpNode->nLengths++; - } else - iOK = FALSE; - } - - /* Add an node to db in memory */ - if (iOK) - xs_sldb_node_insert(db, tmpNode); - else - xs_sldb_node_free(tmpNode); - } - } + /* Parse and add node to db */ + if ((tmpNode = xs_sldb_read_entry(inLine)) != NULL) + xs_sldb_node_insert(db, tmpNode); } } else if ((inLine[0] != ';') && (inLine[0] != '[')) { XSERR("Invalid line in SongLengthDB file '%s' line #%d\n", dbFilename, lineNum); } - } /* while */ + } /* Close the file */ fclose(inFile);