summaryrefslogtreecommitdiff
path: root/src/util/util.h
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2023-02-07 19:47:49 -0600
committersanine <sanine.not@pm.me>2023-02-07 19:47:49 -0600
commit7743fd1feb81e1659584491ca88d50bd629cccef (patch)
treeb114899c8dc8b32c740cb530e0002a228156b6c8 /src/util/util.h
parent79f4b0d33cbb490f957d081bd7c8bc97bd4c689c (diff)
add KAI_FILL_ARRAY_FROM_TAGS
Diffstat (limited to 'src/util/util.h')
-rw-r--r--src/util/util.h49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/util/util.h b/src/util/util.h
index 67d3922..71cae4e 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -2,6 +2,7 @@
#define KALMIA_UTIL_H
#include <kalmia.h>
+#include "xml/xml.h"
/** @brief add a new element to an array
@@ -39,4 +40,52 @@ size_t kai_text_to_uints(unsigned int *dest, const char *str, size_t count);
void *kai_alloc(size_t size, const char *purpose);
+
+#define KAI_FOR_CHILD_OF_TYPE(parent, child, type) \
+for ( \
+ child = kai_tag_get_first_child_with_type(parent, type); \
+ child != NULL; \
+ child = kai_tag_get_next_sibling_with_type(child, type) \
+)
+
+
+#define KAI_FILL_ARRAY_FROM_TAGS(result, dest, count, type, parent, tag_type, read, release) \
+do { \
+ unsigned int count_internal = 0; \
+ bool error = false; \
+ struct kai_tag_t *tag_internal; \
+ KAI_FOR_CHILD_OF_TYPE(parent, tag_internal, tag_type) { \
+ count_internal += 1; \
+ } \
+ \
+ if (count_internal == 0) { \
+ dest = NULL; \
+ } \
+ else { \
+ dest = malloc(count_internal * sizeof(type)); \
+ int i = 0; \
+ KAI_FOR_CHILD_OF_TYPE(parent, tag_internal, tag_type) { \
+ if (error) break; \
+ int result_internal = read(dest + i, tag_internal); \
+ if (result_internal != 0) { \
+ int j; \
+ for (j=0; j<i; j++) { \
+ release(dest[i]); \
+ } \
+ free(dest); \
+ dest = NULL; \
+ result = -1; \
+ count = 0; \
+ error = true; \
+ } \
+ i += 1; \
+ } \
+ } \
+ \
+ if (!error) { \
+ result = 0; \
+ count = count_internal; \
+ } \
+} while (0)
+
#endif