summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2023-01-08 23:27:03 -0600
committersanine <sanine.not@pm.me>2023-01-08 23:27:03 -0600
commite7073c34d2ca92663d98bbb1912ad5f6e615e87f (patch)
tree9a4569ddf30041e35d15bf4b3d0592866e9b5d53
parent38a71a0e8eb25418f6a0be23960a30de566dfb7c (diff)
add kai_allocmain
-rw-r--r--CMakeLists.txt16
-rw-r--r--example/CMakeLists.txt4
-rw-r--r--example/example.c7
-rw-r--r--src/geometry/geometry.c26
-rw-r--r--src/geometry/geometry.h2
-rw-r--r--src/util/util.c11
-rw-r--r--src/util/util.h3
7 files changed, 53 insertions, 16 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index bf4279e..b22b369 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -24,14 +24,14 @@ if (KALMIA_BUILD_TESTS)
endif()
endif()
-# if (KALMIA_BUILD_EXAMPLE)
-# add_executable(kalmia-example)
-# target_link_libraries(kalmia-example kalmia)
-# if (UNIX)
-# target_link_libraries(kalmia-example m)
-# endif()
-# add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/example)
-# endif()
+if (KALMIA_BUILD_EXAMPLE)
+ add_executable(kalmia-example)
+ target_link_libraries(kalmia-example kalmia)
+ if (UNIX)
+ target_link_libraries(kalmia-example m)
+ endif()
+ add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/example)
+endif()
add_subdirectory(src)
add_subdirectory(3rdparty/ezxml)
diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt
new file mode 100644
index 0000000..7951b9f
--- /dev/null
+++ b/example/CMakeLists.txt
@@ -0,0 +1,4 @@
+project(kalmia)
+
+target_sources(kalmia-example PUBLIC example.c)
+file(COPY white-cube.dae DESTINATION ${CMAKE_BINARY_DIR})
diff --git a/example/example.c b/example/example.c
new file mode 100644
index 0000000..050afa3
--- /dev/null
+++ b/example/example.c
@@ -0,0 +1,7 @@
+#include <stdio.h>
+#include <kalmia.h>
+
+int main()
+{
+ return 0;
+}
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 <kalmia.h>
#include <ezxml.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);
#endif
diff --git a/src/util/util.c b/src/util/util.c
index 00561b7..4f79870 100644
--- a/src/util/util.c
+++ b/src/util/util.c
@@ -62,3 +62,14 @@ size_t kai_text_to_longs(long *dest, const char *str, size_t count)
return result;
}
+
+
+void *kai_alloc(size_t size, const char *purpose)
+{
+ void *buf = malloc(size);
+ if (buf == NULL) {
+ fprintf(stderr, "[kalmia] ERROR: failed to allocated %ul-byte buffer for %s\n", size, purpose);
+ return NULL;
+ }
+ return buf;
+}
diff --git a/src/util/util.h b/src/util/util.h
index 0b6bc15..d5fa636 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -34,4 +34,7 @@ size_t kai_text_to_reals(ka_real_t *dest, const char *str, size_t count);
size_t kai_text_to_longs(long *dest, const char *str, size_t count);
+
+void *kai_alloc(size_t size, const char *purpose);
+
#endif