diff options
author | sanine <sanine.not@pm.me> | 2023-01-04 18:49:47 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-01-04 18:49:47 -0600 |
commit | dcb7a5389717c33f7fcfa67f5e2048cacc55d820 (patch) | |
tree | a32e1f64e1e720739e94f9b89e61addaa55882a5 | |
parent | 13a7c902c051fa8da1e476687c17bb5431d258e1 (diff) |
add kai_text_to_reals
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/util/util.c | 19 | ||||
-rw-r--r-- | src/util/util.h | 28 | ||||
-rw-r--r-- | src/util/util.test.c | 34 |
4 files changed, 82 insertions, 1 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index bf4279e..db233da 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,7 @@ target_include_directories(kalmia PUBLIC include 3rdparty 3rdparty/ezxml) if (UNIX) set_target_properties( kalmia PROPERTIES - C_STANDARD 90 + C_STANDARD 99 CMAKE_C_FLAGS "-Wall -Wextra -Werror -Wfatal-errors -Wpedantic" ) endif() diff --git a/src/util/util.c b/src/util/util.c index c77b3f4..d3e8742 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -21,3 +21,22 @@ void *kai_expand_array(void **array, size_t *count, size_t element_size) *count = new_count; return new_element; } + + +int kai_text_to_reals(ka_real_t *dest, const char *str, size_t count) +{ + char *nptr = (char*) str; + char *end; + + int result = count; + + for (int i=0; i<count; i++) { + dest[i] = KA_STR_TO_REAL(nptr, &end); + if (nptr == end) { + result -= 1; + } + nptr = end; + } + + return result; +} diff --git a/src/util/util.h b/src/util/util.h index c2f01eb..d6cdd20 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -1,6 +1,34 @@ #ifndef KALMIA_UTIL_H #define KALMIA_UTIL_H +#include <kalmia.h> + + +/** @brief add a new element to an array + * + * @param array Pointer to the array to expand + * @param len Pointer to the current length of the array + * @param element_size Size of each element in the array + * + * @returns A pointer to the newly added element of the array, or NULL on failure. + */ void * kai_expand_array(void **array, size_t *len, size_t element_size); + +/** @brief Convert a string to an array of reals + * + * This function will always fill the array with `count` elements. If there are not + * `count` elements to be converted in the string, it will fill the rest of the array + * with zeroes. + * + * @param dest Array of reals in which to store the results + * @param str The string to convert + * @param count The number of elements to convert + * + * @returns The number of conversions performed. If there were at least `count` elements + * in the string to convert, this is the same as `count`. If not, it is the number of + * actual elements present in the string. + */ +int kai_text_to_reals(ka_real_t *dest, const char *str, size_t count); + #endif diff --git a/src/util/util.test.c b/src/util/util.test.c index 9f64085..26aa3d1 100644 --- a/src/util/util.test.c +++ b/src/util/util.test.c @@ -57,5 +57,39 @@ LILY_TEST("expand array 100 times") #include LILY_PUSH_TEST() +LILY_TEST("convert string to real array") +{ + const char *str = "0 0.5 1 1.5 2"; + ka_real_t array[5]; + int conv = kai_text_to_reals(array, str, 5); + + CHECK_EQ(conv, 5, "%d"); + + CHECK_EQF(array[0], 0.0f, "%f"); + CHECK_EQF(array[1], 0.5f, "%f"); + CHECK_EQF(array[2], 1.0f, "%f"); + CHECK_EQF(array[3], 1.5f, "%f"); + CHECK_EQF(array[4], 2.0f, "%f"); +} +#include LILY_PUSH_TEST() + + +LILY_TEST("convert string to real array with fewer conversions than expected") +{ + const char *str = "0 0.5 1"; + ka_real_t array[5]; + int conv = kai_text_to_reals(array, str, 5); + + CHECK_EQ(conv, 3, "%d"); + + CHECK_EQF(array[0], 0.0f, "%f"); + CHECK_EQF(array[1], 0.5f, "%f"); + CHECK_EQF(array[2], 1.0f, "%f"); + CHECK_EQF(array[3], 0.0f, "%f"); + CHECK_EQF(array[4], 0.0f, "%f"); +} +#include LILY_PUSH_TEST() + + #define LILY_FILE_END #include LILY_REGISTER_TESTS() |