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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "xml.h"
static void check(char **str, int *len, int *used, int n);
static char *encode_helper(map_t *map, int len);
char *encode(map_t *map) { return encode_helper(map, 1); }
static char *encode_helper(map_t *map, int len) {
int slen = 128, used = 0;
char *str = calloc(slen, sizeof(char));
char buf[64];
for (int i = 0; i < len; ++i) {
check(&str, &slen, &used, snprintf(buf, 64, "<%s", map[i].tag));
strcat(str, buf);
if (map[i].attrs != NULL) {
for (int j = 0; j < map[i].n_attrs; ++j) {
check(&str, &slen, &used,
snprintf(buf, 64, " %s='%s'", map[i].attrs[j].id,
map[i].attrs[j].value));
strcat(str, buf);
}
}
check(&str, &slen, &used, 1);
strcat(str, ">");
if (map[i].size == sizeof(map_t)) {
char *rt = encode_helper((map_t *)map[i].payload, map[i].n);
if (rt == NULL)
return NULL;
check(&str, &slen, &used, strlen(rt));
strcat(str, rt);
free(rt);
} else if (map[i].size == sizeof(char)) {
check(&str, &slen, &used, map[i].n);
strcat(str, map[i].payload);
} else
return NULL;
check(&str, &slen, &used, snprintf(buf, 64, "</%s>", map[i].tag));
strcat(str, buf);
}
return str;
}
static void check(char **str, int *len, int *used, int n) {
if (*used + n >= *len)
*str = realloc(*str, (*len *= 2));
*used += n;
}
|