diff options
Diffstat (limited to 'src/xml')
-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 |
3 files changed, 47 insertions, 21 deletions
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() |