diff options
author | KatolaZ <katolaz@freaknet.org> | 2021-12-08 21:44:11 +0000 |
---|---|---|
committer | KatolaZ <katolaz@freaknet.org> | 2021-12-08 21:44:11 +0000 |
commit | df38d2270241927e5436a0bc36ffb99044a8f4fb (patch) | |
tree | 5eb396ef0fb07e46456895e2178be75d3c0d1d15 | |
parent | b0601f2be7df28d3fab982344a2cc0df8960b012 (diff) |
count tag childrend to fix lack of SEP emission on data with leading whitespaces
-rw-r--r-- | xml2tsv.c | 36 |
1 files changed, 24 insertions, 12 deletions
@@ -26,8 +26,15 @@ /* tag stack */ typedef struct { + char s[STR_MAX]; + int ref; +} taginfo_t; + + + +typedef struct { int top; - char st[DEPTH_MAX][STR_MAX]; + taginfo_t st[DEPTH_MAX]; } tstack_t; int stack_empty(tstack_t *t){ @@ -37,22 +44,27 @@ int stack_empty(tstack_t *t){ int stack_push(tstack_t *t, const char *c){ if (t->top < DEPTH_MAX){ t->top ++; - strncpy(t->st[t->top], c, STR_MAX - 1); - t->st[t->top][STR_MAX - 1] = '\0'; + strncpy(t->st[t->top].s, c, STR_MAX - 1); + t->st[t->top].s[STR_MAX - 1] = '\0'; + t->st[t->top].ref = 0; + if (t->top){ + /* Increment the ref counter of the parent tag*/ + t->st[t->top-1].ref += 1; + } return 0; } return -1; } -char* stack_pop(tstack_t *t){ +taginfo_t* stack_pop(tstack_t *t){ if (!stack_empty(t)) - return t->st[t->top--]; + return t->st + t->top--; return NULL; } -char* stack_peek(tstack_t *t){ +taginfo_t* stack_peek(tstack_t *t){ if (!stack_empty(t)) - return t->st[t->top]; + return t->st + t->top; return NULL; } @@ -102,7 +114,7 @@ void print_cur_str(tstack_t *t){ int i; for (i=0; i<=t->top; i++){ putchar('/'); - fputs(t->st[i], stdout); + fputs(t->st[i].s, stdout); } } @@ -110,7 +122,7 @@ 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); + fputs(t->st[i].s, f); } } @@ -164,7 +176,7 @@ xmlcdata(XMLParser *x, const char *d, size_t dl) void xmldata(XMLParser *x, const char *d, size_t dl) { - if (strcspn(d, " \t\n") && emitsep){ + if (stack_peek(&st) || (strcspn(d, " \t\n") && emitsep)){ putchar(SEP); emitsep = FALSE; } @@ -202,7 +214,7 @@ xmltagend(XMLParser *x, const char *t, size_t tl, int isshort) if (stack_empty(&st)){ fprintf(stderr, "Error: tag-end '%s' before any open tag\n", t); } - tag = stack_pop(&st); + tag = stack_pop(&st)->s; if (strcmp(t, tag)){ fprintf(stderr, "Error: tag-end '%s' closes tag '%s'\n", t, tag); } @@ -212,7 +224,7 @@ void xmltagstart(XMLParser *x, const char *t, size_t tl) { if (stack_push(&st, t)){ - fprintf(stderr, "Error: stack full. Ignoring tag '%s' (parent tag: '%s')\n", t, stack_peek(&st)); + fprintf(stderr, "Error: stack full. Ignoring tag '%s' (parent tag: '%s')\n", t, stack_peek(&st)->s); return; } putchar('\n'); |