changeset 176:2e890e3d5684

Add functionality for escaping the separator characters (-l <str> option) in printed out string data.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 06 Jul 2018 21:09:35 +0300
parents 3d578c0043ae
children 6a73d17f0c34
files sidinfo.c
diffstat 1 files changed, 47 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/sidinfo.c	Mon Jul 02 00:03:45 2018 +0300
+++ b/sidinfo.c	Fri Jul 06 21:09:35 2018 +0300
@@ -6,6 +6,7 @@
 #include "th_args.h"
 #include "th_string.h"
 #include "th_file.h"
+#include "th_datastruct.h"
 #include "sidlib.h"
 #ifdef HAVE_ICONV
 #  include <iconv.h>
@@ -725,6 +726,43 @@
 }
 
 
+static char * siEscapeString(const char *str, const char *esc)
+{
+    if (str == NULL)
+        return NULL;
+
+    if (esc == NULL)
+        return th_strdup(str);
+
+    size_t len = 0, size = strlen(str) + 1;
+    char *buf = th_malloc(size);
+    if (buf == NULL)
+        return NULL;
+
+    while (*str)
+    {
+        if (strchr(esc, *str) != NULL || *str == '\\')
+        {
+            if (!th_strbuf_putch(&buf, &size, &len, '\\'))
+                goto err;
+        }
+        if (!th_strbuf_putch(&buf, &size, &len, *str))
+            goto err;
+
+        str++;
+    }
+
+    if (!th_strbuf_putch(&buf, &size, &len, 0))
+        goto err;
+
+    return buf;
+
+err:
+    th_free(buf);
+    return NULL;
+}
+
+
 static void siPrintStrEscapes(FILE *outFile, const char *str)
 {
     while (*str)
@@ -764,7 +802,7 @@
 static void siPrintPSIDInfoLine(FILE *outFile, BOOL *shown, const PSFStackItem *item, const char *d_str, const int d_int, const BOOL useConv)
 {
     const PSFOption *opt = &optPSOptions[item->cmd];
-    char *fmt, *str;
+    char *fmt, *str, *tmp;
 
     switch (opt->type)
     {
@@ -789,28 +827,25 @@
     siPrintFieldPrefix(outFile, opt);
 
 #ifdef HAVE_ICONV
-    char *tmp;
-
     if (setUseChConv && d_str != NULL && useConv)
-        tmp = siConvertCharset(setChConv, d_str);
+    {
+        char *tmp2 = siConvertCharset(setChConv, d_str);
+        tmp = siEscapeString(tmp2, optFieldSep);
+        th_free(tmp2);
+    }
     else
-        tmp = th_strdup(d_str);
-
-    str = siItemFormatStrPrint(fmt, opt, tmp, d_int);
+        tmp = siEscapeString(d_str, optFieldSep);
 #else
     (void) useConv;
-    str = siItemFormatStrPrint(fmt, opt, d_str, d_int);
+    tmp = siEscapeString(d_str, optFieldSep);
 #endif
 
-    if (str != NULL)
+    if ((str = siItemFormatStrPrint(fmt, opt, tmp, d_int)) != NULL)
         fputs(str, outFile);
 
     siPrintFieldSeparator(outFile);
     th_free(str);
-
-#ifdef HAVE_ICONV
     th_free(tmp);
-#endif
 
     *shown = TRUE;
 }