summaryrefslogtreecommitdiff
path: root/src/include/xml/decode.c
blob: 1962f7eb0ecff45eb9f17a30b2ed14c092811f49 (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
#include <stdlib.h>
#include <string.h>

#include "xml.h"

static char *get_tag(char *xml, unsigned long *pos);
static attr_t *get_attrs(char *parsed, char **tag, int *n_attrs);

static int check_closing_tag(char *xml, unsigned long *pos, char *tag, int *err);
static void set_tag(char *xml, map_t **map, int *closed, unsigned long *pos);
static void set_value(char *xml, unsigned long *pos, map_t **map);

static map_t *decode_helper(char *xml, unsigned long *pos, int *len);

map_t *decode(char *xml) {
	int len = 0;
	unsigned long start = 0;
	return decode_helper(xml, &start, &len);
}

static map_t *decode_helper(char *xml, unsigned long *pos, int *len) {
	if (*len)
		++(*len);
	else
		*len = 1;

	map_t *decoded = calloc(1, sizeof(map_t));
	int err = 0, closed = 1;
	for (unsigned long i = *pos; i < strlen(xml); ++i) {
		if (check_closing_tag(xml, &i, decoded->tag, &err)) {
			if (!err)
				*pos = i;
			break;
		} else if (xml[i] == '<' && closed)
			set_tag(xml, &decoded, &closed, &i);
		else if (xml[i] == '<' && !closed) {
			map_t *ndec = decode_helper(xml, &i, &(decoded->n));
			decoded->size = sizeof(map_t);
			if (decoded->n > 1)
				decoded->payload =
					realloc(decoded->payload, decoded->n * sizeof(map_t));
			else
				decoded->payload = calloc(1, sizeof(map_t));
			((map_t *)decoded->payload)[decoded->n - 1] = *ndec;
			free(ndec);
		} else
			set_value(xml, &i, &decoded);
	}
	if (err) {
		free(decoded);
		decoded = NULL;
	}
	return decoded;
}

static char *get_tag(char *xml, unsigned long *pos) {
	int len;
	unsigned long i;
	for (i = *pos, len = 0; xml[i] != '>' && i < strlen(xml); ++i, ++len)
		;
	if (i >= strlen(xml))
		return NULL;
	char *title = calloc(len + 1, sizeof(char));
	strncpy(title, xml + *pos, len);
	*pos += len;
	return title;
}

static void set_value(char *xml, unsigned long *pos, map_t **map) {
	int len;
	unsigned long i;
	for (i = *pos, len = 0; xml[i] != '<' && i < strlen(xml); ++i, ++len)
		;
	if (i >= strlen(xml))
		return;

	(*map)->payload = calloc(len + 1, sizeof(char));
	strncpy((*map)->payload, xml + *pos, len);
	(*map)->size = sizeof(char);
	(*map)->n = strlen((*map)->payload);
	*pos += len - 1;
}

static attr_t *get_attrs(char *parsed, char **tag, int *n_attrs) {
	attr_t *attrs;
	char *iter = strtok(parsed, " ");
	*tag = calloc(strlen(iter) + 1, sizeof(char));
	strcpy(*tag, iter);

	if ((iter = strtok(NULL, " ")))
		attrs = calloc((*n_attrs = 1), sizeof(attr_t));
	else {
		free(parsed);
		return NULL;
	}

	int pos = 0;
	do {
		if (!iter)
			break;
		int i, len = strlen(iter);
		for (i = 0; i < len && iter[i] != '='; ++i)
			;
		if (i >= len || (iter[i] == '=' && i >= len - 1) ||
			(iter[i + 1] != '\'' && iter[i + 1] != '"')) {
			free(parsed);
			free(attrs);
			return NULL;
		}

		char *name = calloc(i + 1, sizeof(char));
		char *value = calloc(len - i, sizeof(char));
		strncpy(name, iter, i);
		strcpy(value, iter + i + 1);
		memmove(value, value + 1, strlen(value) - 1);
		value[strlen(value) - 2] = '\0';
		if (pos >= *n_attrs)
			attrs = realloc(attrs, (*n_attrs *= 2) * sizeof(attr_t));
		attrs[pos].id = name;
		attrs[pos++].value = value;
	} while ((iter = strtok(NULL, " ")));

	*n_attrs = pos;
	free(parsed);
	return attrs;
}

static void set_tag(char *xml, map_t **map, int *closed, unsigned long *pos) {
	(*pos)++;
	(*map)->attrs =
		get_attrs(get_tag(xml, pos), &((*map)->tag), &((*map)->n_attrs));
	*closed = 0;
}

static int check_closing_tag(char *xml, unsigned long *pos, char *tag, int *err) {
	if (!strncmp(xml + *pos, "</", 2)) {
		*pos += 2;
		char *tmp = get_tag(xml, pos);
		if (strcmp(tag, tmp))
			*err = 1;
		free(tmp);
		return 1;
	}
	return 0;
}