changeset 226:c32015f4969e

Rename PSIDHeader struct to SIDLibPSIDHeader to conform with the SIDLib prefix used otherwise. Add new function sidlib_read_sid_file_alloc() which allocates the PSID header struct, instead of sidlib_read_sid_file() which just reads into the given struct.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 03 Jan 2020 10:38:18 +0200
parents 15a51252b73a
children 2b3d5d49086d
files sidinfo.c sidlib.c sidlib.h
diffstat 3 files changed, 34 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/sidinfo.c	Tue Dec 31 05:20:17 2019 +0200
+++ b/sidinfo.c	Fri Jan 03 10:38:18 2020 +0200
@@ -854,7 +854,9 @@
 }
 
 
-static void siPrintPSIDInfoLine(FILE *outFile, BOOL *shown, const PSFStackItem *item, const char *d_str, const int d_int, const BOOL useConv)
+static void siPrintPSIDInfoLine(FILE *outFile, BOOL *shown,
+    const PSFStackItem *item, const char *d_str,
+    const int d_int, const BOOL useConv)
 {
     const PSFOption *opt = &optPSOptions[item->cmd];
     char *fmt, *str, *tmp;
@@ -905,7 +907,8 @@
 #define PRI(d_int) siPrintPSIDInfoLine(outFile, shown, item, NULL, d_int, FALSE)
 
 
-static void siPrintPSIDInformationField(FILE *outFile, const char *filename, const PSIDHeader *psid, BOOL *shown, const PSFStackItem *item)
+static void siPrintPSIDInformationField(FILE *outFile, const char *filename,
+    const SIDLibPSIDHeader *psid, BOOL *shown, const PSFStackItem *item)
 {
     const PSFOption *opt = &optPSOptions[item->cmd];
     char tmp[128];
@@ -1013,7 +1016,7 @@
 
 BOOL siHandleSIDFile(const char *filename)
 {
-    PSIDHeader *psid = NULL;
+    SIDLibPSIDHeader *psid = NULL;
     th_ioctx *inFile = NULL;
     FILE *outFile;
     BOOL shown = FALSE;
@@ -1031,7 +1034,7 @@
     th_io_set_handlers(inFile, siError, NULL);
 
     // Read PSID data
-    if (!sidlib_read_sid_file(inFile, &psid, setSLDBNewFormat))
+    if (!sidlib_read_sid_file_alloc(inFile, &psid, setSLDBNewFormat))
         goto error;
 
     // Get songlength information, if any
--- a/sidlib.c	Tue Dec 31 05:20:17 2019 +0200
+++ b/sidlib.c	Fri Jan 03 10:38:18 2020 +0200
@@ -47,7 +47,7 @@
 }
 
 
-static BOOL sidlib_read_hash_data(th_ioctx *ctx, PSIDHeader *psid,
+static BOOL sidlib_read_hash_data(th_ioctx *ctx, SIDLibPSIDHeader *psid,
     th_md5state_t *state, const BOOL newSLDB)
 {
     uint8_t *data = NULL;
@@ -105,20 +105,12 @@
 }
 
 
-BOOL sidlib_read_sid_file(th_ioctx *ctx, PSIDHeader **ppsid, const BOOL newSLDB)
+BOOL sidlib_read_sid_file(th_ioctx *ctx, SIDLibPSIDHeader *psid, const BOOL newSLDB)
 {
-    PSIDHeader *psid = NULL;
     th_md5state_t state;
     BOOL ret = FALSE;
     off_t hdrStart, hdrEnd;
 
-    if ((psid = *ppsid = th_malloc0(sizeof(PSIDHeader))) == NULL)
-    {
-        th_io_error(ctx, THERR_MALLOC,
-            "Error PSID context struct.\n");
-        goto error;
-    }
-
     hdrStart = thftell(ctx);
 
     // Read PSID header in
@@ -243,13 +235,31 @@
 }
 
 
-void sidlib_free_sid_file(PSIDHeader *psid)
+BOOL sidlib_read_sid_file_alloc(th_ioctx *ctx, SIDLibPSIDHeader **ppsid, const BOOL newSLDB)
+{
+    if ((*ppsid = th_malloc0(sizeof(SIDLibPSIDHeader))) == NULL)
+    {
+        th_io_error(ctx, THERR_MALLOC,
+            "Error allocating PSID context struct.\n");
+        return FALSE;
+    }
+    
+    (*ppsid)->allocated = TRUE;
+
+    return sidlib_read_sid_file(ctx, *ppsid, newSLDB);
+}
+
+
+void sidlib_free_sid_file(SIDLibPSIDHeader *psid)
 {
     if (psid != NULL)
     {
         th_free(psid->sidName);
         th_free(psid->sidAuthor);
         th_free(psid->sidCopyright);
+        
+        if (psid->allocated)
+            th_free(psid);
     }
 }
 
@@ -581,6 +591,7 @@
     if (hdr.nnodes == 0 || hdr.nnodes > 256*1024)
         return THERR_INVALID_DATA;
 
+    // Initialize MD5 state
     th_md5_init(&state);
     th_md5_append_ne16(&state, hdr.version);
     th_md5_append_ne32(&state, hdr.nnodes);
--- a/sidlib.h	Tue Dec 31 05:20:17 2019 +0200
+++ b/sidlib.h	Fri Jan 03 10:38:18 2020 +0200
@@ -94,15 +94,17 @@
     th_md5hash_t hash;   // Songlength database hash
 
     SIDLibSLDBNode *lengths; // Songlength information node pointer
+    BOOL allocated; // TRUE if structure has been allocated
 
-} PSIDHeader;
+} SIDLibPSIDHeader;
 
 
 //
 // Functions
 //
-BOOL             sidlib_read_sid_file(th_ioctx *ctx, PSIDHeader **ppsid, const BOOL newSLDB);
-void             sidlib_free_sid_file(PSIDHeader *psid);
+int              sidlib_read_sid_file(th_ioctx *ctx, SIDLibPSIDHeader *psid, const BOOL newSLDB);
+int              sidlib_read_sid_file_alloc(th_ioctx *ctx, SIDLibPSIDHeader **ppsid, const BOOL newSLDB);
+void             sidlib_free_sid_file(SIDLibPSIDHeader *psid);
 
 const char *     sidlib_get_sid_clock_str(const int flags);
 const char *     sidlib_get_sid_model_str(const int flags);