summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--TODO2
-rw-r--r--source/kit/xml.c33
-rw-r--r--source/kit/xml.h12
-rw-r--r--source/tests/xml.test.c19
4 files changed, 57 insertions, 9 deletions
diff --git a/TODO b/TODO
index 5722db1..7ac7596 100644
--- a/TODO
+++ b/TODO
@@ -12,7 +12,7 @@ To-Do
- UTF-8
- JSON
- unival
- - XML: print; get element by tag name, property; get property
+ - XML: print
- System
- Long file name support for Windows
- Better atomics support
diff --git a/source/kit/xml.c b/source/kit/xml.c
index 399cc5f..fbe4ec2 100644
--- a/source/kit/xml.c
+++ b/source/kit/xml.c
@@ -4,9 +4,9 @@
#include <assert.h>
typedef struct {
- ib_token_t last;
- kit_str_builder_t text;
- kit_da_xml_t tags;
+ ib_token_t last;
+ str_builder_t text;
+ kit_da_xml_t tags;
} kit_xml_intermediate_t;
static s32 kit_xml_alloc_and_unescape_(str_builder_t *dst, str_t str,
@@ -270,7 +270,7 @@ static kit_xml_intermediate_t kit_xml_parse_buf_(
return res;
}
-kit_xml_parse_result_t kit_xml_parse(kit_is_handle_t is,
+kit_xml_parse_result_t kit_xml_parse(is_handle_t is,
kit_allocator_t *alloc) {
input_buffer_t ib = ib_wrap(is, alloc);
kit_xml_intermediate_t im = kit_xml_parse_buf_(ib_token(&ib),
@@ -370,6 +370,31 @@ kit_xml_text_t kit_xml_full_text(kit_xml_t *xml,
return res;
}
+b8 kit_xml_has_property(kit_xml_t *xml, kit_str_t name) {
+ assert(xml != NULL);
+ if (xml == NULL)
+ return 0;
+
+ for (i64 i = 0; i < xml->properties.size; i++)
+ if (AR_EQUAL(xml->properties.values[i].name, name))
+ return 1;
+
+ return 0;
+}
+
+str_t kit_xml_property(kit_xml_t *xml, str_t name) {
+ assert(xml != NULL);
+ if (xml == NULL)
+ return str(0, NULL);
+
+ for (i64 i = 0; i < xml->properties.size; i++)
+ if (AR_EQUAL(xml->properties.values[i].name, name))
+ return WRAP_STR(xml->properties.values[i].value);
+
+ assert(0);
+ return str(0, NULL);
+}
+
void kit_xml_destroy(kit_xml_t *xml) {
assert(xml != NULL);
if (xml == NULL)
diff --git a/source/kit/xml.h b/source/kit/xml.h
index fab1d1d..fdc9536 100644
--- a/source/kit/xml.h
+++ b/source/kit/xml.h
@@ -42,6 +42,8 @@ kit_xml_parse_result_t kit_xml_parse(kit_is_handle_t is,
kit_xml_text_t kit_xml_print(kit_xml_t *xml, kit_allocator_t *alloc);
kit_xml_text_t kit_xml_full_text(kit_xml_t *xml,
kit_allocator_t *alloc);
+b8 kit_xml_has_property(kit_xml_t *xml, kit_str_t name);
+kit_str_t kit_xml_property(kit_xml_t *xml, kit_str_t name);
void kit_xml_destroy(kit_xml_t *xml);
#ifdef __cplusplus
@@ -49,14 +51,16 @@ void kit_xml_destroy(kit_xml_t *xml);
#endif
#ifndef KIT_DISABLE_SHORT_NAMES
-# define xml_parse kit_xml_parse
-# define xml_print kit_xml_print
-# define xml_full_text kit_xml_full_text
-# define xml_destroy kit_xml_destroy
# define xml_t kit_xml_t
# define xml_property_t kit_xml_property_t
# define xml_parse_result_t kit_xml_parse_result_t
# define xml_text_t kit_xml_text_t
+# define xml_parse kit_xml_parse
+# define xml_print kit_xml_print
+# define xml_full_text kit_xml_full_text
+# define xml_has_property kit_xml_has_property
+# define xml_property kit_xml_property
+# define xml_destroy kit_xml_destroy
#endif
#endif
diff --git a/source/tests/xml.test.c b/source/tests/xml.test.c
index 4c72d28..1534030 100644
--- a/source/tests/xml.test.c
+++ b/source/tests/xml.test.c
@@ -406,4 +406,23 @@ TEST("xml full text") {
is_destroy(is);
}
+TEST("xml property") {
+ is_handle_t is = IS_WRAP_STRING(
+ SZ("<tag foo=\"123\" bar=\"456\" />"));
+ xml_parse_result_t res = xml_parse(is, NULL);
+
+ REQUIRE_EQ(res.status, KIT_OK);
+
+ if (res.status == KIT_OK) {
+ REQUIRE_EQ(xml_has_property(&res.xml, SZ("foo")), 1);
+ REQUIRE_EQ(xml_has_property(&res.xml, SZ("bar")), 1);
+ REQUIRE_EQ(xml_has_property(&res.xml, SZ("buz")), 0);
+ REQUIRE(AR_EQUAL(xml_property(&res.xml, SZ("foo")), SZ("123")));
+ REQUIRE(AR_EQUAL(xml_property(&res.xml, SZ("bar")), SZ("456")));
+ xml_destroy(&res.xml);
+ }
+
+ is_destroy(is);
+}
+
#undef KIT_TEST_FILE