changeset 316:b0c844b39516

Move and rename siEscapeString() to sidutil_escape_string() and siPrintStrEscapes() to sidutil_print_string_escaped().
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 11 Jan 2020 18:34:34 +0200
parents 67392d7ef391
children 6291b08730df
files sidinfo.c sidutil.c sidutil.h
diffstat 3 files changed, 66 insertions(+), 62 deletions(-) [+]
line wrap: on
line diff
--- a/sidinfo.c	Sat Jan 11 18:31:15 2020 +0200
+++ b/sidinfo.c	Sat Jan 11 18:34:34 2020 +0200
@@ -699,65 +699,6 @@
 }
 
 
-static char * siEscapeString(const char *str, const char *esc)
-{
-    size_t len, size;
-    char *buf;
-
-    if (str == NULL)
-        return NULL;
-
-    if (esc == NULL)
-        return th_strdup(str);
-
-    size = strlen(str) + 1;
-    if ((buf = th_malloc(size)) == NULL)
-        return NULL;
-
-    for (len = 0; *str; 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;
-    }
-
-    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)
-    {
-        if (*str == '\\')
-        switch (*(++str))
-        {
-            case 'n': fputc('\n', outFile); break;
-            case 'r': fputc('\r', outFile); break;
-            case 't': fputc('\r', outFile); break;
-            case '\\': fputc('\\', outFile); break;
-            default: fputc(*str, outFile); break;
-        }
-        else
-            fputc(*str, outFile);
-
-        str++;
-    }
-}
-
-
 static void siPrintFieldPrefixName(FILE *outFile, const char *name, const BOOL multifield)
 {
     if (optFieldNamePrefix)
@@ -821,11 +762,11 @@
     if (d_str != NULL && setChConv.enabled && convert)
     {
         char *tmp2 = sidutil_chconv_convert(&setChConv, d_str);
-        tmp = siEscapeString(tmp2, optEscapeChars);
+        tmp = sidutil_escape_string(tmp2, optEscapeChars);
         th_free(tmp2);
     }
     else
-        tmp = siEscapeString(d_str, optEscapeChars);
+        tmp = sidutil_escape_string(d_str, optEscapeChars);
 
     if ((str = siItemFormatStrPrint(fmt, otype, tmp, d_int)) != NULL)
         fputs(str, outFile);
@@ -1074,7 +1015,7 @@
         switch (item->cmd)
         {
             case -1:
-                siPrintStrEscapes(outFile, item->str);
+                sidutil_print_string_escaped(outFile, item->str);
                 break;
 
             case -2:
--- a/sidutil.c	Sat Jan 11 18:31:15 2020 +0200
+++ b/sidutil.c	Sat Jan 11 18:34:34 2020 +0200
@@ -9,6 +9,65 @@
 #include "th_datastruct.h"
 
 
+char * sidutil_escape_string(const char *str, const char *escchars)
+{
+    size_t len, size;
+    char *buf;
+
+    if (str == NULL)
+        return NULL;
+
+    if (escchars == NULL)
+        return th_strdup(str);
+
+    size = strlen(str) + 1;
+    if ((buf = th_malloc(size)) == NULL)
+        return NULL;
+
+    for (len = 0; *str; str++)
+    {
+        if (strchr(escchars, *str) != NULL || *str == '\\')
+        {
+            if (!th_strbuf_putch(&buf, &size, &len, '\\'))
+                goto err;
+        }
+
+        if (!th_strbuf_putch(&buf, &size, &len, *str))
+            goto err;
+    }
+
+    if (!th_strbuf_putch(&buf, &size, &len, 0))
+        goto err;
+
+    return buf;
+
+err:
+    th_free(buf);
+    return NULL;
+}
+
+
+void sidutil_print_string_escaped(FILE *outFile, const char *str)
+{
+    while (*str)
+    {
+        if (*str == '\\')
+        switch (*(++str))
+        {
+            case 'n': fputc('\n', outFile); break;
+            case 'r': fputc('\r', outFile); break;
+            case 't': fputc('\r', outFile); break;
+            case '\\': fputc('\\', outFile); break;
+            default: fputc(*str, outFile); break;
+        }
+        else
+            fputc(*str, outFile);
+
+        str++;
+    }
+}
+
+
 void sidutil_print_license(void)
 {
     printf("%s - %s\n%s\n", th_prog_name, th_prog_desc, th_prog_author);
--- a/sidutil.h	Sat Jan 11 18:31:15 2020 +0200
+++ b/sidutil.h	Sat Jan 11 18:34:34 2020 +0200
@@ -59,6 +59,10 @@
 // Functions
 //
 void           sidutil_print_license(void);
+
+char *         sidutil_escape_string(const char *str, const char *escchars);
+void           sidutil_print_string_escaped(FILE *outFile, const char *str);
+
 const char *   sidutil_strip_hvsc_path(const char *hvscPath, const char *filename);
 char *         sidutil_check_hvsc_file(const char *hvscPath, const char *filebase, const char *fext);