From bf76b8c582ed9c541438ebe90fc6415fb82f2ed3 Mon Sep 17 00:00:00 2001 From: bpc2003 Date: Tue, 20 May 2025 14:40:47 -0400 Subject: Add Formatting --- src/include/xml/encode.c | 79 +++++++++++++++++++++++------------------------- 1 file changed, 38 insertions(+), 41 deletions(-) (limited to 'src/include/xml/encode.c') 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 -#include #include +#include #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, "", 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, "", 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; } -- cgit v1.2.3