summaryrefslogtreecommitdiff
path: root/src/include/xml/encode.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/include/xml/encode.c')
-rw-r--r--src/include/xml/encode.c79
1 files changed, 38 insertions, 41 deletions
diff --git a/src/include/xml/encode.c b/src/include/xml/encode.c
index 9e76c69..60e418d 100644
--- a/src/include/xml/encode.c
+++ b/src/include/xml/encode.c
@@ -1,52 +1,49 @@
#include <stdio.h>
-#include <string.h>
#include <stdlib.h>
+#include <string.h>
#include "xml.h"
static void check(char **str, int *len, int *used, int n);
-char *encode(map_t *map, int len)
-{
- int slen = 128, used = 0;
- char *str = calloc(slen, sizeof(char));
+char *encode(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((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;
+ 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((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;
+static void check(char **str, int *len, int *used, int n) {
+ if (*used + n >= *len)
+ *str = realloc(*str, (*len *= 2));
+ *used += n;
}