comparison sidinfo.c @ 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 f3ba2ba894b1
comparison
equal deleted inserted replaced
315:67392d7ef391 316:b0c844b39516
697 697
698 return TRUE; 698 return TRUE;
699 } 699 }
700 700
701 701
702 static char * siEscapeString(const char *str, const char *esc)
703 {
704 size_t len, size;
705 char *buf;
706
707 if (str == NULL)
708 return NULL;
709
710 if (esc == NULL)
711 return th_strdup(str);
712
713 size = strlen(str) + 1;
714 if ((buf = th_malloc(size)) == NULL)
715 return NULL;
716
717 for (len = 0; *str; str++)
718 {
719 if (strchr(esc, *str) != NULL || *str == '\\')
720 {
721 if (!th_strbuf_putch(&buf, &size, &len, '\\'))
722 goto err;
723 }
724
725 if (!th_strbuf_putch(&buf, &size, &len, *str))
726 goto err;
727 }
728
729 if (!th_strbuf_putch(&buf, &size, &len, 0))
730 goto err;
731
732 return buf;
733
734 err:
735 th_free(buf);
736 return NULL;
737 }
738
739
740 static void siPrintStrEscapes(FILE *outFile, const char *str)
741 {
742 while (*str)
743 {
744 if (*str == '\\')
745 switch (*(++str))
746 {
747 case 'n': fputc('\n', outFile); break;
748 case 'r': fputc('\r', outFile); break;
749 case 't': fputc('\r', outFile); break;
750 case '\\': fputc('\\', outFile); break;
751 default: fputc(*str, outFile); break;
752 }
753 else
754 fputc(*str, outFile);
755
756 str++;
757 }
758 }
759
760
761 static void siPrintFieldPrefixName(FILE *outFile, const char *name, const BOOL multifield) 702 static void siPrintFieldPrefixName(FILE *outFile, const char *name, const BOOL multifield)
762 { 703 {
763 if (optFieldNamePrefix) 704 if (optFieldNamePrefix)
764 { 705 {
765 if (optFieldOutput && optOneLineFieldSep == NULL) 706 if (optFieldOutput && optOneLineFieldSep == NULL)
819 char *str, *tmp; 760 char *str, *tmp;
820 761
821 if (d_str != NULL && setChConv.enabled && convert) 762 if (d_str != NULL && setChConv.enabled && convert)
822 { 763 {
823 char *tmp2 = sidutil_chconv_convert(&setChConv, d_str); 764 char *tmp2 = sidutil_chconv_convert(&setChConv, d_str);
824 tmp = siEscapeString(tmp2, optEscapeChars); 765 tmp = sidutil_escape_string(tmp2, optEscapeChars);
825 th_free(tmp2); 766 th_free(tmp2);
826 } 767 }
827 else 768 else
828 tmp = siEscapeString(d_str, optEscapeChars); 769 tmp = sidutil_escape_string(d_str, optEscapeChars);
829 770
830 if ((str = siItemFormatStrPrint(fmt, otype, tmp, d_int)) != NULL) 771 if ((str = siItemFormatStrPrint(fmt, otype, tmp, d_int)) != NULL)
831 fputs(str, outFile); 772 fputs(str, outFile);
832 773
833 th_free(str); 774 th_free(str);
1072 { 1013 {
1073 PSFStackItem *item = &optFormat.items[index]; 1014 PSFStackItem *item = &optFormat.items[index];
1074 switch (item->cmd) 1015 switch (item->cmd)
1075 { 1016 {
1076 case -1: 1017 case -1:
1077 siPrintStrEscapes(outFile, item->str); 1018 sidutil_print_string_escaped(outFile, item->str);
1078 break; 1019 break;
1079 1020
1080 case -2: 1021 case -2:
1081 fputc(item->chr, outFile); 1022 fputc(item->chr, outFile);
1082 break; 1023 break;