aboutsummaryrefslogtreecommitdiff
path: root/lib.c
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2024-12-19 08:44:53 +0000
committerAryadev Chavali <aryadev@aryadevchavali.com>2024-12-19 08:44:53 +0000
commitffc8068a74cca75a66296e8bec06d1ef47d7c830 (patch)
treea97c33e70f34f0ba4ebe0b48504d4bccdc88afd0 /lib.c
parent2a4d7addaea5883213841b3ab6372346e70ccef3 (diff)
downloadobf-ffc8068a74cca75a66296e8bec06d1ef47d7c830.tar.gz
obf-ffc8068a74cca75a66296e8bec06d1ef47d7c830.tar.bz2
obf-ffc8068a74cca75a66296e8bec06d1ef47d7c830.zip
Introduce and implement vec_t
A vec_t is a dynamic array, separate to a buffer. Should make it easier to distinguish purposes using these two data types.
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c38
1 files changed, 35 insertions, 3 deletions
diff --git a/lib.c b/lib.c
index 4ef6506..0c32c3b 100644
--- a/lib.c
+++ b/lib.c
@@ -8,6 +8,12 @@
#include "./lib.h"
+bool usable_character(char c)
+{
+ return c == '>' || c == '<' || c == '+' || c == '-' || c == '-' || c == '.' ||
+ c == ',' || c == '[' || c == ']';
+}
+
buffer_t *buffer_init_str(const char *name, const char *str, size_t str_size)
{
buffer_t *buf = malloc(sizeof(*buf) + str_size + 1);
@@ -61,8 +67,34 @@ char *fread_all(FILE *fp)
return buffer.data;
}
-bool usable_character(char c)
+void vec_append(vec_t *vec, void *ptr, u64 size)
{
- return c == '>' || c == '<' || c == '+' || c == '-' || c == '-' || c == '.' ||
- c == ',' || c == '[' || c == ']';
+ vec_ensure_free(vec, size);
+ memcpy(vec->data + vec->size, ptr, size);
+ vec->size += size;
+}
+
+void vec_ensure(vec_t *vec, u64 abs_size)
+{
+ if (vec->capacity < abs_size)
+ {
+ vec->capacity = MAX(vec->capacity * 2, abs_size);
+ vec->data = realloc(vec->data, vec->capacity);
+ }
+ else if (!vec->data)
+ {
+ vec->data = calloc(1, vec->capacity);
+ }
+}
+
+void vec_ensure_free(vec_t *vec, u64 rel_size)
+{
+ vec_ensure(vec, vec->size + rel_size);
+}
+
+void vec_free(vec_t *vec)
+{
+ if (vec->data)
+ free(vec->data);
+ memset(vec, 0, sizeof(*vec));
}