summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--xml2tsv.c47
1 files changed, 30 insertions, 17 deletions
diff --git a/xml2tsv.c b/xml2tsv.c
index 5725531..ddfa22a 100644
--- 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