changeset 308:04502257ccdf

Add default formats for certain fields, but use them only for the standard output and not for parsable output.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 10 Jan 2020 02:04:49 +0200
parents 696e93dd7ecc
children ff93c168c4aa
files sidinfo.c
diffstat 1 files changed, 37 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/sidinfo.c	Thu Jan 09 23:29:15 2020 +0200
+++ b/sidinfo.c	Fri Jan 10 02:04:49 2020 +0200
@@ -75,40 +75,42 @@
     char *name;
     char *lname;
     int type;
+    char *dfmt;
 } PSFOption;
 
 
+#define HEX_ADDR "%d ($%04x)"
+
 static const PSFOption optPSOptions[] =
 {
-    { "Filename"     , NULL                   , OTYPE_STR },
-    { "Type"         , NULL                   , OTYPE_STR },
-    { "Version"      , NULL                   , OTYPE_STR },
-    { "PlayerType"   , "Player type"          , OTYPE_STR },
-    { "PlayerCompat" , "Player compatibility" , OTYPE_STR },
-    { "VideoClock"   , "Video clock speed"    , OTYPE_STR },
-    { "SIDModel"     , "SID model"            , OTYPE_STR },
+    { "Filename"     , NULL                   , OTYPE_STR    , NULL },
+    { "Type"         , NULL                   , OTYPE_STR    , NULL },
+    { "Version"      , NULL                   , OTYPE_STR    , NULL },
+    { "PlayerType"   , "Player type"          , OTYPE_STR    , NULL },
+    { "PlayerCompat" , "Player compatibility" , OTYPE_STR    , NULL },
+    { "VideoClock"   , "Video clock speed"    , OTYPE_STR    , NULL },
+    { "SIDModel"     , "SID model"            , OTYPE_STR    , NULL },
 
-    { "DataOffs"     , "Data offset"          , OTYPE_INT },
-    { "DataSize"     , "Data size"            , OTYPE_INT },
-    { "LoadAddr"     , "Load address"         , OTYPE_INT },
-    { "InitAddr"     , "Init address"         , OTYPE_INT },
-    { "PlayAddr"     , "Play address"         , OTYPE_INT },
-    { "Songs"        , "Songs"                , OTYPE_INT },
-    { "StartSong"    , "Start song"           , OTYPE_INT },
+    { "DataOffs"     , "Data offset"          , OTYPE_INT    , HEX_ADDR },
+    { "DataSize"     , "Data size"            , OTYPE_INT    , HEX_ADDR },
+    { "LoadAddr"     , "Load address"         , OTYPE_INT    , HEX_ADDR },
+    { "InitAddr"     , "Init address"         , OTYPE_INT    , HEX_ADDR },
+    { "PlayAddr"     , "Play address"         , OTYPE_INT    , HEX_ADDR },
+    { "Songs"        , "Songs"                , OTYPE_INT    , "%d" },
+    { "StartSong"    , "Start song"           , OTYPE_INT    , "%d" },
 
-    { "SID2Model"    , "2nd SID model"        , OTYPE_STR },
-    { "SID3Model"    , "3rd SID model"        , OTYPE_STR },
-    { "SID2Addr"     , "2nd SID address"      , OTYPE_INT },
-    { "SID3Addr"     , "3rd SID address"      , OTYPE_INT },
+    { "SID2Model"    , "2nd SID model"        , OTYPE_STR    , NULL },
+    { "SID3Model"    , "3rd SID model"        , OTYPE_STR    , NULL },
+    { "SID2Addr"     , "2nd SID address"      , OTYPE_INT    , HEX_ADDR },
+    { "SID3Addr"     , "3rd SID address"      , OTYPE_INT    , HEX_ADDR },
 
-    { "Name"         , NULL                   , OTYPE_STR },
-    { "Author"       , NULL                   , OTYPE_STR },
-    { "Copyright"    , NULL                   , OTYPE_STR },
-    { "Hash"         , NULL                   , OTYPE_STR },
+    { "Name"         , NULL                   , OTYPE_STR    , NULL },
+    { "Author"       , NULL                   , OTYPE_STR    , NULL },
+    { "Copyright"    , NULL                   , OTYPE_STR    , NULL },
+    { "Hash"         , NULL                   , OTYPE_STR    , NULL },
 
-    { "Songlengths"  , "Song lengths"         , OTYPE_OTHER },
-
-    { "STIL"         , "STIL information"     , OTYPE_OTHER },
+    { "Songlengths"  , "Song lengths"         , OTYPE_OTHER  , NULL },
+    { "STIL"         , "STIL information"     , OTYPE_OTHER  , NULL },
 };
 
 static const int noptPSOptions = sizeof(optPSOptions) / sizeof(optPSOptions[0]);
@@ -378,6 +380,7 @@
 
             memset(&item, 0, sizeof(item));
             item.cmd = found;
+            item.fmt = th_strdup(optPSOptions[found].dfmt);
 
             if (!siStackAddItem(stack, &item))
                 return FALSE;
@@ -885,6 +888,7 @@
 
     case 7:
         optFieldOutput = FALSE;
+        optParsable = FALSE;
         if (!argParsePSFormatStr(&optFormat, optArg))
             return FALSE;
         break;
@@ -1012,13 +1016,13 @@
     switch (otype)
     {
         case OTYPE_INT:
-            if (item->flags & OFMT_FORMAT)
+            if (!optParsable && item->fmt != NULL)
                 return item->fmt;
             else
                 return optHexadecimal ? "$%04x" : "%d";
 
         case OTYPE_STR:
-            if (item->flags & OFMT_FORMAT)
+            if (!optParsable && item->fmt != NULL)
                 return item->fmt;
             else
                 return "%s";
@@ -1488,9 +1492,10 @@
     THMSG(2, "Requested output LANG='%s', use charset conversion=%s\n",
         setLang, setUseOutConv ? "yes" : "no");
 
-    if (optOneLineFieldSep != NULL)
+    if (optOneLineFieldSep != NULL ||
+        (!optFieldOutput && optFormat.nitems > 0))
     {
-        // For one-line format, disable parsable and prefixes
+        // For one-line format and formatted output (-F), disable parsable
         optParsable = FALSE;
 
         // If no escape chars have been set, use the field separator(s)
@@ -1501,14 +1506,14 @@
     if (optFieldOutput && optFormat.nitems == 0)
     {
         // For standard field output, push standard items to format stack
-        PSFStackItem item;
-
-        memset(&item, 0, sizeof(item));
         siClearStack(&optFormat);
 
         for (int i = 0; i < noptPSOptions; i++)
         {
+            PSFStackItem item;
+            memset(&item, 0, sizeof(item));
             item.cmd = i;
+            item.fmt = th_strdup(optPSOptions[i].dfmt);
             siStackAddItem(&optFormat, &item);
         }
     }