changeset 268:6312d33d1361

Refactor and improve the field formatting / printing to be more flexible.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 06 Jan 2020 00:59:35 +0200
parents ac6b5957b82b
children cea4a7df9efd
files sidinfo.c
diffstat 1 files changed, 44 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/sidinfo.c	Mon Jan 06 00:57:52 2020 +0200
+++ b/sidinfo.c	Mon Jan 06 00:59:35 2020 +0200
@@ -432,7 +432,7 @@
 
 
 int siItemFormatStrPrintDo(th_vprintf_ctx *ctx, th_vprintf_putch vputch, const char *fmt,
-    const PSFOption *opt, const char *d_str, const int d_int)
+    const int otype, const char *d_str, const int d_int)
 {
     int ret = 0;
 
@@ -523,7 +523,7 @@
                     return -104;
 
                 case 'o':
-                    if (opt->type != OTYPE_INT) return -120;
+                    if (otype != OTYPE_INT) return -120;
                     if ((ret = siItemFormatStrPutInt(ctx, vputch, d_int, 8, f_flags, f_width, f_prec, TRUE, th_vprintf_altfmt_oct)) == EOF)
                         goto out;
                     break;
@@ -531,14 +531,14 @@
                 case 'u':
                 case 'i':
                 case 'd':
-                    if (opt->type != OTYPE_INT) return -120;
+                    if (otype != OTYPE_INT) return -120;
                     if ((ret = siItemFormatStrPutInt(ctx, vputch, d_int, 10, f_flags, f_width, f_prec, *fmt == 'u', NULL)) == EOF)
                         goto out;
                     break;
 
                 case 'x':
                 case 'X':
-                    if (opt->type != OTYPE_INT) return -120;
+                    if (otype != OTYPE_INT) return -120;
                     if (*fmt == 'X')
                         f_flags |= TH_PF_UPCASE;
                     if ((ret = siItemFormatStrPutInt(ctx, vputch, d_int, 16, f_flags, f_width, f_prec, TRUE, th_vprintf_altfmt_hex)) == EOF)
@@ -546,7 +546,7 @@
                     break;
 
                 case 's':
-                    if (opt->type != OTYPE_STR) return -121;
+                    if (otype != OTYPE_STR) return -121;
                     if ((ret = th_vprintf_put_str(ctx, vputch, d_str, f_flags, f_width, f_prec)) == EOF)
                         goto out;
                     break;
@@ -583,7 +583,7 @@
 }
 
 
-char * siItemFormatStrPrint(const char *fmt, const PSFOption *opt, const char *d_str, const int d_int)
+char * siItemFormatStrPrint(const char *fmt, const int otype, const char *d_str, const int d_int)
 {
     th_vprintf_ctx ctx;
 
@@ -595,7 +595,7 @@
     if (ctx.buf == NULL)
         return NULL;
 
-    if (siItemFormatStrPrintDo(&ctx, siItemFormatStrPutCH, fmt, opt, d_str, d_int) <= 0)
+    if (siItemFormatStrPrintDo(&ctx, siItemFormatStrPutCH, fmt, otype, d_str, d_int) <= 0)
         goto err;
 
     if (siItemFormatStrPutCH(&ctx, 0) < 0)
@@ -623,7 +623,7 @@
 
     memset(&ctx, 0, sizeof(ctx));
 
-    return siItemFormatStrPrintDo(&ctx, siItemFormatStrPutCHNone, fmt, opt, NULL, 0) >= 0;
+    return siItemFormatStrPrintDo(&ctx, siItemFormatStrPutCHNone, fmt, opt->type, NULL, 0) >= 0;
 }
 
 
@@ -866,11 +866,17 @@
 }
 
 
+static void siPrintFieldPrefixName(FILE *outFile, const char *name)
+{
+    if (!optNoNamePrefix && optFieldOutput)
+        fprintf(outFile, optParsable ? "%s=" : "%-20s : ", name);
+}
+
+
 static void siPrintFieldPrefix(FILE *outFile, const PSFOption *opt)
 {
-    const char *name = (optParsable || opt->lname == NULL) ? opt->name : opt->lname;
-    if (!optNoNamePrefix && optFieldOutput)
-        fprintf(outFile, optParsable ? "%s=" : "%-20s : ", name);
+    siPrintFieldPrefixName(outFile,
+        (optParsable || opt->lname == NULL) ? opt->name : opt->lname);
 }
 
 
@@ -881,32 +887,34 @@
 }
 
 
-static void siPrintPSIDInfoLine(FILE *outFile, BOOL *shown,
-    const PSFStackItem *item, const char *d_str,
-    const int d_int, const BOOL useConv)
+static const char *siGetInfoFormat(const PSFStackItem *item, const int otype)
 {
-    const PSFOption *opt = &optPSOptions[item->cmd];
-    char *fmt, *str, *tmp;
-
-    switch (opt->type)
+    switch (otype)
     {
         case OTYPE_INT:
             if (item->flags & OFMT_FORMAT)
-                fmt = item->fmt;
+                return item->fmt;
             else
-                fmt = optHexadecimal ? "$%04x" : "%d";
-            break;
+                return optHexadecimal ? "$%04x" : "%d";
 
         case OTYPE_STR:
             if (item->flags & OFMT_FORMAT)
-                fmt = item->fmt;
+                return item->fmt;
             else
-                fmt = "%s";
-            break;
+                return "%s";
 
         default:
-            return;
+            return NULL;
     }
+}
+
+
+static void siPrintPSIDInfoLine(FILE *outFile, BOOL *shown,
+    const char *fmt, const int otype,
+    const char *d_str, const int d_int,
+    const BOOL useConv)
+{
+    char *str, *tmp;
 
     if (setUseChConv && d_str != NULL && useConv)
     {
@@ -917,12 +925,11 @@
     else
         tmp = siEscapeString(d_str, optEscapeChars);
 
-    siPrintFieldPrefix(outFile, opt);
-
-    if ((str = siItemFormatStrPrint(fmt, opt, tmp, d_int)) != NULL)
+    if ((str = siItemFormatStrPrint(fmt, otype, tmp, d_int)) != NULL)
         fputs(str, outFile);
 
     siPrintFieldSeparator(outFile);
+
     th_free(str);
     th_free(tmp);
 
@@ -930,8 +937,15 @@
 }
 
 
-#define PRS(d_str, d_conv) siPrintPSIDInfoLine(outFile, shown, item, d_str, -1, d_conv)
-#define PRI(d_int) siPrintPSIDInfoLine(outFile, shown, item, NULL, d_int, FALSE)
+#define PRS(d_str, d_conv) do { \
+        siPrintFieldPrefix(outFile, opt); \
+        siPrintPSIDInfoLine(outFile, shown, siGetInfoFormat(item, opt->type), opt->type, d_str, -1, d_conv); \
+    } while (0)
+
+#define PRI(d_int) do { \
+        siPrintFieldPrefix(outFile, opt); \
+        siPrintPSIDInfoLine(outFile, shown, siGetInfoFormat(item, opt->type), opt->type, NULL, d_int, FALSE); \
+    } while (0)
 
 
 static void siPrintPSIDInformationField(FILE *outFile, const char *filename,