summaryrefslogtreecommitdiff
path: root/xml2tsv.c
blob: 7363ce6a0e3bbd026f81d278210d86c689cf81a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* (c) 2020 Vincenzo "KatolaZ" Nicosia <katolaz@freaknet.org>
* 
* A simple xml-to-tsv converter, based on xmlparser by Hiltjo Posthuma
* http://codemadness.org/git/xmlparser/
*
* You can use, distribute, modify, and/or redistribute this program under
* the terms of the ISC LICENSE. See LICENSE for details.
*
*/


#include <sys/types.h>

#include <stdio.h>
#include <string.h>

#include "xml.h"
#include "config.h"

/* tag stack */

typedef struct {
	int top;
	char st[DEPTH_MAX][STR_MAX];
} tstack_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';
		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)
		return t->st[t->top];
	return NULL;
}

int stack_empty(tstack_t *t){
	return (t->top < 0);
}

void stack_init(tstack_t *t){
	t->top = -1;
}


/* utility functions */

void print_no_cr(FILE *f, const char *s){
	const char *tmp = s;
	size_t len;
	while (*tmp != '\0'){
		len = strcspn(tmp, "\n\t");
		fwrite(tmp, 1, len, f);
		tmp += len;
		if (*tmp == '\n'){
			if (len > 0){
				fprintf(f, "\\n");
			}
			tmp ++;
		}
		else if (*tmp == '\t'){
			fprintf(f, "\\t");
			tmp ++;
		}
	}
}

void print_cur_str(FILE *f, tstack_t *t){
	int i;
	for (i=0; i<=t->top; i++){
		fprintf(f, "/%s", t->st[i]);
	}
}

/* global variables */

tstack_t st;


/* 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);
}

void
xmlattrentity(XMLParser *x, const char *t, size_t tl, const char *a, size_t al,
              const char *v, size_t vl)
{
	static 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
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);
}

void
xmlcdatastart(XMLParser *x)
{
	printf("%c", SEP);
}

void
xmlcdata(XMLParser *x, const char *d, size_t dl)
{
	print_no_cr(stdout, d);
}

void
xmlcdataend(XMLParser *x)
{
}

void
xmlcommentstart(XMLParser *x)
{
}

void
xmlcomment(XMLParser *x, const char *c, size_t cl)
{
}

void
xmlcommentend(XMLParser *x)
{
}

void
xmldata(XMLParser *x, const char *d, size_t dl)
{
	print_no_cr(stdout, d);
}

void
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("%c", SEP);
}

void
xmltagend(XMLParser *x, const char *t, size_t tl, int isshort)
{
	char *tag;
	if (stack_empty(&st)){
		fprintf(stderr, "Error: tag-end '%s' before any open tag", t);
	}
	tag = stack_pop(&st);
	if (strcmp(t, tag)){
		fprintf(stderr, "Error: tag-end '%s' closes tag '%s'", t, tag);
	}

	if (isshort) {
		printf("\n");
		print_cur_str(stdout, &st);
	}
}

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));
		return;
	}
	printf("\n");
	print_cur_str(stdout, &st);
}

void
xmltagstartparsed(XMLParser *x, const char *t, size_t tl, int isshort)
{
	/* printf("inside tagstartparsed\n"); */
}

int
main(void)
{
	stack_init(&st);
	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");
	return 0;
}