diff options
-rw-r--r-- | lib.c | 38 | ||||
-rw-r--r-- | lib.h | 12 |
2 files changed, 47 insertions, 3 deletions
@@ -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)); } @@ -34,4 +34,16 @@ typedef struct Buffer } buffer_t; buffer_t *buffer_init_str(const char *name, const char *str, size_t str_size); + +typedef struct +{ + u64 size, capacity; + u8 *data; +} vec_t; + +void vec_append(vec_t *vec, void *ptr, u64 size); +void vec_ensure(vec_t *vec, u64 abs_size); +void vec_ensure_free(vec_t *vec, u64 rel_size); +void vec_free(vec_t *vec); + #endif |