summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbpc2003 <wpesfriendnva@gmail.com>2025-05-12 13:25:29 -0400
committerbpc2003 <wpesfriendnva@gmail.com>2025-05-12 13:25:29 -0400
commite99a79f1499b6cfb60007587ebeda645c32d38dc (patch)
treed30220cda791f8ea0adecb8f8e4262be4335f150
parent5bb744d350a68ff75e5af32b9070983b7e0aa716 (diff)
Have decoder working properly
-rw-r--r--.gitignore4
-rw-r--r--Makefile2
-rw-r--r--src/include/xml/decode.c60
-rw-r--r--src/test.c11
4 files changed, 42 insertions, 35 deletions
diff --git a/.gitignore b/.gitignore
index 17f634a..ea5ce08 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,6 @@
# db files
-*.db
-*.db~
+*.xdb
+*.xdb~
# build files
target/
diff --git a/Makefile b/Makefile
index 818f482..ce7e26c 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-CC = gcc
+CC = cc
BUILD = target
C_FLAGS = -Wall -lmdb -std=c11
D_FLAGS = -L$(BUILD) -Wl,-rpath=$(BUILD) -O0
diff --git a/src/include/xml/decode.c b/src/include/xml/decode.c
index 06c5bf6..a405e97 100644
--- a/src/include/xml/decode.c
+++ b/src/include/xml/decode.c
@@ -6,46 +6,43 @@
static char *gettag(char *xml, int *pos);
static char *getvalue(char *xml, int *pos);
+// TODO: make this smaller
map_t *decode(char *xml, int *pos, int *len)
{
- printf("%s\n", xml);
if (*len)
++(*len);
else
*len = 1;
map_t *decoded = calloc(1, sizeof(map_t));
- decoded->payload = NULL;
- char *tag = NULL;
- int err = 0;
+ int err = 0, closed = 1;
for (int i = *pos; i < strlen(xml); ++i) {
- if (xml[i] == '<') {
+ if (!strncmp(xml + i, "</", 2)) {
+ i += 2;
+ char *tmp = gettag(xml, &i);
+ if (!strcmp(decoded->tag, tmp))
+ *pos = i;
+ else
+ err = 1;
+ free(tmp);
+ goto end;
+ } else if (xml[i] == '<' && closed) {
++i;
- if (xml[i] == '/') {
- ++i;
- char *tmp = gettag(xml, &i);
- if (!strcmp(tag, tmp)) {
- free(tmp);
- *pos = i + 1;
- goto end;
- } else {
- err = 1;
- free(tmp);
- goto end;
- }
- }
- else if (!tag)
- tag = gettag(xml, &i);
- } else if (xml[i] == '>') {
- ++i;
- if (xml[i] == '<') {
- map_t *ndec = decode(xml, &i, &(decoded->n));
- free(ndec);
- } else {
- char *value = getvalue(xml, &i);
- printf("%s\n", value);
- free(value);
- }
+ decoded->tag = gettag(xml, &i);
+ closed = 0;
+ } else if (xml[i] == '<' && !closed) {
+ map_t *ndec = decode(xml, &i, &(decoded->n));
+ decoded->size = sizeof(map_t);
+ if (decoded->n > 1)
+ decoded->payload = realloc(decoded->payload, decoded->n * sizeof(map_t));
+ else
+ decoded->payload = calloc(1, sizeof(map_t));
+ ((map_t *) decoded->payload)[decoded->n - 1] = *ndec;
+ free(ndec);
+ } else {
+ decoded->payload = getvalue(xml, &i);
+ decoded->size = sizeof(char);
+ decoded->n = strlen(decoded->payload);
}
}
end:
@@ -53,7 +50,6 @@ map_t *decode(char *xml, int *pos, int *len)
free(decoded);
decoded = NULL;
}
- free(tag);
return decoded;
}
@@ -63,7 +59,7 @@ static char *gettag(char *xml, int *pos)
for (i = *pos, len = 0; xml[i] != '>'; ++i, ++len) ;
char *title = calloc(len + 1, sizeof(char));
strncpy(title, xml + *pos, len);
- *pos += len - 1;
+ *pos += len;
return title;
}
diff --git a/src/test.c b/src/test.c
index 57ef27a..9c9492c 100644
--- a/src/test.c
+++ b/src/test.c
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include "include/engine/engine.h"
#include "include/xml/xml.h"
@@ -265,6 +266,16 @@ void test_decode(void)
"<set><key1>value1</key1><key2>value2</key2></set>";
int len = 0, start = 0;
map_t *map = decode(xml, &start, &len);
+ printf("%s\n", (xml = encode(map, 1)));
+ free(xml);
+ // TODO: create a freemap function
+ free(map->tag);
+ map_t *pl = (map_t *) map->payload;
+ free(pl[0].tag);
+ free(pl[1].tag);
+ free(pl[0].payload);
+ free(pl[1].payload);
+ free(map->payload);
free(map);
}