changeset 126:c1462b7880e8

Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust API/ABI accordingly.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 27 Oct 2016 11:52:27 +0300
parents ffccc712409a
children 1786b9d77782
files sidinfo.c sidlib.c sidlib.h
diffstat 3 files changed, 27 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/sidinfo.c	Wed Oct 26 11:59:15 2016 +0300
+++ b/sidinfo.c	Thu Oct 27 11:52:27 2016 +0300
@@ -511,7 +511,7 @@
 
 BOOL argHandleFile(char *filename)
 {
-    static PSIDHeader psid;
+    PSIDHeader *psid = NULL;
     th_ioctx *inFile = NULL;
     FILE *outFile;
     int index;
@@ -523,7 +523,7 @@
     if ((inFile = th_io_fopen(&th_stdio_io_ops, filename, "rb")) == NULL)
     {
         THERR("Could not open file '%s'.\n", filename);
-        return TRUE;
+        goto error;
     }
 
     // Read PSID data
@@ -535,7 +535,7 @@
 
     // Get songlength information, if any
     if (sidSLDB != NULL)
-        psid.lengths = si_sldb_get_by_hash(sidSLDB, psid.hash);
+        psid->lengths = si_sldb_get_by_hash(sidSLDB, psid->hash);
 
     // Output
     for (index = 0; index < optFormat.nitems; index++)
@@ -552,7 +552,7 @@
                 break;
 
             default:
-                siPrintPSIDInformationField(outFile, filename, &psid, &shown, item);
+                siPrintPSIDInformationField(outFile, filename, psid, &shown, item);
                 break;
         }
     }
@@ -565,6 +565,7 @@
 
     // Shutdown
 error:
+    si_free_sid_file(psid);
     th_io_free(inFile);
 
     return TRUE;
--- a/sidlib.c	Wed Oct 26 11:59:15 2016 +0300
+++ b/sidlib.c	Thu Oct 27 11:52:27 2016 +0300
@@ -26,16 +26,15 @@
 } PSIDLibHdr;
 
 
-int si_read_sid_file(th_ioctx *ctx, PSIDHeader *psid)
+int si_read_sid_file(th_ioctx *ctx, PSIDHeader **ppsid)
 {
+    PSIDHeader *psid = NULL;
     th_md5state_t state;
     uint8_t tmp8, *data = NULL;
     int index, ret = -1;
     size_t read;
     BOOL first;
 
-    memset(psid, 0, sizeof(*psid));
-
     if ((data = (uint8_t *) th_malloc(PSID_BUFFER_SIZE)) == NULL)
     {
         th_io_error(ctx, THERR_MALLOC,
@@ -44,6 +43,13 @@
         goto error;
     }
 
+    if ((psid = *ppsid = th_malloc0(sizeof(PSIDHeader))) == NULL)
+    {
+        th_io_error(ctx, THERR_MALLOC,
+            "Error PSID context struct.\n");
+        goto error;
+    }
+
     // Read PSID header in
     if (!thfread_str(ctx, (uint8_t *) psid->magic, PSID_MAGIC_LEN) ||
         !thfread_be16(ctx, &psid->version) ||
@@ -180,6 +186,17 @@
 }
 
 
+void si_free_sid_file(PSIDHeader *psid)
+{
+    if (psid != NULL)
+    {
+        th_free(psid->sidName);
+        th_free(psid->sidAuthor);
+        th_free(psid->sidCopyright);
+    }
+}
+
+
 const char *si_get_sid_clock_str(const int flags)
 {
     switch (flags & PSF_CLOCK_MASK)
--- a/sidlib.h	Wed Oct 26 11:59:15 2016 +0300
+++ b/sidlib.h	Thu Oct 27 11:52:27 2016 +0300
@@ -92,7 +92,8 @@
 //
 // Functions
 //
-int             si_read_sid_file(th_ioctx *ctx, PSIDHeader *psid);
+int             si_read_sid_file(th_ioctx *ctx, PSIDHeader **ppsid);
+void            si_free_sid_file(PSIDHeader *psid);
 
 const char *    si_get_sid_clock_str(const int flags);
 const char *    si_get_sid_model_str(const int flags);