aboutsummaryrefslogtreecommitdiff
path: root/utils.c
diff options
context:
space:
mode:
authorKatharina Fey <kookie@spacekookie.de>2019-06-09 09:07:48 +0200
committerKatharina Fey <kookie@spacekookie.de>2019-06-09 09:12:52 +0200
commitd0cca976be6fb90f3724911c8a124bce56b3c5f9 (patch)
tree96c92dc695f81b39227fb5f52e4c2ac469fea06b /utils.c
parent7df71001ef1a9ea271ae3c409a367d6c2dd628b7 (diff)
Restructuring the main API and project
This commit rewrites pretty much the entire library. It is now much smaller and more maintainable (split over multiple files). It will now also support more features (that aren't implemented yet). Adding two examples to show how to use the new API. Also changing the name of the library everywhere.
Diffstat (limited to 'utils.c')
-rw-r--r--utils.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/utils.c b/utils.c
new file mode 100644
index 0000000..9e5a027
--- /dev/null
+++ b/utils.c
@@ -0,0 +1,46 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include "utils.h"
+
+#define DELTA 0x2
+#define OVERSHOOT 0x8
+
+err_t _array_rescale(void ***ptr, size_t *len, size_t use)
+{
+ CHECK(len, INVALID_PARAMS)
+ CHECK((use <= *len), INVALID_PARAMS)
+ CHECK(ptr, INVALID_PARAMS)
+
+ // This is a simple linear scale strategy
+ if ((int) (*len - OVERSHOOT) >= (int) (use + DELTA)) *len -= OVERSHOOT;
+ if (use + DELTA >= *len) *len += DELTA;
+
+ *ptr = realloc(*ptr, *len * sizeof(void *));
+ return OK;
+}
+
+err_t _array_search(void **ptr, size_t len, size_t *out, void *in)
+{
+ CHECK(ptr, INVALID_PARAMS)
+ CHECK(in, INVALID_PARAMS)
+ CHECK((len > 0), INVALID_PARAMS)
+ (*out) = -1;
+
+ for(int i = 0; i < len; i++)
+ if((*ptr) == in) (*out) = i;
+
+ return OK;
+}
+
+err_t _array_remove(void **ptr, size_t idx, size_t len, void **out)
+{
+ CHECK(ptr, INVALID_PARAMS)
+ CHECK((idx >= 0), INVALID_PARAMS)
+ CHECK((len > idx), INVALID_PARAMS)
+
+ if(out != NULL) (*out) = ptr[idx];
+ for(int i = idx + 1; idx < len; i++)
+ ptr[i - 1] = ptr[i];
+
+ return OK;
+}