diff options
author | Mitya Selivanov <automainint@guattari.tech> | 2024-01-20 15:35:36 +0100 |
---|---|---|
committer | Mitya Selivanov <automainint@guattari.tech> | 2024-01-20 15:35:36 +0100 |
commit | f5d0b7438be4f70a17841e7735a82d612d2ba596 (patch) | |
tree | e58d4cac5e459321abfe2161786c4bcd8ebdfa9f /source | |
parent | b9c44821df36c488b7ead015110edd309b614fb4 (diff) | |
download | kit-f5d0b7438be4f70a17841e7735a82d612d2ba596.zip |
xml: property
Diffstat (limited to 'source')
-rw-r--r-- | source/kit/xml.c | 33 | ||||
-rw-r--r-- | source/kit/xml.h | 12 | ||||
-rw-r--r-- | source/tests/xml.test.c | 19 |
3 files changed, 56 insertions, 8 deletions
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 |