diff options
author | Ekaitz Zarraga <ekaitz@elenq.tech> | 2024-02-28 22:03:04 +0100 |
---|---|---|
committer | Ekaitz Zarraga <ekaitz@elenq.tech> | 2024-02-28 22:08:20 +0100 |
commit | a1f6afa69bb25191d4b206e4ecba1557629330bc (patch) | |
tree | 3041324b6f0932ef036e9de230e965a4400f2b08 /src/buffer.c |
First commit with project structure
Diffstat (limited to 'src/buffer.c')
-rw-r--r-- | src/buffer.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/buffer.c b/src/buffer.c new file mode 100644 index 0000000..2fbfe72 --- /dev/null +++ b/src/buffer.c @@ -0,0 +1,46 @@ +#include "buffer.h" +#include <string.h> +#include <malloc.h> + +bool init_growable_buffer(growable_buffer *buffer) { + buffer->size = GROW_SIZE; + buffer->used = 0; + buffer->content = malloc(GROW_SIZE); + if (!buffer->content) { + return false; + } + return true; +} + +bool grow_growable_buffer(growable_buffer *buffer, size_t at_least) { + size_t new_size = ((at_least % GROW_SIZE) + 1 ) * GROW_SIZE; + if (new_size < buffer->size){ + buffer->content = realloc(buffer->content, new_size); + if (!buffer->content){ + return false; + } + buffer->size = new_size; + return true; + } +} + +void free_growable_buffer(growable_buffer *buffer) { + free(buffer->content); + buffer->content = NULL; + buffer->used = 0; + buffer->size = 0; +} + + + +bool init_fixed_buffer(fixed_buffer *buffer, char *orig) { + buffer->content = orig; + buffer->size = strlen(orig); + return buffer->content != NULL; +} + +void free_fixed_buffer(fixed_buffer *buffer) { + free(buffer->content); + buffer->content = NULL; + buffer->size = 0; +} |