changeset 85:4c0ecb078591

Rename various variables and functions and change relevant places to use the new th_ctx API.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 11 Feb 2016 23:18:58 +0200
parents 04ce0ffbbbb0
children e1ff9cd27a84
files Makefile.gen sidinfo.c sidlib.c sidlib.h
diffstat 4 files changed, 61 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile.gen	Thu Feb 11 23:15:59 2016 +0200
+++ b/Makefile.gen	Thu Feb 11 23:18:58 2016 +0200
@@ -10,7 +10,7 @@
 # Objects
 #
 THLIBS_A=$(OBJPATH)thlibs.a
-THLIBS_OBJ=th_util.o th_string.o th_endian.o th_args.o th_crypto.o
+THLIBS_OBJ=th_util.o th_string.o th_ioctx.o th_args.o th_crypto.o
 
 
 SIDINFO_OBJ=sidlib.o sidinfo.o
--- a/sidinfo.c	Thu Feb 11 23:15:59 2016 +0200
+++ b/sidinfo.c	Thu Feb 11 23:18:58 2016 +0200
@@ -463,11 +463,11 @@
             break;
         case  5:
             if (psid->version >= 2)
-                PR("%s", NULL, siGetSIDClockStr(psid->flags));
+                PR("%s", NULL, si_get_sid_clock_str(psid->flags));
             break;
         case  6:
             if (psid->version >= 2)
-                PR("%s", NULL, siGetSIDModelStr(psid->flags));
+                PR("%s", NULL, si_get_sid_model_str(psid->flags));
             break;
 
         case  7: PR("%d", "$%08x", psid->dataOffset); break;
@@ -499,21 +499,22 @@
 BOOL argHandleFile(char *filename)
 {
     static PSIDHeader psid;
-    FILE *inFile = NULL, *outFile;
+    th_ioctx *inFile = NULL;
+    FILE *outFile;
     int index;
     BOOL shown = FALSE;
 
     optNFiles++;
     outFile = stdout;
 
-    if ((inFile = fopen(filename, "rb")) == NULL)
+    if ((inFile = th_io_fopen(&th_stdio_io_ops, filename, "rb")) == NULL)
     {
         THERR("Could not open file '%s'.\n", filename);
         return TRUE;
     }
 
     // Read PSID data
-    if (siReadPSIDFile(inFile, &psid) != 0)
+    if (si_read_sid_file(inFile, &psid) != 0)
     {
         THERR("Error reading %s\n", filename);
         goto error;
@@ -552,8 +553,7 @@
 
     // Shutdown
 error:
-    if (inFile != NULL)
-        fclose(inFile);
+    th_io_free(inFile);
 
     return TRUE;
 }
--- a/sidlib.c	Thu Feb 11 23:15:59 2016 +0200
+++ b/sidlib.c	Thu Feb 11 23:18:58 2016 +0200
@@ -1,13 +1,14 @@
 /*
  * SIDInfoLib - Way too simplistic PSID/RSID file library
- * Written by Matti 'ccr' Hämäläinen <ccr@tnsp.org>
+ * Written by Matti 'ccr' H�m�l�inen <ccr@tnsp.org>
  * (C) Copyright 2014-2016 Tecnic Software productions (TNSP)
  */
 #include "sidlib.h"
 #include "th_endian.h"
+#include "th_string.h"
 
 
-static void siAppendHash16(th_md5state_t *state, uint16_t data)
+static void si_append_hash16(th_md5state_t *state, uint16_t data)
 {
     uint8_t ib8[2];
     ib8[0] = data & 0xff;
@@ -16,35 +17,38 @@
 }
 
 
-int siReadPSIDFile(FILE *fh, PSIDHeader *psid)
+int si_read_sid_file(th_ioctx *ctx, PSIDHeader *psid)
 {
     th_md5state_t state;
-    uint8_t tmp8, *fileData = NULL;
+    uint8_t tmp8, *data = NULL;
     int index, ret = -1;
     size_t read;
     BOOL first;
 
     memset(psid, 0, sizeof(*psid));
 
-    if ((fileData = (uint8_t *) th_malloc(PSID_BUFFER_SIZE)) == NULL)
+    if ((data = (uint8_t *) th_malloc(PSID_BUFFER_SIZE)) == NULL)
     {
-        THERR("Error allocating temporary data buffer of %d bytes.\n",
+        th_io_error(ctx, THERR_MALLOC,
+            "Error allocating temporary data buffer of %d bytes.\n",
             PSID_BUFFER_SIZE);
         goto error;
     }
 
     // Read PSID header in
-    if (!th_fread_str(fh, (uint8_t *) psid->magic, PSID_MAGIC_LEN) ||
-        !th_fread_be16(fh, &psid->version) ||
-        !th_fread_be16(fh, &psid->dataOffset) ||
-        !th_fread_be16(fh, &psid->loadAddress) ||
-        !th_fread_be16(fh, &psid->initAddress) ||
-        !th_fread_be16(fh, &psid->playAddress) ||
-        !th_fread_be16(fh, &psid->nSongs) ||
-        !th_fread_be16(fh, &psid->startSong) ||
-        !th_fread_be32(fh, &psid->speed))
+    if (!thfread_str(ctx, (uint8_t *) psid->magic, PSID_MAGIC_LEN) ||
+        !thfread_be16(ctx, &psid->version) ||
+        !thfread_be16(ctx, &psid->dataOffset) ||
+        !thfread_be16(ctx, &psid->loadAddress) ||
+        !thfread_be16(ctx, &psid->initAddress) ||
+        !thfread_be16(ctx, &psid->playAddress) ||
+        !thfread_be16(ctx, &psid->nSongs) ||
+        !thfread_be16(ctx, &psid->startSong) ||
+        !thfread_be32(ctx, &psid->speed))
     {
-        THERR("Could not read PSID/RSID header.\n");
+        th_io_error(ctx, ctx->errno,
+            "Could not read PSID/RSID header: %s.\n",
+            th_error_str(ctx->errno));
         goto error;
     }
 
@@ -54,17 +58,20 @@
         psid->magic[1] != 'S' || psid->magic[2] != 'I' || psid->magic[3] != 'D' ||
         psid->version < 1 || psid->version > 3)
     {
-        THERR("Not a supported PSID or RSID file.\n");
+        th_io_error(ctx, THERR_NOT_SUPPORTED,
+            "Not a supported PSID or RSID file.\n");
         goto error;
     }
 
     psid->isRSID = psid->magic[0] == 'R';
 
-    if (!th_fread_str(fh, (uint8_t *)psid->sidName, PSID_STR_LEN) ||
-        !th_fread_str(fh, (uint8_t *)psid->sidAuthor, PSID_STR_LEN) ||
-        !th_fread_str(fh, (uint8_t *)psid->sidCopyright, PSID_STR_LEN))
+    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))
     {
-        THERR("Error reading SID file header.\n");
+        th_io_error(ctx, ctx->errno,
+            "Error reading SID file header: %s.\n",
+            th_error_str(ctx->errno));
         goto error;
     }
 
@@ -76,12 +83,14 @@
     if (psid->version >= 2)
     {
         // Yes, we need to
-        if (!th_fread_be16(fh, &psid->flags) ||
-            !th_fread_byte(fh, &psid->startPage) ||
-            !th_fread_byte(fh, &psid->pageLength) ||
-            !th_fread_be16(fh, &psid->reserved))
+        if (!thfread_be16(ctx, &psid->flags) ||
+            !thfread_byte(ctx, &psid->startPage) ||
+            !thfread_byte(ctx, &psid->pageLength) ||
+            !thfread_be16(ctx, &psid->reserved))
         {
-            THERR("Error reading PSID/RSID v2+ extra header data.\n");
+            th_io_error(ctx, ctx->errno,
+                "Error reading PSID/RSID v2+ extra header data: %s.\n",
+                th_error_str(ctx->errno));
             goto error;
         }
     }
@@ -94,36 +103,37 @@
     first = TRUE;
     do
     {
-        read = fread(fileData, sizeof(uint8_t), PSID_BUFFER_SIZE, fh);
+        read = thfread(data, sizeof(uint8_t), PSID_BUFFER_SIZE, ctx);
         psid->dataSize += read;
 
         if (first && psid->loadAddress == 0)
         {
             if (read < 4)
             {
-                THERR("Error reading song data, unexpectedly small file.\n");
+                th_io_error(ctx, THERR_FREAD,
+                    "Error reading song data, unexpectedly small file.\n");
                 goto error;
             }
 
             // Grab the actual load address
-            psid->loadAddress = TH_LE16_TO_NATIVE(*(uint16_t *) fileData);
+            psid->loadAddress = TH_LE16_TO_NATIVE(*(uint16_t *) data);
 
             // Strip load address (2 first bytes)
-            th_md5_append(&state, &fileData[2], read - 2);
+            th_md5_append(&state, &data[2], read - 2);
             first = FALSE;
         }
         else
         if (read > 0)
         {
             // Append "as is"
-            th_md5_append(&state, fileData, read);
+            th_md5_append(&state, data, read);
         }
-    } while (read > 0 && !feof(fh));
+    } while (read > 0 && !thfeof(ctx));
 
     // Append header data to hash
-    siAppendHash16(&state, psid->initAddress);
-    siAppendHash16(&state, psid->playAddress);
-    siAppendHash16(&state, psid->nSongs);
+    si_append_hash16(&state, psid->initAddress);
+    si_append_hash16(&state, psid->playAddress);
+    si_append_hash16(&state, psid->nSongs);
 
     // Append song speed data to hash
     tmp8 = psid->isRSID ? 60 : 0;
@@ -156,12 +166,12 @@
 
 error:
     // Free buffer
-    th_free(fileData);
+    th_free(data);
     return ret;
 }
 
 
-const char *siGetSIDClockStr(const int flags)
+const char *si_get_sid_clock_str(const int flags)
 {
     switch (flags & PSF_CLOCK_MASK)
     {
@@ -174,7 +184,7 @@
 }
 
 
-const char *siGetSIDModelStr(const int flags)
+const char *si_get_sid_model_str(const int flags)
 {
     switch (flags & PSF_MODEL_MASK)
     {
--- a/sidlib.h	Thu Feb 11 23:15:59 2016 +0200
+++ b/sidlib.h	Thu Feb 11 23:18:58 2016 +0200
@@ -7,8 +7,10 @@
 #define SIDLIB_H 1
 
 #include "th_util.h"
+#include "th_ioctx.h"
 #include "th_crypto.h"
 
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -71,10 +73,10 @@
 //
 // Functions
 //
-int             siReadPSIDFile(FILE *fh, PSIDHeader *psid);
+int             si_read_sid_file(th_ioctx *ctx, PSIDHeader *psid);
 
-const char *    siGetSIDClockStr(const int flags);
-const char *    siGetSIDModelStr(const int flags);
+const char *    si_get_sid_clock_str(const int flags);
+const char *    si_get_sid_model_str(const int flags);
 
 
 #ifdef __cplusplus