# HG changeset patch # User Matti Hamalainen # Date 1455232428 -7200 # Node ID c5ff71d64e53bf3dd9b6d303381b6cc0baa3dbde # Parent 1e89b757f8a1263bdda54639b1c9a135433efb07 More work on sidlib SLDB code. diff -r 1e89b757f8a1 -r c5ff71d64e53 sidlib.c --- a/sidlib.c Fri Feb 12 00:13:27 2016 +0200 +++ b/sidlib.c Fri Feb 12 01:13:48 2016 +0200 @@ -241,37 +241,12 @@ } -static int si_sldb_get_hex_value(const char *str, size_t *pos) -{ - int result = 0; - - do - { - char ch = str[*pos]; - result <<= 4; - if (ch >= 'A' && ch <= 'F') - result |= ch - 'A'; - else - if (ch >= 'a' && ch <= 'f') - result |= ch - 'a'; - else - if (ch >= '0' && ch <= '9') - result |= ch - '0'; - else - break; - } - while (1); - - return result; -} - - static int si_sldb_gettime(const char *str, size_t *pos) { int result; // Check if it starts with a digit - if (isdigit(str[*pos])) + if (th_isdigit(str[*pos])) { // Get minutes-field result = si_sldb_get_value(str, pos) * 60; @@ -290,7 +265,7 @@ result = -1; // Ignore and skip the possible attributes - while (str[*pos] && !isspace(str[*pos])) + while (str[*pos] && !th_isspace(str[*pos])) (*pos)++; return result; @@ -383,13 +358,27 @@ } +SIDLibSLDB * si_sldb_new(void) +{ + return (SIDLibSLDB *) th_malloc0(sizeof(SIDLibSLDB)); +} + + // Read SLDB database to memory // int si_sldb_read(th_ioctx *ctx, SIDLibSLDB *dbh) { - char line[PSID_BUFFER2_SIZE]; + char *line = NULL; - while (thfgets(line, PSID_BUFFER2_SIZE, ctx) != NULL) + if ((line = th_malloc(PSID_BUFFER_SIZE)) == NULL) + { + th_io_error(ctx, THERR_MALLOC, + "Error allocating temporary data buffer of %d bytes.\n", + PSID_BUFFER_SIZE); + return ctx->errno; + } + + while (thfgets(line, PSID_BUFFER_SIZE, ctx) != NULL) { SIDLibSLDBNode *tmnode; size_t pos = 0; @@ -434,6 +423,7 @@ } } + th_free(line); return THERR_OK; } @@ -483,9 +473,9 @@ size_t i; // Allocate memory for index-table - dbh->pindex = (SIDLibSLDBNode * *) th_malloc(sizeof(SIDLibSLDBNode *) * dbh->n); + dbh->pindex = (SIDLibSLDBNode **) th_malloc(sizeof(SIDLibSLDBNode *) * dbh->n); if (dbh->pindex == NULL) - return -1; + return THERR_MALLOC; // Get node-pointers to table for (i = 0, node = dbh->nodes; node && i < dbh->n; node = node->next) @@ -495,7 +485,7 @@ qsort(dbh->pindex, dbh->n, sizeof(SIDLibSLDBNode *), si_sldb_compare_nodes); } - return 0; + return THERR_OK; } @@ -503,19 +493,22 @@ // void si_sldb_free(SIDLibSLDB *dbh) { - SIDLibSLDBNode *node = dbh->nodes; - while (node != NULL) + if (dbh != NULL) { - SIDLibSLDBNode *next = node->next; - si_sldb_node_free(node); - node = next; + SIDLibSLDBNode *node = dbh->nodes; + while (node != NULL) + { + SIDLibSLDBNode *next = node->next; + si_sldb_node_free(node); + node = next; + } + + dbh->nodes = NULL; + dbh->n = 0; + + th_free_r(&dbh->pindex); + th_free(dbh); } - - dbh->nodes = NULL; - dbh->n = 0; - - th_free_r(&dbh->pindex); - th_free(dbh); } @@ -523,7 +516,7 @@ { SIDLibSLDBNode keyItem, *key, **item; - memcpy(&keyItem.hash, &hash, sizeof(th_md5hash_t)); + memcpy(&keyItem.hash, hash, sizeof(th_md5hash_t)); key = &keyItem; item = bsearch(&key, dbh->pindex, dbh->n, sizeof(dbh->pindex[0]), si_sldb_compare_nodes); diff -r 1e89b757f8a1 -r c5ff71d64e53 sidlib.h --- a/sidlib.h Fri Feb 12 00:13:27 2016 +0200 +++ b/sidlib.h Fri Feb 12 01:13:48 2016 +0200 @@ -20,7 +20,6 @@ #define PSID_MAGIC_LEN 4 #define PSID_STR_LEN 32 #define PSID_BUFFER_SIZE (1024 * 16) -#define PSID_BUFFER2_SIZE (1024) typedef struct _SIDLibSLDBNode @@ -66,6 +65,8 @@ size_t dataSize; // Total size of data - header th_md5hash_t hash; // Songlength database hash + SIDLibSLDBNode *lengths; // Songlength information node pointer + } PSIDHeader; @@ -96,6 +97,7 @@ const char * si_get_sid_clock_str(const int flags); const char * si_get_sid_model_str(const int flags); +SIDLibSLDB * si_sldb_new(void); int si_sldb_read(th_ioctx *ctx, SIDLibSLDB *dbh); int si_sldb_build_index(SIDLibSLDB *dbh); void si_sldb_free(SIDLibSLDB *dbh);