From 414bc3c6db05ec6d5127967eae39dbaa7f48ad2c Mon Sep 17 00:00:00 2001 From: sanine Date: Fri, 3 Feb 2023 23:16:18 -0600 Subject: implement kai_tag_get_attr --- src/xml/CMakeLists.txt | 7 +++++++ src/xml/xml.c | 13 +++++++++++++ src/xml/xml.h | 5 +++++ src/xml/xml.test.c | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 src/xml/xml.test.c (limited to 'src/xml') 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 + +#include +#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( + "" + ); + + 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() -- cgit v1.2.1