summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2023-02-03 23:16:18 -0600
committersanine <sanine.not@pm.me>2023-02-03 23:16:18 -0600
commit414bc3c6db05ec6d5127967eae39dbaa7f48ad2c (patch)
tree714c87d7d7963e20f9311a34968bab252159ea34 /src
parent1b4564c02380d10f7f8630d73441e7f9aa83c812 (diff)
implement kai_tag_get_attr
Diffstat (limited to 'src')
-rw-r--r--src/test/test.h1
-rw-r--r--src/xml/CMakeLists.txt7
-rw-r--r--src/xml/xml.c13
-rw-r--r--src/xml/xml.h5
-rw-r--r--src/xml/xml.test.c34
5 files changed, 60 insertions, 0 deletions
diff --git a/src/test/test.h b/src/test/test.h
index 910fe65..cb11152 100644
--- a/src/test/test.h
+++ b/src/test/test.h
@@ -8,6 +8,7 @@
#define TESTS \
X(util_suite) \
X(geometry_suite) \
+ X(xml_suite) \
#define X(test) extern void (*test)();
diff --git a/src/xml/CMakeLists.txt b/src/xml/CMakeLists.txt
index 9790692..8c5efb5 100644
--- a/src/xml/CMakeLists.txt
+++ b/src/xml/CMakeLists.txt
@@ -5,3 +5,10 @@ target_sources(kalmia PUBLIC
kalmia.lex.c
xml.c
)
+
+if (KALMIA_BUILD_TESTS)
+ target_sources(
+ kalmia-tests PUBLIC
+ xml.test.c
+ )
+endif()
diff --git a/src/xml/xml.c b/src/xml/xml.c
index e2aa008..33782c4 100644
--- a/src/xml/xml.c
+++ b/src/xml/xml.c
@@ -18,3 +18,16 @@ struct kai_tag_t * kai_parse_string(const char *str)
return document;
}
+
+
+char * kai_tag_get_attr(struct kai_tag_t *t, const char *attr)
+{
+ struct kai_attr_t *a = t->attrs;
+ while (a != NULL) {
+ if (strcmp(a->key, attr) == 0) {
+ return a->value;
+ }
+ a = a->next;
+ }
+ return NULL;
+}
diff --git a/src/xml/xml.h b/src/xml/xml.h
index 238177e..e2abf8f 100644
--- a/src/xml/xml.h
+++ b/src/xml/xml.h
@@ -8,4 +8,9 @@
struct kai_tag_t * kai_parse_string(const char *str);
+/* return pointer to the attribute's value string, or NULL if it
+ * isn't present
+ */
+char * kai_tag_get_attr(struct kai_tag_t *t, const char *attr);
+
#endif
diff --git a/src/xml/xml.test.c b/src/xml/xml.test.c
new file mode 100644
index 0000000..acee4b4
--- /dev/null
+++ b/src/xml/xml.test.c
@@ -0,0 +1,34 @@
+#include <string.h>
+
+#include <kalmia.h>
+#include "xml.h"
+#include "test/test.h"
+
+
+LILY_FILE_BEGIN(xml_suite)
+
+
+LILY_TEST("get xml attributes")
+{
+ struct kai_tag_t *t = kai_parse_string(
+ "<tag attr1=\"hello\" attr2=\"world\"></tag>"
+ );
+
+ char *attr1 = kai_tag_get_attr(t, "attr1");
+ REQUIRE_NEQ(attr1, NULL, "%p");
+ CHECK_EQS(attr1, "hello");
+
+ char *attr2 = kai_tag_get_attr(t, "attr2");
+ REQUIRE_NEQ(attr2, NULL, "%p");
+ CHECK_EQS(attr2, "world");
+
+ char *attr3 = kai_tag_get_attr(t, "attr3");
+ CHECK_EQ(attr3, NULL, "%p");
+
+ kai_tag_destroy(t);
+}
+#include LILY_PUSH_TEST()
+
+
+#define LILY_FILE_END
+#include LILY_REGISTER_TESTS()