changeset 180:6913c5dbbb58

Add separate escape characters option (-e <chars>), but make -l <separator> option to set escape chars if it has not been explicitly set via -e.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 07 Jul 2018 00:05:45 +0300
parents 63a0069ab217
children 5bc837384177
files sidinfo.c
diffstat 1 files changed, 21 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/sidinfo.c	Fri Jul 06 21:11:26 2018 +0300
+++ b/sidinfo.c	Sat Jul 07 00:05:45 2018 +0300
@@ -99,9 +99,9 @@
         optParsable = FALSE,
         optNoNamePrefix = FALSE,
         optHexadecimal = FALSE,
-        optOneLine = FALSE,
         optFieldOutput = TRUE;
-char    *optFieldSep = NULL;
+char    *optOneLineFieldSep = NULL,
+        *optEscapeChars = NULL;
 int     optNFiles = 0;
 
 PSFStack optFormat;
@@ -124,6 +124,7 @@
     { 2, 'p', "parsable",   "Output in script-parsable format", OPT_NONE },
     { 5, 'n', "noprefix",   "Output without field name prefix", OPT_NONE },
     { 6, 'l', "line",       "Output in one line format, -l <field separator>", OPT_ARGREQ },
+    {11, 'e', "escape",     "Escape these characters in fields (see note)", OPT_ARGREQ },
     { 3, 'f', "fields",     "Show only specified field(s)", OPT_ARGREQ },
     { 4, 'x', "hex",        "Use hexadecimal values", OPT_NONE },
     { 7, 'F', "format",     "Use given format string (see below)", OPT_ARGREQ },
@@ -204,6 +205,9 @@
         "\n"
         "When specifying HVSC path, it is preferable to use -H/--hvsc option,\n"
         "as STIL.txt and Songlengths.txt will be automatically used from there.\n"
+        "\n"
+        "NOTE: One line output (-l <field separator>) also sets escape characters\n"
+        "(option -e <chars>), if escape characters have NOT been separately set.\n"
         , th_prog_name);
 }
 
@@ -701,8 +705,7 @@
         break;
 
     case 6:
-        optOneLine = TRUE;
-        optFieldSep = optArg;
+        optOneLineFieldSep = optArg;
         break;
 
     case 7:
@@ -719,6 +722,10 @@
         setSLDBPath = th_strdup(optArg);
         break;
 
+    case 11:
+        optEscapeChars = optArg;
+        break;
+
     default:
         THERR("Unknown option '%s'.\n", currArg);
         return FALSE;
@@ -797,7 +804,7 @@
 static void siPrintFieldSeparator(FILE *outFile)
 {
     if (optFieldOutput)
-        fputs(optOneLine ? optFieldSep : "\n", outFile);
+        fputs(optOneLineFieldSep != NULL ? optOneLineFieldSep : "\n", outFile);
 }
 
 
@@ -832,14 +839,14 @@
     if (setUseChConv && d_str != NULL && useConv)
     {
         char *tmp2 = siConvertCharset(setChConv, d_str);
-        tmp = siEscapeString(tmp2, optFieldSep);
+        tmp = siEscapeString(tmp2, optEscapeChars);
         th_free(tmp2);
     }
     else
-        tmp = siEscapeString(d_str, optFieldSep);
+        tmp = siEscapeString(d_str, optEscapeChars);
 #else
     (void) useConv;
-    tmp = siEscapeString(d_str, optFieldSep);
+    tmp = siEscapeString(d_str, optEscapeChars);
 #endif
 
     if ((str = siItemFormatStrPrint(fmt, opt, tmp, d_int)) != NULL)
@@ -1004,7 +1011,7 @@
 
     if (optFieldOutput)
     {
-        if (shown && optOneLine)
+        if (shown && optOneLineFieldSep != NULL)
             fprintf(outFile, "\n");
     }
 
@@ -1047,11 +1054,15 @@
         argHandleOpt, argHandleFile, OPTH_ONLY_OPTS))
         return -1;
 
-    if (optOneLine)
+    if (optOneLineFieldSep != NULL)
     {
         // For one-line format, disable parsing and prefixes
         optParsable = FALSE;
         optNoNamePrefix = TRUE;
+
+        // If no escape chars have been set, use the field separator(s)
+        if (optEscapeChars == NULL)
+            optEscapeChars = optOneLineFieldSep;
     }
 
     if (optFieldOutput && optFormat.nitems == 0)