diff options
Diffstat (limited to 'xml2tsv.c')
-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; } } } |