diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/geometry/geometry.c | 32 | ||||
-rw-r--r-- | src/geometry/geometry.h | 1 | ||||
-rw-r--r-- | src/geometry/geometry.test.c | 61 |
3 files changed, 94 insertions, 0 deletions
diff --git a/src/geometry/geometry.c b/src/geometry/geometry.c index 6f6cbae..6c84b11 100644 --- a/src/geometry/geometry.c +++ b/src/geometry/geometry.c @@ -52,3 +52,35 @@ int kai_read_param(struct ka_param_t *dest, struct kai_tag_t *src) return 0; } + + +int kai_read_accessor(struct ka_accessor_t *dest, struct kai_tag_t *src) +{ + CHECK_TAG_TYPE("accessor"); + + long count = kai_tag_attr_to_long(src, "count", -1); + if (count == -1) { return -1; } + char *source = kai_tag_attr_to_dup(src, "source"); + if (source == NULL) { return -1; } + + dest->count = count; + dest->offset = kai_tag_attr_to_long(src, "offset", 0); + dest->source = source; + dest->stride = kai_tag_attr_to_long(src, "stride", 1); + + dest->param_count = kai_tag_num_children(src); + dest->param = malloc(dest->param_count * sizeof(struct ka_param_t)); + struct kai_tag_t *t = src->children; + int i; + for (i=0; i<dest->param_count; i++) { + int result = kai_read_param(dest->param + i, t); + if (result != 0) { + free(dest->param); + free(dest->source); + return -1; + } + t = t->next; + } + + return 0; +} diff --git a/src/geometry/geometry.h b/src/geometry/geometry.h index acb27ee..daa883d 100644 --- a/src/geometry/geometry.h +++ b/src/geometry/geometry.h @@ -6,5 +6,6 @@ int kai_read_float_array(struct ka_float_array_t *dest, struct kai_tag_t *src); int kai_read_param(struct ka_param_t *dest, struct kai_tag_t *src); +int kai_read_accessor(struct ka_accessor_t *dest, struct kai_tag_t *src); #endif diff --git a/src/geometry/geometry.test.c b/src/geometry/geometry.test.c index dec16e9..2c172f2 100644 --- a/src/geometry/geometry.test.c +++ b/src/geometry/geometry.test.c @@ -117,5 +117,66 @@ LILY_TEST("read param tag") #include LILY_PUSH_TEST() +LILY_TEST("fail to read non-accessor tag") +{ + struct kai_tag_t *t = kai_parse_string( + "<tag />" + ); + + struct ka_accessor_t accessor; + int result = kai_read_accessor(&accessor, t); + CHECK_EQ(result, -1, "%d"); + + kai_tag_destroy(t); +} +#include LILY_PUSH_TEST() + + +LILY_TEST("fail to read accessor tag with no specified count or source") +{ + struct kai_tag_t *t = kai_parse_string( + "<accessor />" + ); + + struct ka_accessor_t accessor; + int result = kai_read_accessor(&accessor, t); + CHECK_EQ(result, -1, "%d"); + + kai_tag_destroy(t); +} +#include LILY_PUSH_TEST() + + +LILY_TEST("parse accessor") +{ + struct kai_tag_t *t = kai_parse_string( + "<accessor source=\"some id\" count=\"6\" stride=\"3\">" + " <param type=\"float\" name=\"x\" />" + " <param type=\"float\" name=\"y\" />" + " <param type=\"float\" name=\"z\" />" + "</accessor>" + ); + + struct ka_accessor_t acc; + int result = kai_read_accessor(&acc, t); + kai_tag_destroy(t); + + CHECK_EQ(result, 0, "%d"); + CHECK_EQ(acc.count, 6, "%d"); + CHECK_EQ(acc.offset, 0, "%d"); + CHECK_EQS(acc.source, "some id"); + CHECK_EQ(acc.stride, 3, "%d"); + + REQUIRE_EQ(acc.param_count, 3, "%d"); + CHECK_EQS(acc.param[0].name, "x"); + CHECK_EQS(acc.param[1].name, "y"); + CHECK_EQS(acc.param[2].name, "z"); + + free(acc.source); + for (int i=0; i<3; i++) free(acc.param[i].name); +} +#include LILY_PUSH_TEST() + + #define LILY_FILE_END #include LILY_REGISTER_TESTS() |