From 6ca18697f9eb332c38f0b46b823089ce80347580 Mon Sep 17 00:00:00 2001 From: sanine Date: Sun, 27 Nov 2022 20:03:27 -0600 Subject: add dynamic array functions --- README.md | 2 +- include/kalmia.h | 9 +++++++++ src/CMakeLists.txt | 9 ++++++++- src/node.c | 5 +++++ src/node.h | 9 +++++++++ src/node.test.c | 8 ++++++++ src/test/test.h | 4 ++++ src/util.c | 39 +++++++++++++++++++++++++++++++++++++ src/util.h | 10 ++++++++++ src/util.test.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 150 insertions(+), 2 deletions(-) create mode 100644 src/node.c create mode 100644 src/node.h create mode 100644 src/node.test.c create mode 100644 src/util.c create mode 100644 src/util.h create mode 100644 src/util.test.c diff --git a/README.md b/README.md index 5303029..6004af3 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ kalmia ====== -A C99 COLLADA importer library. +A C89 COLLADA importer library. building diff --git a/include/kalmia.h b/include/kalmia.h index d01fb5d..73d5df3 100644 --- a/include/kalmia.h +++ b/include/kalmia.h @@ -45,6 +45,8 @@ #ifndef KALMIA_H #define KALMIA_H +#include + /* kalmia uses semantic versioning (semver.org) */ #define KALMIA_VERSION_MAJOR 0 #define KALMIA_VERSION_MINOR 0 @@ -60,6 +62,13 @@ /* format data structures */ typedef double ka_matrix_t[16]; +typedef struct ka_node_t { + ka_matrix_t transform; +} ka_node_t; +typedef struct kalmia_t { + size_t n_nodes, n_nodes_max; + ka_node_t *node; +} kalmia_t; #endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a9067ca..a82103b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,11 @@ project(kalmia) -target_sources(kalmia PUBLIC transform.c) +target_sources( + kalmia PUBLIC + transform.c + node.c + util.c +) target_include_directories(kalmia PUBLIC ezxml) target_link_libraries(kalmia ezxml) @@ -13,5 +18,7 @@ if (KALMIA_BUILD_TESTS) test/test.c transform.test.c + node.test.c + util.test.c ) endif() diff --git a/src/node.c b/src/node.c new file mode 100644 index 0000000..1ba862d --- /dev/null +++ b/src/node.c @@ -0,0 +1,5 @@ +#include +#include +#include "node.h" + + diff --git a/src/node.h b/src/node.h new file mode 100644 index 0000000..ea0bedf --- /dev/null +++ b/src/node.h @@ -0,0 +1,9 @@ +#ifndef KALMIA_NODE_H +#define KALMIA_NODE_H + +#include +#include + + + +#endif diff --git a/src/node.test.c b/src/node.test.c new file mode 100644 index 0000000..236d200 --- /dev/null +++ b/src/node.test.c @@ -0,0 +1,8 @@ +#include +#include +#include "test/test.h" + + +void suite_node() +{ +} diff --git a/src/test/test.h b/src/test/test.h index 17731b9..881384e 100644 --- a/src/test/test.h +++ b/src/test/test.h @@ -5,11 +5,15 @@ void suite_transform(); +void suite_node(); +void suite_util(); #define RUN_TESTS() \ do { \ lily_run_suite(suite_transform); \ + lily_run_suite(suite_node); \ + lily_run_suite(suite_util); \ } while (0) #endif diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..7b8120b --- /dev/null +++ b/src/util.c @@ -0,0 +1,39 @@ +#include + +#include +#include "util.h" + +void * kai_next_array_element(void **array, size_t *count, size_t *max, size_t sz) +{ + void *array_new = NULL; + if (*max == 0) { + array_new = malloc(sz); + *count = 1; + *max = 1; + } + else if (*count == *max) { + array_new = realloc(*array, 2*(*max)*sz); + *count += 1; + *max *= 2; + } + else { + array_new = *array; + *count += 1; + } + + if (array_new == NULL) + return NULL; + + *array = array_new; + return array_new + ((*count-1) * sz); +} + + +ka_node_t * kai_next_node(kalmia_t *k) +{ + return kai_next_array_element( + (void**) &(k->node), + &(k->n_nodes), &(k->n_nodes_max), + sizeof(ka_node_t) + ); +} diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..7cf72bf --- /dev/null +++ b/src/util.h @@ -0,0 +1,10 @@ +#ifndef KALMIA_UTIL_H +#define KALMIA_UTIL_H + +#include + +void * kai_next_array_element(void **array, size_t *count, size_t *max, size_t sz); + +ka_node_t * kai_next_node(kalmia_t *k); + +#endif diff --git a/src/util.test.c b/src/util.test.c new file mode 100644 index 0000000..c2f8462 --- /dev/null +++ b/src/util.test.c @@ -0,0 +1,57 @@ +#include + +#include +#include "util.h" +#include "test/test.h" + +void next_array_element(); + +void suite_util() +{ + lily_run_test(next_array_element); +} + +void next_array_element() +{ + int *arr = NULL; + size_t count = 0; + size_t max = 0; + + int *next = kai_next_array_element((void**) &arr, &count, &max, sizeof(int)); + lily_assert_not_null(arr); + lily_assert_not_null(next); + + lily_assert_ptr_equal(arr+0, next); + lily_assert_int_equal(count, 1); + lily_assert_int_equal(max, 1); + *next = 2; + + next = kai_next_array_element((void**) &arr, &count, &max, sizeof(int)); + + lily_assert_not_null(next); + lily_assert_ptr_equal(arr+1, next); + lily_assert_int_equal(count, 2); + lily_assert_int_equal(max, 2); + *next = 1; + + next = kai_next_array_element((void**) &arr, &count, &max, sizeof(int)); + + lily_assert_not_null(next); + lily_assert_ptr_equal(arr+2, next); + lily_assert_int_equal(count, 3); + lily_assert_int_equal(max, 4); + *next = 3; + + next = kai_next_array_element((void**) &arr, &count, &max, sizeof(int)); + + lily_assert_not_null(next); + lily_assert_ptr_equal(arr+3, next); + lily_assert_int_equal(count, 4); + lily_assert_int_equal(max, 4); + *next = 4; + + int expected[] = { 2, 1, 3, 4 }; + lily_assert_memory_equal(expected, arr, 4 * sizeof(int)); + + free(arr); +} -- cgit v1.2.1