remove control chars - xml2tsv - a simple xml-to-tsv converter, based on xmlparser DIR Log DIR Files DIR Refs DIR Tags DIR README DIR LICENSE --- DIR commit 302088dbb8f0eb6bca0c3d97bb2066bad3d5d4c5 DIR parent 90c501f47d0a32cedca9a4ea27fbe27be9fb3be4 HTML Author: KatolaZ <katolaz@freaknet.org> Date: Sun, 5 Jan 2020 06:54:03 +0000 remove control chars Diffstat: M xml2tsv.c | 38 ++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 13 deletions(-) --- DIR diff --git a/xml2tsv.c b/xml2tsv.c @@ -14,6 +14,7 @@ #include <stdio.h> #include <string.h> +#include <ctype.h> #include "xml.h" #include "config.h" @@ -58,26 +59,37 @@ void stack_init(tstack_t *t){ /* utility functions */ +/* quote_print: quote \\, \n, \t, and strip other ctrl chars */ void quote_print(FILE *f, const char *s){ const char *tmp = s; size_t len; + int i; while (*tmp != '\0'){ len = strcspn(tmp, "\\\n\t"); - fwrite(tmp, 1, len, f); - tmp += len; - if (*tmp == '\n'){ - if (len > 0){ - fprintf(f, "\\n"); + for(i=0; i<len; i++, tmp++){ + if (!iscntrl((unsigned char)*tmp)){ + fwrite(tmp, 1, 1, f); } - tmp ++; - } - else if (*tmp == '\t'){ - fprintf(f, "\\t"); - tmp ++; } - else if (*tmp == '\\'){ - fprintf(f, "\\\\"); - tmp ++; + switch (*tmp){ + case '\n': + if (len > 0){ + fprintf(f, "\\n"); + } + tmp ++; + break; + case '\t': + fprintf(f, "\\t"); + tmp ++; + break; + case '\r': + fprintf(f, "\\r"); + tmp ++; + break; + case '\\': + fprintf(f, "\\\\"); + tmp ++; + break; } } }