From 13b990a49dc9f18c17110f2c6d9f3c8d2e324851 Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Sat, 2 Mar 2024 23:43:38 +0100 Subject: buffer: append and fix some bool shit --- src/buffer.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'src/buffer.c') diff --git a/src/buffer.c b/src/buffer.c index 2fbfe72..dde1d90 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -12,15 +12,28 @@ bool init_growable_buffer(growable_buffer *buffer) { return true; } -bool grow_growable_buffer(growable_buffer *buffer, size_t at_least) { +char* 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; + } + return buffer->content; +} + +void append_growable_buffer(growable_buffer *buffer, char* content, + size_t length) { + size_t i; + char *buf; + + if (buffer->size <= buffer->used + length) { + buf = grow_growable_buffer(buffer, length); + } else { + buf = buffer->content; + } + + for (i=0; iused + i] = content[i]; } } -- cgit v1.2.3