diff options
author | sanine <sanine.not@pm.me> | 2023-02-06 00:30:41 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-02-06 00:30:41 -0600 |
commit | c5936b5e28c8f81e974bd8d9eb901832921c2b78 (patch) | |
tree | 4c293632d1af39c1d38d0726136d6d3a68635ef8 | |
parent | 287dcb62f19d0cf55c8f179bf4b36a7a66c7737a (diff) |
add kai_read_vertices()
-rw-r--r-- | include/kalmia.h | 9 | ||||
-rw-r--r-- | src/geometry/geometry.c | 53 | ||||
-rw-r--r-- | src/geometry/geometry.h | 3 | ||||
-rw-r--r-- | src/geometry/geometry.test.c | 57 |
4 files changed, 122 insertions, 0 deletions
diff --git a/include/kalmia.h b/include/kalmia.h index 05da78f..4dd5d1d 100644 --- a/include/kalmia.h +++ b/include/kalmia.h @@ -110,4 +110,13 @@ struct ka_input_t { }; +struct ka_vertices_t { + char *id; /* required */ + char *name; + + unsigned int input_count; + struct ka_input_t *input; +}; + + #endif diff --git a/src/geometry/geometry.c b/src/geometry/geometry.c index 0bc8602..ea04fd3 100644 --- a/src/geometry/geometry.c +++ b/src/geometry/geometry.c @@ -205,3 +205,56 @@ void kai_release_input(struct ka_input_t i) free(i.source); } + +int kai_read_vertices(struct ka_vertices_t *dest, struct kai_tag_t *src) +{ + CHECK_TAG_TYPE("vertices"); + + char *id = kai_tag_attr_to_dup(src, "id"); + if (id == NULL) { return -1; } + + dest->id = id; + dest->name = kai_tag_attr_to_dup(src, "name"); + + /* count input children */ + int count = 0; + struct kai_tag_t *child; + for (child = src->children; child != NULL; child = child->next) { + if (strcmp(child->type, "input") == 0) { count += 1; } + } + + dest->input_count = count; + dest->input = malloc(count * sizeof(struct ka_input_t)); + if (dest->input == NULL) { + kai_release_vertices(*dest); + return -1; + } + + int i=0; + for (child = src->children; child != NULL; child = child->next) { + if (strcmp(child->type, "input") == 0) { + int result = kai_read_input_unshared(dest->input + i, child); + if (result != 0) { + dest->input_count = i; + kai_release_vertices(*dest); + return -1; + } + i += 1; + } + } + + return 0; +} + +void kai_release_vertices(struct ka_vertices_t v) +{ + free(v.id); + free(v.name); + int i; + for (i=0; i<v.input_count; i++) { + kai_release_input(v.input[i]); + } + free(v.input); +} + + diff --git a/src/geometry/geometry.h b/src/geometry/geometry.h index 4e260fa..a5ebab7 100644 --- a/src/geometry/geometry.h +++ b/src/geometry/geometry.h @@ -20,4 +20,7 @@ int kai_read_input_unshared(struct ka_input_t *dest, struct kai_tag_t *src); int kai_read_input_shared(struct ka_input_t *dest, struct kai_tag_t *src); void kai_release_input(struct ka_input_t i); +int kai_read_vertices(struct ka_vertices_t *dest, struct kai_tag_t *src); +void kai_release_vertices(struct ka_vertices_t v); + #endif diff --git a/src/geometry/geometry.test.c b/src/geometry/geometry.test.c index f9dc3de..968060d 100644 --- a/src/geometry/geometry.test.c +++ b/src/geometry/geometry.test.c @@ -353,5 +353,62 @@ LILY_TEST("read input (shared)") #include LILY_PUSH_TEST() +LILY_TEST("fail to read non-vertices tag") +{ + struct kai_tag_t *t = kai_parse_string( + "<tag />" + ); + + struct ka_vertices_t vertices; + int result = kai_read_vertices(&vertices, t); + CHECK_EQ(result, -1, "%d"); + + kai_tag_destroy(t); +} +#include LILY_PUSH_TEST() + + +LILY_TEST("fail to read vertices tag with no specified id") +{ + struct kai_tag_t *t = kai_parse_string( + "<vertices />" + ); + + struct ka_vertices_t vertices; + int result = kai_read_vertices(&vertices, t); + CHECK_EQ(result, -1, "%d"); + kai_tag_destroy(t); +} +#include LILY_PUSH_TEST() + + +LILY_TEST("read vertices tag") +{ + struct kai_tag_t *t = kai_parse_string( + "<vertices id=\"xxx\" name=\"yyy\" >" + " <input semantic=\"zzz\" source=\"xxx\" />" + " <input semantic=\"www\" source=\"xxx\" />" + "</vertices>" + ); + + struct ka_vertices_t vertices; + vertices.input = NULL; + + int result = kai_read_vertices(&vertices, t); + kai_tag_destroy(t); + + REQUIRE_EQ(result, 0, "%d"); + CHECK_EQS(vertices.id, "xxx"); + CHECK_EQS(vertices.name, "yyy"); + CHECK_EQ(vertices.input_count, 2, "%d"); + CHECK_NEQ(vertices.input, NULL, "%p"); + + kai_release_vertices(vertices); +} +#include LILY_PUSH_TEST() + + + + #define LILY_FILE_END #include LILY_REGISTER_TESTS() |