diff options
author | bpc2003 <wpesfriendnva@gmail.com> | 2025-05-05 16:51:27 -0400 |
---|---|---|
committer | bpc2003 <wpesfriendnva@gmail.com> | 2025-05-05 16:51:27 -0400 |
commit | b6858640ec6ca17dc746863d1e92cb2452253d7d (patch) | |
tree | 28eed53fbdbdaf0b946175905ffadaaad0760949 | |
parent | 0102df793c02c94eb48a16370610e0e28a5c3361 (diff) |
Get started on XML decoder
-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 | ||||
-rw-r--r-- | src/test.c | 31 |
4 files changed, 83 insertions, 22 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); @@ -210,14 +210,9 @@ void test_encode(void) map->n = 2; map->payload = (map_t []) { { - .attrs = (map_t []) {{ - .attrs = NULL, - .n_attrs = 0, - .tag = "id", - .payload = "0", - .size = sizeof(char), - .n = 1 - }}, + .attrs = (attr_t []) { + { .id = "id", .value = "0" } + }, .n_attrs = 1, .tag = "document", .payload = (map_t []) { @@ -234,14 +229,9 @@ void test_encode(void) .n = 1 }, { - .attrs = (map_t []){{ - .attrs = NULL, - .n_attrs = 0, - .tag = "id", - .payload = "1", - .size = sizeof(char), - .n = 1 - }}, + .attrs = (attr_t []) { + { .id = "id", .value = "1" } + }, .n_attrs = 1, .tag = "document", .payload = "test", @@ -260,6 +250,14 @@ void test_encode(void) free(xml); } +void test_decode(void) +{ + char *xml_string="<documents><document><key>value</key></document><document><key>value</key></document></documents>"; + int len; + map_t *map = decode(xml_string, &len); + free(map); +} + int main(void) { // test_writedb(); @@ -280,6 +278,7 @@ int main(void) // test_delkeys_single(); // test_delkeys_multi(); test_encode(); + test_decode(); exit(0); } |