# HG changeset patch # User Matti Hamalainen # Date 1352543356 -7200 # Node ID 84394ee26545ababd5299dd2465dbf9792a2cf6e # Parent 8b4c016802eaf344dc2fd4a273774bb5f19a473a Cleanups. diff -r 8b4c016802ea -r 84394ee26545 src/xs_about.c --- a/src/xs_about.c Sat Nov 10 11:58:59 2012 +0200 +++ b/src/xs_about.c Sat Nov 10 12:29:16 2012 +0200 @@ -71,7 +71,8 @@ gchar tmpStr[64]; /* Check if there already is an open about window */ - if (xs_aboutwin != NULL) { + if (xs_aboutwin != NULL) + { XS_WINDOW_PRESENT(xs_aboutwin); return; } diff -r 8b4c016802ea -r 84394ee26545 src/xs_length.c --- a/src/xs_length.c Sat Nov 10 11:58:59 2012 +0200 +++ b/src/xs_length.c Sat Nov 10 12:29:16 2012 +0200 @@ -31,8 +31,8 @@ */ static void xs_sldb_node_free(XSSLDBNode *node) { - if (node) { - /* Nothing much to do here ... */ + if (node) + { g_free(node->lengths); g_free(node); } @@ -45,11 +45,14 @@ { assert(db != NULL); - if (db->nodes) { + if (db->nodes) + { node->prev = db->nodes->prev; db->nodes->prev->next = node; db->nodes->prev = node; - } else { + } + else + { db->nodes = node; node->prev = node; } @@ -64,7 +67,8 @@ gint result, tmp; /* Check if it starts with a digit */ - if (isdigit(str[*pos])) { + if (isdigit(str[*pos])) + { /* Get minutes-field */ result = 0; while (isdigit(str[*pos])) @@ -73,18 +77,20 @@ result *= 60; /* Check the field separator char */ - if (str[*pos] == ':') { + if (str[*pos] == ':') + { /* Get seconds-field */ (*pos)++; tmp = 0; - while (isdigit(str[*pos])) { + while (isdigit(str[*pos])) tmp = (tmp * 10) + (str[(*pos)++] - '0'); - } result += tmp; - } else + } + else result = -2; - } else + } + else result = -1; /* Ignore and skip the possible attributes */ @@ -106,26 +112,32 @@ /* Allocate new node */ tmnode = (XSSLDBNode *) g_malloc0(sizeof(XSSLDBNode)); - if (!tmnode) { + if (!tmnode) + { xs_error("Error allocating new node. Fatal error.\n"); return NULL; } /* Get hash value */ linePos = 0; - for (i = 0; i < XS_MD5HASH_LENGTH; i++, linePos += 2) { + for (i = 0; i < XS_MD5HASH_LENGTH; i++, linePos += 2) + { gint tmpu; sscanf(&inLine[linePos], "%2x", &tmpu); tmnode->md5Hash[i] = tmpu; } /* Get playtimes */ - if (inLine[linePos] != 0) { - if (inLine[linePos] != '=') { + if (inLine[linePos] != 0) + { + if (inLine[linePos] != '=') + { xs_error("'=' expected on column #%d.\n", linePos); xs_sldb_node_free(tmnode); return NULL; - } else { + } + else + { size_t tmpLen, savePos; /* First playtime is after '=' */ @@ -134,7 +146,8 @@ /* Get number of sub-tune lengths */ isOK = TRUE; - while ((linePos < tmpLen) && isOK) { + while (linePos < tmpLen && isOK) + { xs_findnext(inLine, &linePos); if (xs_sldb_gettime(inLine, &linePos) >= 0) @@ -144,23 +157,26 @@ } /* Allocate memory for lengths */ - if (tmnode->nlengths > 0) { + if (tmnode->nlengths > 0) + { tmnode->lengths = (gint *) g_malloc0(tmnode->nlengths * sizeof(gint)); - if (!tmnode->lengths) { + if (!tmnode->lengths) + { xs_error("Could not allocate memory for node.\n"); xs_sldb_node_free(tmnode); return NULL; } - } else { + } + else + { xs_sldb_node_free(tmnode); return NULL; } /* Read lengths in */ - i = 0; - linePos = savePos; - isOK = TRUE; - while ((linePos < tmpLen) && (i < tmnode->nlengths) && isOK) { + for (i = 0, linePos = savePos, isOK = TRUE; + linePos < tmpLen && i < tmnode->nlengths && isOK; i++) + { gint l; xs_findnext(inLine, &linePos); @@ -174,10 +190,12 @@ i++; } - if (!isOK) { + if (!isOK) + { xs_sldb_node_free(tmnode); return NULL; - } else + } + else return tmnode; } } @@ -197,7 +215,8 @@ assert(db); /* Try to open the file */ - if ((inFile = fopen(dbFilename, "ra")) == NULL) { + if ((inFile = fopen(dbFilename, "ra")) == NULL) + { xs_error("Could not open SongLengthDB '%s'\n", dbFilename); return -1; } @@ -205,40 +224,49 @@ /* Read and parse the data */ lineNum = 0; - while (fgets(inLine, XS_BUF2_SIZE, inFile) != NULL) { + while (fgets(inLine, XS_BUF2_SIZE, inFile) != NULL) + { size_t linePos = 0; lineNum++; xs_findnext(inLine, &linePos); /* Check if it is datafield */ - if (isxdigit(inLine[linePos])) { + if (isxdigit(inLine[linePos])) + { /* Check the length of the hash */ gint hashLen; for (hashLen = 0; inLine[linePos] && isxdigit(inLine[linePos]); hashLen++, linePos++); - if (hashLen != XS_MD5HASH_LENGTH_CH) { + if (hashLen != XS_MD5HASH_LENGTH_CH) + { xs_error("Invalid MD5-hash in SongLengthDB file '%s' line #%d:\n%s\n", dbFilename, lineNum, inLine); - } else { + } + else + { /* Parse and add node to db */ - if ((tmnode = xs_sldb_read_entry(inLine)) != NULL) { + if ((tmnode = xs_sldb_read_entry(inLine)) != NULL) + { xs_sldb_node_insert(db, tmnode); - } else { + } + else + { xs_error("Invalid entry in SongLengthDB file '%s' line #%d:\n%s\n", dbFilename, lineNum, inLine); } } - } else if (inLine[linePos] != ';' && inLine[linePos] != '[' && inLine[linePos] != 0) { + } + else + if (inLine[linePos] != ';' && inLine[linePos] != '[' && inLine[linePos] != 0) + { xs_error("Invalid line in SongLengthDB file '%s' line #%d:\n%s\n", dbFilename, lineNum, inLine); } } - /* Close the file */ fclose(inFile); - return 0; } @@ -275,38 +303,29 @@ */ gint xs_sldb_index(XSSLDB * db) { - XSSLDBNode *pCurr; + XSSLDBNode *node; size_t i; assert(db); /* Free old index */ - if (db->pindex) { - g_free(db->pindex); - db->pindex = NULL; - } + g_free(db->pindex); + db->pindex = NULL; /* Get size of db */ - pCurr = db->nodes; - db->n = 0; - while (pCurr) { + for (node = db->nodes, db->n = 0; node != NULL; node = node->next) db->n++; - pCurr = pCurr->next; - } /* Check number of nodes */ - if (db->n > 0) { + if (db->n > 0) + { /* Allocate memory for index-table */ db->pindex = (XSSLDBNode **) g_malloc(sizeof(XSSLDBNode *) * db->n); if (!db->pindex) return -1; /* Get node-pointers to table */ - i = 0; - pCurr = db->nodes; - while (pCurr && (i < db->n)) { - db->pindex[i++] = pCurr; - pCurr = pCurr->next; - } + for (i = 0, node = db->nodes; node && i < db->n; node = node->next) + db->pindex[i++] = node; /* Sort the indexes */ qsort(db->pindex, db->n, sizeof(XSSLDBNode *), xs_sldb_cmp); @@ -320,26 +339,25 @@ */ void xs_sldb_free(XSSLDB * db) { - XSSLDBNode *pCurr, *next; + XSSLDBNode *node, *next; if (!db) return; /* Free the memory allocated for nodes */ - pCurr = db->nodes; - while (pCurr) { - next = pCurr->next; - xs_sldb_node_free(pCurr); - pCurr = next; + node = db->nodes; + while (node != NULL) + { + next = node->next; + xs_sldb_node_free(node); + node = next; } db->nodes = NULL; /* Free memory allocated for index */ - if (db->pindex) { - g_free(db->pindex); - db->pindex = NULL; - } + g_free(db->pindex); + db->pindex = NULL; /* Free structure */ db->n = 0; @@ -349,7 +367,8 @@ /* Compute md5hash of given SID-file */ -typedef struct { +typedef struct +{ gchar magicID[4]; /* "PSID" / "RSID" magic identifier */ guint16 version, /* Version number */ dataOffset, /* Start of actual c64 data in file */ @@ -365,7 +384,8 @@ } psidv1_header_t; -typedef struct { +typedef struct +{ guint16 flags; /* Flags */ guint8 startPage, pageLength; guint16 reserved; @@ -388,7 +408,9 @@ /* Read PSID header in */ xs_fread(psidH.magicID, sizeof(psidH.magicID), 1, inFile); - if (strncmp(psidH.magicID, "PSID", 4) && strncmp(psidH.magicID, "RSID", 4)) { + if (strncmp(psidH.magicID, "PSID", 4) && + strncmp(psidH.magicID, "RSID", 4)) + { xs_fclose(inFile); xs_error("Not a PSID or RSID file '%s'\n", filename); return -2; @@ -407,7 +429,8 @@ xs_fread(psidH.sidAuthor, sizeof(gchar), sizeof(psidH.sidAuthor), inFile); xs_fread(psidH.sidCopyright, sizeof(gchar), sizeof(psidH.sidCopyright), inFile); - if (xs_feof(inFile) || xs_ferror(inFile)) { + if (xs_feof(inFile) || xs_ferror(inFile)) + { xs_fclose(inFile); xs_error("Error reading SID file header from '%s'\n", filename); return -4; @@ -416,7 +439,8 @@ /* Check if we need to load PSIDv2NG header ... */ psidH2.flags = 0; /* Just silence a stupid gcc warning */ - if (psidH.version == 2) { + if (psidH.version == 2) + { /* Yes, we need to */ psidH2.flags = xs_fread_be16(inFile); psidH2.startPage = xs_fgetc(inFile); @@ -426,7 +450,8 @@ /* Allocate buffer */ songData = (guint8 *) g_malloc(XS_SIDBUF_SIZE * sizeof(guint8)); - if (!songData) { + if (!songData) + { xs_fclose(inFile); xs_error("Error allocating temp data buffer for file '%s'\n", filename); return -3; @@ -439,10 +464,13 @@ /* Initialize and start MD5-hash calculation */ xs_md5_init(&inState); - if (psidH.loadAddress == 0) { + if (psidH.loadAddress == 0) + { /* Strip load address (2 first bytes) */ xs_md5_append(&inState, &songData[2], result - 2); - } else { + } + else + { /* Append "as is" */ xs_md5_append(&inState, songData, result); } @@ -451,9 +479,9 @@ g_free(songData); /* Append header data to hash */ -#define XSADDHASH(QDATAB) do { \ - ib8[0] = (QDATAB & 0xff); \ - ib8[1] = (QDATAB >> 8); \ +#define XSADDHASH(QDATAB) do { \ + ib8[0] = (QDATAB & 0xff); \ + ib8[1] = (QDATAB >> 8); \ xs_md5_append(&inState, (guint8 *) &ib8, sizeof(ib8)); \ } while (0) @@ -464,18 +492,19 @@ /* Append song speed data to hash */ i8 = 0; - for (index = 0; (index < psidH.nSongs) && (index < 32); index++) { + for (index = 0; index < psidH.nSongs && index < 32; index++) + { i8 = (psidH.speed & (1 << index)) ? 60 : 0; xs_md5_append(&inState, &i8, sizeof(i8)); } /* Rest of songs (more than 32) */ - for (index = 32; index < psidH.nSongs; index++) { + for (index = 32; index < psidH.nSongs; index++) xs_md5_append(&inState, &i8, sizeof(i8)); - } /* PSIDv2NG specific */ - if (psidH.version == 2) { + if (psidH.version == 2) + { /* SEE SIDPLAY HEADERS FOR INFO */ i8 = (psidH2.flags >> 2) & 3; if (i8 == 2) @@ -500,17 +529,14 @@ return NULL; /* Get the hash and then look up from db */ - if (xs_get_sid_hash(filename, keyItem.md5Hash) == 0) { + if (xs_get_sid_hash(filename, keyItem.md5Hash) == 0) + { key = &keyItem; item = bsearch(&key, db->pindex, db->n, sizeof(db->pindex[0]), xs_sldb_cmp); - if (item) - return *item; - else - return NULL; - } else + return (item != NULL) ? *item : NULL; + } + else return NULL; } - - diff -r 8b4c016802ea -r 84394ee26545 src/xs_length.h --- a/src/xs_length.h Sat Nov 10 11:58:59 2012 +0200 +++ b/src/xs_length.h Sat Nov 10 12:29:16 2012 +0200 @@ -10,7 +10,8 @@ /* Types */ -typedef struct _XSSLDBNode { +typedef struct _XSSLDBNode +{ xs_md5hash_t md5Hash; /* 128-bit MD5 hash-digest */ gint nlengths; /* Number of lengths */ gint *lengths; /* Lengths in seconds */ @@ -18,7 +19,8 @@ } XSSLDBNode; -typedef struct { +typedef struct +{ XSSLDBNode *nodes, **pindex; size_t n; @@ -30,7 +32,7 @@ gint xs_sldb_read(XSSLDB *, const gchar *); gint xs_sldb_index(XSSLDB *); void xs_sldb_free(XSSLDB *); -XSSLDBNode * xs_sldb_get(XSSLDB *, const gchar *); +XSSLDBNode * xs_sldb_get(XSSLDB *, const gchar *); #ifdef __cplusplus }