diff options
Diffstat (limited to 'src/include')
-rw-r--r-- | src/include/xml/decode.c | 57 | ||||
-rw-r--r-- | src/include/xml/encode.c | 2 | ||||
-rw-r--r-- | src/include/xml/xml.h | 15 |
3 files changed, 68 insertions, 6 deletions
diff --git a/src/include/xml/decode.c b/src/include/xml/decode.c new file mode 100644 index 0000000..f072ae7 --- /dev/null +++ b/src/include/xml/decode.c @@ -0,0 +1,57 @@ +#include <stdlib.h> +#include <string.h> + +#include "xml.h" + +static char *gettag(char *xml, int *pos); + +map_t *decode(char *xml, int *len) +{ + map_t *decoded = calloc(1, sizeof(map_t)); + *len = 1; + + int closed = 0; + char *title = NULL, *tmp; + for (int i = 0, j = 0; i < strlen(xml); ++i) { + switch (xml[i]) { + case '<': + ++i; + if (xml[i] == '/') { + ++i; + if (!strcmp(title, (tmp = gettag(xml, &i)))) + closed = 1; + else { + free(tmp); + return NULL; + } + free(tmp); + } else { + if (title != NULL) + free(title); + closed = 0; + title = gettag(xml, &i); + } + printf("%s\n", title); + break; + case '>': + // TODO: make this recursive + if (xml[i + 1] == '<') + printf("nested tag\n"); + else + ; + break; + } + } + free(title); + return decoded; +} + +static char *gettag(char *xml, int *pos) +{ + int len, i; + for (i = *pos, len = 0; xml[i] != '>'; ++i, ++len) ; + char *title = calloc(len + 1, sizeof(char)); + strncpy(title, xml + *pos, len); + *pos += i - 2; + return title; +} diff --git a/src/include/xml/encode.c b/src/include/xml/encode.c index b8c090d..9e76c69 100644 --- a/src/include/xml/encode.c +++ b/src/include/xml/encode.c @@ -19,7 +19,7 @@ char *encode(map_t *map, int len) for (int j = 0; j < map[i].n_attrs; ++j) { check(&str, &slen, &used, snprintf(buf, 64, " %s='%s'", - map[i].attrs[j].tag, (char *)map[i].attrs[j].payload)); + map[i].attrs[j].id, map[i].attrs[j].value)); strcat(str, buf); } } diff --git a/src/include/xml/xml.h b/src/include/xml/xml.h index c20f9d9..5668d12 100644 --- a/src/include/xml/xml.h +++ b/src/include/xml/xml.h @@ -3,16 +3,21 @@ #include <stdio.h> -struct map { + +typedef struct { + char *id; + char *value; +} attr_t; + +typedef struct { char *tag; void *payload; size_t size; size_t n; - struct map *attrs; - size_t n_attrs; -}; + attr_t *attrs; + int n_attrs; +} map_t; -typedef struct map map_t; /* decode: decodes the provided xml statement into a map_t */ map_t *decode(char *xml_str, int *len); |