summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbpc2003 <wpesfriendnva@gmail.com>2025-05-21 11:07:50 -0400
committerbpc2003 <wpesfriendnva@gmail.com>2025-05-21 11:07:50 -0400
commita08b44e1c6fbd8b56011a969f8c11bcc630e1ee0 (patch)
tree5a4337c8993f1bee673d4de819a5b64fe0aa1f95
parent3c07894d658c3fa75f1cd1fd524e7464d9d95f1d (diff)
Decrease visibilty in encode and decode functions
-rw-r--r--src/include/wal/wal.h4
-rw-r--r--src/include/xdbms.h (renamed from src/include/mdb.h)4
-rw-r--r--src/include/xml/decode.c11
-rw-r--r--src/include/xml/encode.c7
-rw-r--r--src/include/xml/xml.h7
-rw-r--r--src/main.c2
-rw-r--r--src/test.c7
7 files changed, 26 insertions, 16 deletions
diff --git a/src/include/wal/wal.h b/src/include/wal/wal.h
new file mode 100644
index 0000000..59bc81a
--- /dev/null
+++ b/src/include/wal/wal.h
@@ -0,0 +1,4 @@
+#ifndef WAL_H
+#define WAL_H
+
+#endif
diff --git a/src/include/mdb.h b/src/include/xdbms.h
index cba4aa8..3438f5e 100644
--- a/src/include/mdb.h
+++ b/src/include/xdbms.h
@@ -1,5 +1,5 @@
-#ifndef MDB_H
-#define MDB_H
+#ifndef XDBMS_H
+#define XDBMS_H
#include "engine/engine.h"
diff --git a/src/include/xml/decode.c b/src/include/xml/decode.c
index 14af551..dccbc2f 100644
--- a/src/include/xml/decode.c
+++ b/src/include/xml/decode.c
@@ -10,7 +10,14 @@ static int check_closing_tag(char *xml, int *pos, char *tag, int *err);
static void set_tag(char *xml, map_t **map, int *closed, int *pos);
static void set_value(char *xml, int *pos, map_t **map);
-map_t *decode(char *xml, int *pos, int *len) {
+static map_t *decode_helper(char *xml, int *pos, int *len);
+
+map_t *decode(char *xml) {
+ int start = 0, len = 0;
+ return decode_helper(xml, &start, &len);
+}
+
+static map_t *decode_helper(char *xml, int *pos, int *len) {
if (*len)
++(*len);
else
@@ -26,7 +33,7 @@ map_t *decode(char *xml, int *pos, int *len) {
} else if (xml[i] == '<' && closed)
set_tag(xml, &decoded, &closed, &i);
else if (xml[i] == '<' && !closed) {
- map_t *ndec = decode(xml, &i, &(decoded->n));
+ map_t *ndec = decode_helper(xml, &i, &(decoded->n));
decoded->size = sizeof(map_t);
if (decoded->n > 1)
decoded->payload =
diff --git a/src/include/xml/encode.c b/src/include/xml/encode.c
index 60e418d..6ed753e 100644
--- a/src/include/xml/encode.c
+++ b/src/include/xml/encode.c
@@ -5,8 +5,11 @@
#include "xml.h"
static void check(char **str, int *len, int *used, int n);
+static char *encode_helper(map_t *map, int len);
-char *encode(map_t *map, int len) {
+char *encode(map_t *map) { return encode_helper(map, 1); }
+
+static char *encode_helper(map_t *map, int len) {
int slen = 128, used = 0;
char *str = calloc(slen, sizeof(char));
@@ -25,7 +28,7 @@ char *encode(map_t *map, int len) {
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);
+ char *rt = encode_helper((map_t *)map[i].payload, map[i].n);
if (rt == NULL)
return NULL;
check(&str, &slen, &used, strlen(rt));
diff --git a/src/include/xml/xml.h b/src/include/xml/xml.h
index c812005..eb250e3 100644
--- a/src/include/xml/xml.h
+++ b/src/include/xml/xml.h
@@ -17,14 +17,11 @@ typedef struct {
int n_attrs;
} map_t;
-// TODO: make encode more reliant on helpers
-// to make function more private
-
/* decode: decodes the provided xml statement into a map_t */
-map_t *decode(char *xml, int *pos, int *len);
+map_t *decode(char *xml);
/* encode: encodes the provided map_t into a xml statement */
-char *encode(map_t *map, int len);
+char *encode(map_t *map);
/* freemap: frees a map and its children */
void freemap(map_t *map);
diff --git a/src/main.c b/src/main.c
index 5f1a7cb..418addd 100644
--- a/src/main.c
+++ b/src/main.c
@@ -4,7 +4,7 @@
#include <string.h>
#include "cmd.h"
-#include "include/mdb.h"
+#include "include/xdbms.h"
int getid(char *selector);
int printkeys(tablist_t *list, int id, char **keys, int klen);
diff --git a/src/test.c b/src/test.c
index f4820db..12cac0d 100644
--- a/src/test.c
+++ b/src/test.c
@@ -215,7 +215,7 @@ void test_encode(void) {
.size = sizeof(map_t),
.n = 1}};
- char *xml = encode(map, 1);
+ char *xml = encode(map);
if (xml == NULL) {
fprintf(stderr, "test_encode: failed\n");
return;
@@ -228,9 +228,8 @@ void test_encode(void) {
void test_decode(void) {
char *xml = "<set id='0' "
"test='true'><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)));
+ map_t *map = decode(xml);
+ printf("%s\n", (xml = encode(map)));
free(xml);
freemap(map);
}