summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--xml2tsv.c38
1 files changed, 25 insertions, 13 deletions
diff --git a/xml2tsv.c b/xml2tsv.c
index 9ad033b..635ae37 100644
--- 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;
}
}
}