diff options
author | sanine <sanine.not@pm.me> | 2023-02-03 23:55:45 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-02-03 23:55:45 -0600 |
commit | b183f50a84efc495b322370976b22bad486800f3 (patch) | |
tree | 581116a19816f6da06cb4502204a85822899f32d | |
parent | 414bc3c6db05ec6d5127967eae39dbaa7f48ad2c (diff) |
add kai_tag_attr_to_long
-rw-r--r-- | src/geometry/geometry.c | 6 | ||||
-rw-r--r-- | src/geometry/geometry.test.c | 19 | ||||
-rw-r--r-- | src/xml/xml.c | 19 | ||||
-rw-r--r-- | src/xml/xml.h | 9 | ||||
-rw-r--r-- | src/xml/xml.test.c | 26 |
5 files changed, 78 insertions, 1 deletions
diff --git a/src/geometry/geometry.c b/src/geometry/geometry.c index 5e93c55..322ada0 100644 --- a/src/geometry/geometry.c +++ b/src/geometry/geometry.c @@ -13,5 +13,11 @@ int kai_read_float_array(struct ka_float_array_t *dest, struct kai_tag_t *src) return -1; } + char *count_str = kai_tag_get_attr(src, "count"); + if (count_str == NULL) { + /* missing required count attribute! */ + return -1; + } + return 0; } diff --git a/src/geometry/geometry.test.c b/src/geometry/geometry.test.c index b724ee7..d433066 100644 --- a/src/geometry/geometry.test.c +++ b/src/geometry/geometry.test.c @@ -16,7 +16,24 @@ LILY_TEST("fail to read non-float_array") struct ka_float_array_t arr; int result = kai_read_float_array(&arr, t); - CHECK_EQF(result, -1, "%d"); + CHECK_EQ(result, -1, "%d"); + + kai_tag_destroy(t); +} +#include LILY_PUSH_TEST() + + +LILY_TEST("fail to read float_array without count attribute") +{ + struct kai_tag_t *t = kai_parse_string( + "<float_array id=\"some id\">" + " blah blah internals" + "</float_array>" + ); + + struct ka_float_array_t arr; + int result = kai_read_float_array(&arr, t); + CHECK_EQ(result, -1, "%d"); kai_tag_destroy(t); } diff --git a/src/xml/xml.c b/src/xml/xml.c index 33782c4..24d544c 100644 --- a/src/xml/xml.c +++ b/src/xml/xml.c @@ -1,3 +1,4 @@ +#include <stdlib.h> #include "xml.h" /* parse a string into a document */ @@ -31,3 +32,21 @@ char * kai_tag_get_attr(struct kai_tag_t *t, const char *attr) } return NULL; } + + +long kai_tag_attr_to_long( + struct kai_tag_t *t, const char *attr, + long base, int *flag) +{ + char *val = kai_tag_get_attr(t, attr); + if (val == NULL) { + /* attribute not present, fall back to base */ + if (flag != NULL) + *flag = 1; + return base; + } + + if (flag != NULL) + *flag = 0; + return strtol(val, NULL, 10); +} diff --git a/src/xml/xml.h b/src/xml/xml.h index e2abf8f..ac956aa 100644 --- a/src/xml/xml.h +++ b/src/xml/xml.h @@ -13,4 +13,13 @@ struct kai_tag_t * kai_parse_string(const char *str); */ char * kai_tag_get_attr(struct kai_tag_t *t, const char *attr); + +/* find an attribute and convert to long. if not found, return "base" instead. + * if flag is not NULL, then it is set to 0 when taking the value from an + * attribute and 1 when taking the value from "base" + */ +long kai_tag_attr_to_long( + struct kai_tag_t *t, const char *attr, + long base, int *flag); + #endif diff --git a/src/xml/xml.test.c b/src/xml/xml.test.c index acee4b4..11c85c9 100644 --- a/src/xml/xml.test.c +++ b/src/xml/xml.test.c @@ -30,5 +30,31 @@ LILY_TEST("get xml attributes") #include LILY_PUSH_TEST() +LILY_TEST("convert xml attribute to long") +{ + struct kai_tag_t *t = kai_parse_string( + "<tag attr1=\"10\" qq=\"12\"></tag>" + ); + + int n = kai_tag_attr_to_long(t, "attr1", 0, NULL); + CHECK_EQ(n, 10, "%d"); + + int m = kai_tag_attr_to_long(t, "attr2", 14, NULL); + CHECK_EQ(m, 14, "%d"); + + int flag = -1; + int qq = kai_tag_attr_to_long(t, "qq", 0, &flag); + CHECK_EQ(qq, 12, "%d"); + CHECK_EQ(flag, 0, "%d"); + + int k = kai_tag_attr_to_long(t, "dne", 22, &flag); + CHECK_EQ(k, 22, "%d"); + CHECK_EQ(flag, 1, "%d"); + + kai_tag_destroy(t); +} +#include LILY_PUSH_TEST() + + #define LILY_FILE_END #include LILY_REGISTER_TESTS() |