diff options
author | sanine <sanine.not@pm.me> | 2023-02-04 00:27:55 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-02-04 00:27:55 -0600 |
commit | 0bb180e4ddeebf6d219a38b862ce3e8238f99c54 (patch) | |
tree | 9556fd77d16cdcee69ff0c9c0765c408ee88ae19 | |
parent | 6292c2bce712dcca01ded960dc5f93528da2b9ce (diff) |
add kai_tag_attr_to_dup()
-rw-r--r-- | src/geometry/geometry.c | 17 | ||||
-rw-r--r-- | src/xml/xml.c | 22 | ||||
-rw-r--r-- | src/xml/xml.h | 13 | ||||
-rw-r--r-- | src/xml/xml.test.c | 33 |
4 files changed, 53 insertions, 32 deletions
diff --git a/src/geometry/geometry.c b/src/geometry/geometry.c index fd42735..ad23853 100644 --- a/src/geometry/geometry.c +++ b/src/geometry/geometry.c @@ -13,21 +13,16 @@ int kai_read_float_array(struct ka_float_array_t *dest, struct kai_tag_t *src) return -1; } - int flag; - dest->count = kai_tag_attr_to_long(src, "count", 0, &flag); - if (flag != 0) { + long count = kai_tag_attr_to_long(src, "count", -1); + if (count == -1) { /* count not present */ return -1; } - char *id = kai_tag_get_attr(src, "id"); - if (id != NULL) - dest->id = strdup(id); - else - dest->id = NULL; - - dest->digits = kai_tag_attr_to_long(src, "digits", 6, NULL); - dest->magnitude = kai_tag_attr_to_long(src, "magnitude", 38, NULL); + dest->count = count; + dest->id = kai_tag_attr_to_dup(src, "id"); + dest->digits = kai_tag_attr_to_long(src, "digits", 6); + dest->magnitude = kai_tag_attr_to_long(src, "magnitude", 38); /* allocate & fill buffer */ dest->buf = malloc(dest->count * sizeof(ka_real_t)); diff --git a/src/xml/xml.c b/src/xml/xml.c index 24d544c..ee3c879 100644 --- a/src/xml/xml.c +++ b/src/xml/xml.c @@ -35,18 +35,26 @@ char * kai_tag_get_attr(struct kai_tag_t *t, const char *attr) long kai_tag_attr_to_long( - struct kai_tag_t *t, const char *attr, - long base, int *flag) + struct kai_tag_t *t, const char *attr, long base) { 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; } + else { + return strtol(val, NULL, 10); + } +} + - if (flag != NULL) - *flag = 0; - return strtol(val, NULL, 10); +char *kai_tag_attr_to_dup(struct kai_tag_t *t, const char *attr) +{ + char *val = kai_tag_get_attr(t, attr); + if (val == NULL) { + return NULL; + } + else { + return strdup(val); + } } diff --git a/src/xml/xml.h b/src/xml/xml.h index ac956aa..4766e2c 100644 --- a/src/xml/xml.h +++ b/src/xml/xml.h @@ -14,12 +14,15 @@ 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" +/* find an attribute and convert to long. + * if not found, return "base" instead. */ long kai_tag_attr_to_long( - struct kai_tag_t *t, const char *attr, - long base, int *flag); + struct kai_tag_t *t, const char *attr, long base); + +/* find an attribute and return a duplicate of the value string, or NULL + * if not found. + */ +char *kai_tag_attr_to_dup(struct kai_tag_t *t, const char *attr); #endif diff --git a/src/xml/xml.test.c b/src/xml/xml.test.c index 11c85c9..d3b8360 100644 --- a/src/xml/xml.test.c +++ b/src/xml/xml.test.c @@ -36,22 +36,37 @@ LILY_TEST("convert xml attribute to long") "<tag attr1=\"10\" qq=\"12\"></tag>" ); - int n = kai_tag_attr_to_long(t, "attr1", 0, NULL); + int n = kai_tag_attr_to_long(t, "attr1", 0); CHECK_EQ(n, 10, "%d"); - int m = kai_tag_attr_to_long(t, "attr2", 14, NULL); + int m = kai_tag_attr_to_long(t, "attr2", 14); 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"); + kai_tag_destroy(t); +} +#include LILY_PUSH_TEST() - int k = kai_tag_attr_to_long(t, "dne", 22, &flag); - CHECK_EQ(k, 22, "%d"); - CHECK_EQ(flag, 1, "%d"); +LILY_TEST("duplicate xml attributes") +{ + struct kai_tag_t *t = kai_parse_string( + "<tag attr1=\"hello\"></tag>" + ); + + char *a = kai_tag_attr_to_dup(t, "attr1"); + char *b = kai_tag_attr_to_dup(t, "attr1"); + char *c = kai_tag_attr_to_dup(t, "dne"); kai_tag_destroy(t); + + REQUIRE_NEQ(a, NULL, "%p"); + REQUIRE_NEQ(b, NULL, "%p"); + CHECK_EQ(c, NULL, "%p"); + + CHECK_EQS(a, "hello"); + CHECK_EQS(b, "hello"); + CHECK_NEQ(a, b, "%p"); + + free(a); free(b); } #include LILY_PUSH_TEST() |