diff options
author | bpc2003 <wpesfriendnva@gmail.com> | 2025-05-09 17:44:20 -0400 |
---|---|---|
committer | bpc2003 <wpesfriendnva@gmail.com> | 2025-05-09 17:44:20 -0400 |
commit | 5bb744d350a68ff75e5af32b9070983b7e0aa716 (patch) | |
tree | 17b8252d8878bd2fbdeb2fa8bab791f1f15f7591 | |
parent | 62fd6c58cb620c63316cb3863195e8bab5da5b84 (diff) |
Have recursive decoder mostly working, now need to find way to store payloads safely
-rw-r--r-- | src/include/xml/decode.c | 16 | ||||
-rw-r--r-- | src/include/xml/xml.h | 2 | ||||
-rw-r--r-- | src/test.c | 6 |
3 files changed, 11 insertions, 13 deletions
diff --git a/src/include/xml/decode.c b/src/include/xml/decode.c index 2b5ca67..06c5bf6 100644 --- a/src/include/xml/decode.c +++ b/src/include/xml/decode.c @@ -6,7 +6,7 @@ static char *gettag(char *xml, int *pos); static char *getvalue(char *xml, int *pos); -map_t *decode(char *xml, int *len) +map_t *decode(char *xml, int *pos, int *len) { printf("%s\n", xml); if (*len) @@ -18,7 +18,7 @@ map_t *decode(char *xml, int *len) char *tag = NULL; int err = 0; - for (int i = 0; i < strlen(xml); ++i) { + for (int i = *pos; i < strlen(xml); ++i) { if (xml[i] == '<') { ++i; if (xml[i] == '/') { @@ -26,6 +26,7 @@ map_t *decode(char *xml, int *len) char *tmp = gettag(xml, &i); if (!strcmp(tag, tmp)) { free(tmp); + *pos = i + 1; goto end; } else { err = 1; @@ -37,21 +38,18 @@ map_t *decode(char *xml, int *len) tag = gettag(xml, &i); } else if (xml[i] == '>') { ++i; - // TODO: figure out recursive calls if (xml[i] == '<') { - map_t *ndec = decode(xml + i, &(decoded->n)); + map_t *ndec = decode(xml, &i, &(decoded->n)); free(ndec); } else { - decoded->payload = getvalue(xml, &i); - decoded->size = sizeof(char); - decoded->n = strlen(decoded->payload); - printf("%s\n", (char *) decoded->payload); + char *value = getvalue(xml, &i); + printf("%s\n", value); + free(value); } } } end: if (err) { - free(decoded->payload); free(decoded); decoded = NULL; } diff --git a/src/include/xml/xml.h b/src/include/xml/xml.h index 4ee80b7..d64c61b 100644 --- a/src/include/xml/xml.h +++ b/src/include/xml/xml.h @@ -20,7 +20,7 @@ typedef struct { /* decode: decodes the provided xml statement into a map_t */ -map_t *decode(char *xml, int *len); +map_t *decode(char *xml, int *pos, int *len); /* encode: encodes the provided map_t into a xml statement */ char *encode(map_t *map, int len); @@ -262,9 +262,9 @@ void test_encode(void) void test_decode(void) { char *xml = - "<documents><document><key>value1</key></document><document><key>value2</key></document></documents>"; - int len = 0; - map_t *map = decode(xml, &len); + "<set><key1>value1</key1><key2>value2</key2></set>"; + int len = 0, start = 0; + map_t *map = decode(xml, &start, &len); free(map); } |