From 6292c2bce712dcca01ded960dc5f93528da2b9ce Mon Sep 17 00:00:00 2001 From: sanine Date: Sat, 4 Feb 2023 00:12:43 -0600 Subject: implement kai_read_float_array --- include/kalmia.h | 2 ++ src/geometry/geometry.c | 20 +++++++++++++++++--- src/geometry/geometry.test.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/include/kalmia.h b/include/kalmia.h index bab1b34..dca0a06 100644 --- a/include/kalmia.h +++ b/include/kalmia.h @@ -70,6 +70,8 @@ struct ka_float_array_t { char *id; unsigned int digits; /* default 6 */ int magnitude; /* default 38 */ + + ka_real_t *buf; }; diff --git a/src/geometry/geometry.c b/src/geometry/geometry.c index 322ada0..fd42735 100644 --- a/src/geometry/geometry.c +++ b/src/geometry/geometry.c @@ -13,11 +13,25 @@ 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! */ + int flag; + dest->count = kai_tag_attr_to_long(src, "count", 0, &flag); + if (flag != 0) { + /* 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); + + /* allocate & fill buffer */ + dest->buf = malloc(dest->count * sizeof(ka_real_t)); + kai_text_to_reals(dest->buf, src->content, dest->count); + return 0; } diff --git a/src/geometry/geometry.test.c b/src/geometry/geometry.test.c index d433066..56673a4 100644 --- a/src/geometry/geometry.test.c +++ b/src/geometry/geometry.test.c @@ -40,5 +40,33 @@ LILY_TEST("fail to read float_array without count attribute") #include LILY_PUSH_TEST() +LILY_TEST("read normal float_array") +{ + struct kai_tag_t *t = kai_parse_string( + "" + " -0.5 0.0 0.5" + "" + ); + + struct ka_float_array_t arr; + int result = kai_read_float_array(&arr, t); + REQUIRE_EQ(result, 0, "%d"); + kai_tag_destroy(t); + + REQUIRE_EQ(arr.count, 3, "%d"); + CHECK_EQS(arr.id, "arr"); + CHECK_EQ(arr.digits, 6, "%d"); + CHECK_EQ(arr.magnitude, 38, "%d"); + + CHECK_EQF(arr.buf[0], -0.5, "%f"); + CHECK_EQF(arr.buf[1], 0.0, "%f"); + CHECK_EQF(arr.buf[2], 0.5, "%f"); + + free(arr.id); + free(arr.buf); +} +#include LILY_PUSH_TEST() + + #define LILY_FILE_END #include LILY_REGISTER_TESTS() -- cgit v1.2.1