comparison sidinfo.c @ 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 e203761c4007
children 6a73d17f0c34
comparison
equal deleted inserted replaced
175:3d578c0043ae 176:2e890e3d5684
4 * (C) Copyright 2014-2018 Tecnic Software productions (TNSP) 4 * (C) Copyright 2014-2018 Tecnic Software productions (TNSP)
5 */ 5 */
6 #include "th_args.h" 6 #include "th_args.h"
7 #include "th_string.h" 7 #include "th_string.h"
8 #include "th_file.h" 8 #include "th_file.h"
9 #include "th_datastruct.h"
9 #include "sidlib.h" 10 #include "sidlib.h"
10 #ifdef HAVE_ICONV 11 #ifdef HAVE_ICONV
11 # include <iconv.h> 12 # include <iconv.h>
12 #endif 13 #endif
13 14
723 724
724 return TRUE; 725 return TRUE;
725 } 726 }
726 727
727 728
729 static char * siEscapeString(const char *str, const char *esc)
730 {
731 if (str == NULL)
732 return NULL;
733
734 if (esc == NULL)
735 return th_strdup(str);
736
737 size_t len = 0, size = strlen(str) + 1;
738 char *buf = th_malloc(size);
739 if (buf == NULL)
740 return NULL;
741
742 while (*str)
743 {
744 if (strchr(esc, *str) != NULL || *str == '\\')
745 {
746 if (!th_strbuf_putch(&buf, &size, &len, '\\'))
747 goto err;
748 }
749 if (!th_strbuf_putch(&buf, &size, &len, *str))
750 goto err;
751
752 str++;
753 }
754
755 if (!th_strbuf_putch(&buf, &size, &len, 0))
756 goto err;
757
758 return buf;
759
760 err:
761 th_free(buf);
762 return NULL;
763 }
764
765
728 static void siPrintStrEscapes(FILE *outFile, const char *str) 766 static void siPrintStrEscapes(FILE *outFile, const char *str)
729 { 767 {
730 while (*str) 768 while (*str)
731 { 769 {
732 if (*str == '\\') 770 if (*str == '\\')
762 800
763 801
764 static void siPrintPSIDInfoLine(FILE *outFile, BOOL *shown, const PSFStackItem *item, const char *d_str, const int d_int, const BOOL useConv) 802 static void siPrintPSIDInfoLine(FILE *outFile, BOOL *shown, const PSFStackItem *item, const char *d_str, const int d_int, const BOOL useConv)
765 { 803 {
766 const PSFOption *opt = &optPSOptions[item->cmd]; 804 const PSFOption *opt = &optPSOptions[item->cmd];
767 char *fmt, *str; 805 char *fmt, *str, *tmp;
768 806
769 switch (opt->type) 807 switch (opt->type)
770 { 808 {
771 case OTYPE_INT: 809 case OTYPE_INT:
772 if (item->flags & OFMT_FORMAT) 810 if (item->flags & OFMT_FORMAT)
787 } 825 }
788 826
789 siPrintFieldPrefix(outFile, opt); 827 siPrintFieldPrefix(outFile, opt);
790 828
791 #ifdef HAVE_ICONV 829 #ifdef HAVE_ICONV
792 char *tmp;
793
794 if (setUseChConv && d_str != NULL && useConv) 830 if (setUseChConv && d_str != NULL && useConv)
795 tmp = siConvertCharset(setChConv, d_str); 831 {
832 char *tmp2 = siConvertCharset(setChConv, d_str);
833 tmp = siEscapeString(tmp2, optFieldSep);
834 th_free(tmp2);
835 }
796 else 836 else
797 tmp = th_strdup(d_str); 837 tmp = siEscapeString(d_str, optFieldSep);
798
799 str = siItemFormatStrPrint(fmt, opt, tmp, d_int);
800 #else 838 #else
801 (void) useConv; 839 (void) useConv;
802 str = siItemFormatStrPrint(fmt, opt, d_str, d_int); 840 tmp = siEscapeString(d_str, optFieldSep);
803 #endif 841 #endif
804 842
805 if (str != NULL) 843 if ((str = siItemFormatStrPrint(fmt, opt, tmp, d_int)) != NULL)
806 fputs(str, outFile); 844 fputs(str, outFile);
807 845
808 siPrintFieldSeparator(outFile); 846 siPrintFieldSeparator(outFile);
809 th_free(str); 847 th_free(str);
810
811 #ifdef HAVE_ICONV
812 th_free(tmp); 848 th_free(tmp);
813 #endif
814 849
815 *shown = TRUE; 850 *shown = TRUE;
816 } 851 }
817 852
818 853