diff options
author | Hiltjo Posthuma <hiltjo@codemadness.org> | 2020-09-30 11:42:07 +0100 |
---|---|---|
committer | KatolaZ <katolaz@freaknet.org> | 2020-09-30 11:42:07 +0100 |
commit | 60c249ec24ab865c4a55759c7ffde2da99530b1d (patch) | |
tree | 38494b9c66d9e915a6de57e89f3720627aaa1f6a /xml2tsv.c | |
parent | b416c171bb34297d7f8bc4c027de7136a113d144 (diff) |
several improvements and more efficient xml parser
Diffstat (limited to 'xml2tsv.c')
-rw-r--r-- | xml2tsv.c | 93 |
1 files changed, 30 insertions, 63 deletions
@@ -64,7 +64,7 @@ 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){ +void quote_print(const char *s){ const char *tmp = s; size_t len; int i; @@ -72,36 +72,45 @@ void quote_print(FILE *f, const char *s){ len = strcspn(tmp, "\\\n\t"); for(i=0; i<len; i++, tmp++){ if (!iscntrl((unsigned char)*tmp)){ - fwrite(tmp, 1, 1, f); + putchar(*tmp); } } switch (*tmp){ case '\n': if (len > 0){ - fprintf(f, "\\n"); + fputs("\\n", stdout); } tmp ++; break; case '\t': - fprintf(f, "\\t"); + fputs("\\t", stdout); tmp ++; break; case '\r': - fprintf(f, "\\r"); + fputs("\\r", stdout); tmp ++; break; case '\\': - fprintf(f, "\\\\"); + fputs("\\\\", stdout); tmp ++; break; } } } -void print_cur_str(FILE *f, tstack_t *t){ +void print_cur_str(tstack_t *t){ int i; for (i=0; i<=t->top; i++){ - fprintf(f, "/%s", t->st[i]); + putchar('/'); + fputs(t->st[i], stdout); + } +} + +void print_cur_str_fp(FILE *f, tstack_t *t){ + int i; + for (i=0; i<=t->top; i++){ + fputc('/', f); + fputs(t->st[i], f); } } @@ -110,13 +119,13 @@ void print_cur_str(FILE *f, tstack_t *t){ tstack_t st; char emitsep; -/* xml callbacks */ +/* XML callbacks */ void xmlattr(XMLParser *x, const char *t, size_t tl, const char *a, size_t al, const char *v, size_t vl) { - printf("%s", v); + fputs(v, stdout); } void @@ -133,56 +142,33 @@ xmlattrentity(XMLParser *x, const char *t, size_t tl, const char *a, size_t al, } void -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("%c%s%c", SEP, a, SATTR); + putchar(SEP); + fputs(a, stdout); + putchar(SATTR); } void xmlcdatastart(XMLParser *x) { - printf("%c", SEP); + putchar(SEP); } void xmlcdata(XMLParser *x, const char *d, size_t dl) { - quote_print(stdout, d); -} - -void -xmlcdataend(XMLParser *x) -{ -} - -void -xmlcommentstart(XMLParser *x) -{ -} - -void -xmlcomment(XMLParser *x, const char *c, size_t cl) -{ -} - -void -xmlcommentend(XMLParser *x) -{ + quote_print(d); } void xmldata(XMLParser *x, const char *d, size_t dl) { if (strcspn(d, " \t\n") && emitsep){ - printf("%c", SEP); + putchar(SEP); emitsep = FALSE; } - quote_print(stdout, d); + quote_print(d); } void @@ -220,12 +206,6 @@ xmltagend(XMLParser *x, const char *t, size_t tl, int isshort) if (strcmp(t, tag)){ fprintf(stderr, "Error: tag-end '%s' closes tag '%s'\n", t, tag); } - -/* if (isshort) { - printf("\n"); - print_cur_str(stdout, &st); - } -*/ } void @@ -235,13 +215,8 @@ xmltagstart(XMLParser *x, const char *t, size_t tl) fprintf(stderr, "Error: stack full. Ignoring tag '%s' (parent tag: '%s')\n", t, stack_peek(&st)); return; } - printf("\n"); - print_cur_str(stdout, &st); -} - -void -xmltagstartparsed(XMLParser *x, const char *t, size_t tl, int isshort) -{ + putchar('\n'); + print_cur_str(&st); } int @@ -252,30 +227,22 @@ main(void) XMLParser x = { 0 }; x.xmlattr = xmlattr; - x.xmlattrend = xmlattrend; x.xmlattrstart = xmlattrstart; x.xmlattrentity = xmlattrentity; x.xmlcdatastart = xmlcdatastart; x.xmlcdata = xmlcdata; - x.xmlcdataend = xmlcdataend; - x.xmlcommentstart = xmlcommentstart; - x.xmlcomment = xmlcomment; - x.xmlcommentend = xmlcommentend; x.xmldata = xmldata; x.xmldataend = xmldataend; x.xmldataentity = xmldataentity; x.xmldatastart = xmldatastart; x.xmltagend = xmltagend; x.xmltagstart = xmltagstart; - x.xmltagstartparsed = xmltagstartparsed; - - x.getnext = getchar; xml_parse(&x); - printf("\n"); + putchar('\n'); if (! stack_empty(&st)) { fprintf(stderr, "Error: tags still open at EOF: "); - print_cur_str(stderr, &st); + print_cur_str_fp(stderr, &st); fprintf(stderr, "\n"); } return 0; |