changeset 55:54b48086a1d0

Begin slight refactoring of how output is done.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 26 Dec 2015 19:37:04 +0200
parents c4b4acee5efa
children d2148880c71e
files sidinfo.c
diffstat 1 files changed, 87 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/sidinfo.c	Sat Dec 26 13:18:58 2015 +0200
+++ b/sidinfo.c	Sat Dec 26 19:37:04 2015 +0200
@@ -54,8 +54,14 @@
 
 static const PSFOption optPSFlags[] =
 {
+    { SIF_FILENAME       , "Filename"   , NULL },
     { SIF_TYPE           , "Type"       , NULL },
     { SIF_VERSION        , "Version"    , NULL },
+    { SIF_PLAYER_TYPE    , "PlayerType" , "Player type" },
+    { SIF_PLAYSID_TUNE   , "PlayerCompat", "Player compatibility" },
+    { SIF_VIDEO_CLOCK    , "VideoClock" , "Video clock speed" },
+    { SIF_SID_MODEL      , "SIDModel"   , "SID model" },
+
     { SIF_DATA_OFFS      , "DataOffs"   , "Data offset" },
     { SIF_DATA_SIZE      , "DataSize"   , "Data size" },
     { SIF_LOAD_ADDR      , "LoadAddr"   , "Load address" },
@@ -67,12 +73,6 @@
     { SIF_SID_AUTHOR     , "Author"     , NULL },
     { SIF_SID_COPYRIGHT  , "Copyright"  , NULL },
     { SIF_HASH           , "Hash"       , NULL },
-    { SIF_FILENAME       , "Filename"   , NULL },
-
-    { SIF_PLAYER_TYPE    , "PlayerType" , "Player type" },
-    { SIF_PLAYSID_TUNE   , "PlayerCompat", "Player compatibility" },
-    { SIF_VIDEO_CLOCK    , "VideoClock" , "Video clock speed" },
-    { SIF_SID_MODEL      , "SIDModel"   , "SID model" },
 
     { SIF_ALL            , "All"        , NULL },
 };
@@ -85,7 +85,7 @@
         optNoNamePrefix = FALSE,
         optHexadecimal = FALSE,
         optOneLine = FALSE;
-char *  optFieldSep = NULL;
+char    *optFieldSep = NULL;
 uint32_t optFields = SIF_ALL;
 int     optNFiles = 0;
 
@@ -165,11 +165,11 @@
     switch (found)
     {
         case -1:
-            THERR("No such flag '%s'.\n", opt);
+            THERR("No such field '%s'.\n", opt);
             return FALSE;
 
         case -2:
-            THERR("Flag '%s' is ambiguous.\n", opt);
+            THERR("Field '%s' is ambiguous.\n", opt);
             return FALSE;
 
         default:
@@ -439,6 +439,32 @@
 }
 
 
+const char *siGetSIDClockStr(int flags)
+{
+    switch (flags & PSF_CLOCK_MASK)
+    {
+        case PSF_CLOCK_UNKNOWN : return "Unknown";
+        case PSF_CLOCK_PAL     : return "PAL 50Hz";
+        case PSF_CLOCK_NTSC    : return "NTSC 60Hz";
+        case PSF_CLOCK_ANY     : return "PAL / NTSC";
+        default                : return "?";
+    }
+}
+
+
+const char *siGetSIDModelStr(int flags)
+{
+    switch (flags & PSF_MODEL_MASK)
+    {
+        case PSF_MODEL_UNKNOWN : return "Unknown";
+        case PSF_MODEL_MOS6581 : return "MOS6581";
+        case PSF_MODEL_MOS8580 : return "MOS8580";
+        case PSF_MODEL_ANY     : return "MOS6581 / MOS8580";
+        default                : return "?";
+    }
+}
+
+
 static void siPrintFieldPrefix(FILE *outFile, const char *name)
 {
     if (!optNoNamePrefix)
@@ -476,73 +502,64 @@
     }
 }
 
-#define PR(xindex, xfmt, xaltfmt, ...) siPrintPSIDInfoLine(outFile, &shown, xindex, xfmt, xaltfmt, __VA_ARGS__ )
-
-
-void siPrintPSIDInformation(FILE *outFile, const char *filename, const PSIDHeader *psid)
-{
-    BOOL shown = FALSE;
-
-    PR(13, "%s", NULL, filename);
+#define PR(xfmt, xaltfmt, ...) siPrintPSIDInfoLine(outFile, shown, xindex, xfmt, xaltfmt, __VA_ARGS__ )
 
-    PR( 0, "%s", NULL, psid->magic);
-    PR( 1, "%d.%d", NULL, (psid->version & 0xff), (psid->version >> 8));
-    PR(14, "%s", NULL, (psid->flags & PSF_PLAYER_TYPE) ? "Compute! SIDPlayer MUS" : "Normal built-in");
-    if (psid->version >= 2)
+void siPrintPSIDInformationField(FILE *outFile, const char *filename, const PSIDHeader *psid, BOOL *shown, const int xindex)
+{
+    switch (xindex)
     {
-        char *str;
-        PR(15, "%s", NULL, (psid->flags & PSF_PLAYSID_TUNE) ? "PlaySID" : "C64 compatible");
-
-        switch (psid->flags & PSF_CLOCK_MASK)
-        {
-            case PSF_CLOCK_UNKNOWN : str = "Unknown"; break;
-            case PSF_CLOCK_PAL     : str = "PAL 50Hz"; break;
-            case PSF_CLOCK_NTSC    : str = "NTSC 60Hz"; break;
-            case PSF_CLOCK_ANY     : str = "PAL / NTSC"; break;
-            default                : str = "?"; break;
-        }
-        PR(16, "%s", NULL, str);
+        case  0: PR("%s", NULL, filename); break;
+        case  1: PR("%s", NULL, psid->magic); break;
+        case  2: PR("%d.%d", NULL, (psid->version & 0xff), (psid->version >> 8)); break;
+        case  3: PR("%s", NULL, (psid->flags & PSF_PLAYER_TYPE) ? "Compute! SIDPlayer MUS" : "Normal built-in"); break;
+        case  4:
+            if (psid->version >= 2)
+                PR("%s", NULL, (psid->flags & PSF_PLAYSID_TUNE) ? "PlaySID" : "C64 compatible");
+            break;
+        case  5:
+            if (psid->version >= 2)
+                PR("%s", NULL, siGetSIDClockStr(psid->flags));
+            break;
+        case  6:
+            if (psid->version >= 2)
+                PR("%s", NULL, siGetSIDModelStr(psid->flags));
+            break;
 
-        switch (psid->flags & PSF_MODEL_MASK)
-        {
-            case PSF_MODEL_UNKNOWN : str = "Unknown"; break;
-            case PSF_MODEL_MOS6581 : str = "MOS6581"; break;
-            case PSF_MODEL_MOS8580 : str = "MOS8580"; break;
-            case PSF_MODEL_ANY     : str = "MOS6581 / MOS8580"; break;
-            default                : str = "?"; break;
-        }
-        PR(17, "%s", NULL, str);
-    }
+        case  7: PR("%d", "$%08x", psid->dataOffset); break;
+        case  8: PR("%d", "$%08x", psid->dataSize); break;
+        case  9: PR("%d", "$%04x", psid->loadAddress); break;
+        case 10: PR("%d", "$%04x", psid->initAddress); break;
+        case 11: PR("%d", "$%04x", psid->playAddress); break;
+        case 12: PR("%d", "$%04x", psid->nSongs); break;
+        case 13: PR("%d", "$%04x", psid->startSong); break;
+        case 14: PR("%s", NULL, psid->sidName); break;
+        case 15: PR("%s", NULL, psid->sidAuthor); break;
+        case 16: PR("%s", NULL, psid->sidCopyright); break;
 
-    PR( 2, "%d", "$%08x", psid->dataOffset);
-    PR( 3, "%d", "$%08x", psid->dataSize);
-    PR( 4, "%d", "$%04x", psid->loadAddress);
-    PR( 5, "%d", "$%04x", psid->initAddress);
-    PR( 6, "%d", "$%04x", psid->playAddress);
-    PR( 7, "%d", "$%04x", psid->nSongs);
-    PR( 8, "%d", "$%04x", psid->startSong);
-
-    PR( 9, "%s", NULL, psid->sidName);
-    PR(10, "%s", NULL, psid->sidAuthor);
-    PR(11, "%s", NULL, psid->sidCopyright);
-
-    if (optFields & SIF_HASH)
-    {
-        siPrintFieldPrefix(outFile, "Hash");
-        th_md5_print(outFile, psid->hash);
-        siPrintFieldSeparator(outFile);
+        case 17:
+            {
+                const PSFOption *opt = &optPSFlags[xindex];
+                if (optFormatStack || (optFields & opt->flag))
+                {
+                    siPrintFieldPrefix(outFile, "Hash");
+                    th_md5_print(outFile, psid->hash);
+                    siPrintFieldSeparator(outFile);
+                }
+            }
+            break;
     }
-
-    if (shown && optOneLine)
-        fprintf(outFile, "\n");
 }
 
 
 BOOL argHandleFile(char *filename)
 {
     static PSIDHeader psid;
-    static FILE *inFile = NULL;
+    FILE *inFile = NULL, *outFile;
+    int index;
+    BOOL shown = FALSE;
+
     optNFiles++;
+    outFile = stdout;
 
     if ((inFile = fopen(filename, "rb")) == NULL)
     {
@@ -558,7 +575,12 @@
     }
 
     // Output
-    siPrintPSIDInformation(stdout, filename, &psid);
+    {
+        for (index = 0; index < noptPSFlags - 1; index++)
+            siPrintPSIDInformationField(outFile, filename, &psid, &shown, index);
+        if (shown && optOneLine)
+            fprintf(outFile, "\n");
+    }
 
     // Shutdown
 error: