From e7073c34d2ca92663d98bbb1912ad5f6e615e87f Mon Sep 17 00:00:00 2001 From: sanine Date: Sun, 8 Jan 2023 23:27:03 -0600 Subject: add kai_alloc --- src/geometry/geometry.c | 26 +++++++++++++++++++------- src/geometry/geometry.h | 2 +- 2 files changed, 20 insertions(+), 8 deletions(-) (limited to 'src/geometry') diff --git a/src/geometry/geometry.c b/src/geometry/geometry.c index 1d26a70..7046f1e 100644 --- a/src/geometry/geometry.c +++ b/src/geometry/geometry.c @@ -6,24 +6,36 @@ #include "geometry.h" -void kai_parse_float_array(struct ka_float_array_t *dest, ezxml_t src) +int kai_parse_float_array(struct ka_float_array_t *dest, ezxml_t src) { + /* get count */ + const char *count = ezxml_attr(src, "count"); + if (count == NULL) { + fprintf(stderr, "[kalmia] ERROR: required \"count\" parameter not present for float_array\n"); + return -1; + } + char *end; + dest->count = strtol(count, &end, 10); + /* get id, if present */ const char *id = ezxml_attr(src, "id"); if (id != NULL) { size_t id_len = strlen(id) + 1; - dest->id = malloc(sizeof(char) * id_len); + dest->id = kai_alloc(sizeof(char) * id_len, "float_array id"); + if (dest->id == NULL) { + return -1; + } strncpy(dest->id, id, id_len); } else { dest->id = NULL; } - /* get count */ - const char *count = ezxml_attr(src, "count"); - dest->count = strtol(count, NULL, 10); - /* parse floats */ - dest->data = malloc(sizeof(ka_real_t) * dest->count); + dest->data = kai_alloc(sizeof(ka_real_t) * dest->count, "float_array data"); + if (dest->data == NULL) { + free(dest->id); + return -1; + } kai_text_to_reals(dest->data, ezxml_txt(src), dest->count); } diff --git a/src/geometry/geometry.h b/src/geometry/geometry.h index 7da5c3d..6268e3b 100644 --- a/src/geometry/geometry.h +++ b/src/geometry/geometry.h @@ -4,6 +4,6 @@ #include #include -void kai_parse_float_array(struct ka_float_array_t *dest, ezxml_t src); +int kai_parse_float_array(struct ka_float_array_t *dest, ezxml_t src); #endif -- cgit v1.2.1