changeset 128:2636185649c6

Add si_fread_str() helper function.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 27 Oct 2016 11:58:52 +0300
parents 1786b9d77782
children abed2715ea68
files sidlib.c sidlib.h
diffstat 2 files changed, 27 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/sidlib.c	Thu Oct 27 11:52:58 2016 +0300
+++ b/sidlib.c	Thu Oct 27 11:58:52 2016 +0300
@@ -26,6 +26,27 @@
 } PSIDLibHdr;
 
 
+BOOL si_fread_str(th_ioctx *ctx, char **str, const size_t len)
+{
+    char *tmp = th_malloc(len + 1);
+    if (tmp == NULL)
+        goto err;
+
+    if (!thfread_str(ctx, tmp, len))
+        goto err;
+
+    tmp[len] = 0;
+
+    *str = tmp;
+
+    return TRUE;
+
+err:
+    th_free(tmp);
+    return FALSE;
+}
+
+
 int si_read_sid_file(th_ioctx *ctx, PSIDHeader **ppsid)
 {
     PSIDHeader *psid = NULL;
@@ -80,9 +101,9 @@
 
     psid->isRSID = psid->magic[0] == 'R';
 
-    if (!thfread_str(ctx, (uint8_t *) psid->sidName, PSID_STR_LEN) ||
-        !thfread_str(ctx, (uint8_t *) psid->sidAuthor, PSID_STR_LEN) ||
-        !thfread_str(ctx, (uint8_t *) psid->sidCopyright, PSID_STR_LEN))
+    if (!si_fread_str(ctx, &psid->sidName, PSID_STR_LEN) ||
+        !si_fread_str(ctx, &psid->sidAuthor, PSID_STR_LEN) ||
+        !si_fread_str(ctx, &psid->sidCopyright, PSID_STR_LEN))
     {
         th_io_error(ctx, ctx->status,
             "Error reading SID file header: %s.\n",
@@ -90,10 +111,6 @@
         goto error;
     }
 
-    psid->sidName[PSID_STR_LEN] = 0;
-    psid->sidAuthor[PSID_STR_LEN] = 0;
-    psid->sidCopyright[PSID_STR_LEN] = 0;
-
     // Check if we need to load PSIDv2NG header ...
     if (psid->version >= 2)
     {
--- a/sidlib.h	Thu Oct 27 11:52:58 2016 +0300
+++ b/sidlib.h	Thu Oct 27 11:58:52 2016 +0300
@@ -51,9 +51,9 @@
         nSongs,          // Number of subsongs
         startSong;       // Default starting song
     uint32_t speed;      // Speed
-    char sidName[PSID_STR_LEN + 1];    // Descriptive text-fields, ASCIIZ
-    char sidAuthor[PSID_STR_LEN + 1];
-    char sidCopyright[PSID_STR_LEN + 1];
+    char *sidName;       // Descriptive text-fields, ASCIIZ
+    char *sidAuthor;
+    char *sidCopyright;
 
     // PSIDv2 data
     uint16_t flags;      // Flags