summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbpc2003 <wpesfriendnva@gmail.com>2025-05-01 12:23:37 -0400
committerbpc2003 <wpesfriendnva@gmail.com>2025-05-01 12:23:37 -0400
commit0102df793c02c94eb48a16370610e0e28a5c3361 (patch)
tree09d3f96f8b24b8284504803a3b9d2961810cc10c
parent4ea8fe896cc333f5e7addd56e23db5668f4599b3 (diff)
Get started on xml encoding
-rw-r--r--Makefile2
-rw-r--r--src/include/xml/encode.c52
-rw-r--r--src/include/xml/xml.h24
-rw-r--r--src/test.c102
4 files changed, 159 insertions, 21 deletions
diff --git a/Makefile b/Makefile
index 2bcd015..818f482 100644
--- a/Makefile
+++ b/Makefile
@@ -20,7 +20,7 @@ test: dev_lib
dev: dev_lib
$(CC) src/main.c src/cmd.c $(D_FLAGS) -g $(C_FLAGS) -o $(BUILD)/devmdb.out
dev_lib: $(BUILD)
- $(CC) src/include/engine/*.c $(L_FLAGS) -g
+ $(CC) src/include/engine/*.c src/include/xml/*.c $(L_FLAGS) -g
$(CC) -shared -o $(BUILD)/libmdb.so *.o
rm *.o
diff --git a/src/include/xml/encode.c b/src/include/xml/encode.c
new file mode 100644
index 0000000..b8c090d
--- /dev/null
+++ b/src/include/xml/encode.c
@@ -0,0 +1,52 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+#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 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].tag, (char *)map[i].attrs[j].payload));
+ 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, "</%s>", 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;
+}
diff --git a/src/include/xml/xml.h b/src/include/xml/xml.h
new file mode 100644
index 0000000..c20f9d9
--- /dev/null
+++ b/src/include/xml/xml.h
@@ -0,0 +1,24 @@
+#ifndef XML_H
+#define XML_H
+
+#include <stdio.h>
+
+struct map {
+ char *tag;
+ void *payload;
+ size_t size;
+ size_t n;
+ struct map *attrs;
+ size_t n_attrs;
+};
+
+typedef struct map map_t;
+
+/* decode: decodes the provided xml statement into a map_t */
+map_t *decode(char *xml_str, int *len);
+
+/* encode: encodes the provided map_t into a xml statement */
+char *encode(map_t *map, int len);
+
+
+#endif
diff --git a/src/test.c b/src/test.c
index 6a00fa6..13745b8 100644
--- a/src/test.c
+++ b/src/test.c
@@ -1,11 +1,12 @@
#include <stdio.h>
#include <stdlib.h>
-#include "include/mdb.h"
+#include "include/engine/engine.h"
+#include "include/xml/xml.h"
void test_writedb(void)
{
- tablist_t *list = readdb("dbs/new.db");
+ tablist_t *list = readdb("dbs/test.db");
writedb("dbs/test.db", list);
delkeys(list, -1, NULL, 0);
free(list);
@@ -13,7 +14,7 @@ void test_writedb(void)
void test_readdb(void)
{
- tablist_t *list = readdb("dbs/new.db");
+ tablist_t *list = readdb("dbs/test.db");
tablist_t *indices = getkeys(list, -1, NULL, 0);
for (int i = 0; i < indices[0].len; ++i) {
printf("id: %d\n", i);
@@ -199,25 +200,86 @@ void test_getkeys(void)
free(list);
}
+void test_encode(void)
+{
+ map_t *map = calloc(1, sizeof(map_t));
+ map->attrs = NULL;
+ map->n_attrs = 0;
+ map->tag = "documents";
+ map->size = sizeof(map_t);
+ map->n = 2;
+ map->payload = (map_t []) {
+ {
+ .attrs = (map_t []) {{
+ .attrs = NULL,
+ .n_attrs = 0,
+ .tag = "id",
+ .payload = "0",
+ .size = sizeof(char),
+ .n = 1
+ }},
+ .n_attrs = 1,
+ .tag = "document",
+ .payload = (map_t []) {
+ {
+ .attrs = NULL,
+ .n_attrs = 0,
+ .tag = "test",
+ .payload = "test",
+ .size = sizeof(char),
+ .n = 4
+ }
+ },
+ .size = sizeof(map_t),
+ .n = 1
+ },
+ {
+ .attrs = (map_t []){{
+ .attrs = NULL,
+ .n_attrs = 0,
+ .tag = "id",
+ .payload = "1",
+ .size = sizeof(char),
+ .n = 1
+ }},
+ .n_attrs = 1,
+ .tag = "document",
+ .payload = "test",
+ .size = sizeof(char),
+ .n = 4
+ }
+ };
+
+ char *xml = encode(map, 1);
+ if (xml == NULL) {
+ fprintf(stderr, "test_encode: failed\n");
+ return;
+ }
+ printf("%s\n", xml);
+ free(map);
+ free(xml);
+}
+
int main(void)
{
- test_writedb();
- test_readdb();
- test_getkeys();
- test_getkeys_multi();
- test_getkeys_multi_fail();
-
- test_setkeys();
- test_setkeys_fail();
- test_setkeys_single();
- test_setkeys_multipairs();
- test_setkeys_multi_fail();
-
- test_delkeys();
- test_delkeys_all();
- test_delkeys_fail();
- test_delkeys_single();
- test_delkeys_multi();
+ // test_writedb();
+ // test_readdb();
+ // test_getkeys();
+ // test_getkeys_multi();
+ // test_getkeys_multi_fail();
+ //
+ // test_setkeys();
+ // test_setkeys_fail();
+ // test_setkeys_single();
+ // test_setkeys_multipairs();
+ // test_setkeys_multi_fail();
+ //
+ // test_delkeys();
+ // test_delkeys_all();
+ // test_delkeys_fail();
+ // test_delkeys_single();
+ // test_delkeys_multi();
+ test_encode();
exit(0);
}