some fixes and improvements - xml2tsv - a simple xml-to-tsv converter, based on xmlparser DIR Log DIR Files DIR Refs DIR Tags DIR README DIR LICENSE --- DIR commit 5cb461a629973380fc58050bee2d8d893db7923d DIR parent 0fe049559d88b204e73332e205caa8c5936985d6 HTML Author: Hiltjo Posthuma <hiltjo@codemadness.org> Date: Fri, 3 Jan 2020 16:37:46 +0100 some fixes and improvements - print_no_cr: fix for modification of const buffer. - improve buffering of data handlers using the start and end handlers. - add XML entity conversion. - some white-space fixes. Diffstat: M xml2tsv.c | 47 ++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 17 deletions(-) --- DIR diff --git a/xml2tsv.c b/xml2tsv.c @@ -36,13 +36,13 @@ int stack_push(tstack_t *t, const char *c){ return 0; } return -1; -} +} char* stack_pop(tstack_t *t){ if (t->top >= 0) return t->st[t->top--]; return NULL; -} +} char* stack_peek(tstack_t *t){ if (t->top >= 0) @@ -61,17 +61,15 @@ void stack_init(tstack_t *t){ /* utility functions */ -void print_no_cr(FILE *f, const char *c){ - char *tmp = c; - while (c != NULL){ - tmp = strchr(c, '\n'); - if (tmp != NULL) - *tmp = '\0'; - fprintf(f, "%s", c); - if (tmp != NULL) - c = tmp + 1; - else - c = NULL; +void print_no_cr(FILE *f, const char *s){ + const char *tmp = s; + size_t len; + while (*tmp != '\0'){ + len = strcspn(tmp, "\n"); + fwrite(tmp, 1, len, f); + tmp += len; + if (tmp[len] == '\n') + tmp++; } } @@ -93,14 +91,20 @@ void xmlattr(XMLParser *x, const char *t, size_t tl, const char *a, size_t al, const char *v, size_t vl) { - printf("\t%s=%s", a, v); + printf("%s", v); } void xmlattrentity(XMLParser *x, const char *t, size_t tl, const char *a, size_t al, const char *v, size_t vl) { - printf("attrentity: %s\n", a); + char buf[16]; + int n; + + if ((n = xml_entitytostr(v, buf, sizeof(buf))) > 0) + xmlattr(x, t, tl, a, al, buf, (size_t)n); + else + xmlattr(x, t, tl, a, al, v, vl); } void @@ -111,17 +115,19 @@ xmlattrend(XMLParser *x, const char *t, size_t tl, const char *a, size_t al) void xmlattrstart(XMLParser *x, const char *t, size_t tl, const char *a, size_t al) { + printf("\t%s=", a); } void xmlcdatastart(XMLParser *x) { + printf("\t"); } void xmlcdata(XMLParser *x, const char *d, size_t dl) { - printf("\t%s", d); + print_no_cr(stdout, d); } void @@ -147,7 +153,6 @@ xmlcommentend(XMLParser *x) void xmldata(XMLParser *x, const char *d, size_t dl) { - printf("\t"); print_no_cr(stdout, d); } @@ -159,11 +164,19 @@ xmldataend(XMLParser *x) void xmldataentity(XMLParser *x, const char *d, size_t dl) { + char buf[16]; + int n; + + if ((n = xml_entitytostr(d, buf, sizeof(buf))) > 0) + xmldata(x, buf, (size_t)n); + else + xmldata(x, d, dl); } void xmldatastart(XMLParser *x) { + printf("\t"); } void