diff options
author | KatolaZ <katolaz@freaknet.org> | 2020-01-05 06:54:03 +0000 |
---|---|---|
committer | KatolaZ <katolaz@freaknet.org> | 2020-01-05 06:54:03 +0000 |
commit | 302088dbb8f0eb6bca0c3d97bb2066bad3d5d4c5 (patch) | |
tree | 6c2bdc57e49594e89d70f634d9b60c0a92065903 | |
parent | 90c501f47d0a32cedca9a4ea27fbe27be9fb3be4 (diff) |
remove control chars
-rw-r--r-- | xml2tsv.c | 38 |
1 files changed, 25 insertions, 13 deletions
@@ -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; } } } |