summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEkaitz Zarraga <ekaitz@elenq.tech>2024-03-02 23:43:38 +0100
committerEkaitz Zarraga <ekaitz@elenq.tech>2024-03-02 23:43:52 +0100
commit13b990a49dc9f18c17110f2c6d9f3c8d2e324851 (patch)
treef5fdd8261cda713cac529c228762e22e52d98af1 /src
parenta1f6afa69bb25191d4b206e4ecba1557629330bc (diff)
buffer: append and fix some bool shit
Diffstat (limited to 'src')
-rw-r--r--src/buffer.c23
-rw-r--r--src/buffer.h4
2 files changed, 20 insertions, 7 deletions
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; i<length; i++){
+ buf[buffer->used + i] = content[i];
}
}
diff --git a/src/buffer.h b/src/buffer.h
index 35f2bdf..67e1bf7 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -1,6 +1,5 @@
#ifndef BUFFER_H
#define BUFFER_H
-#include <stdbool.h>
#include <stddef.h>
#define GROW_SIZE 10
@@ -12,7 +11,8 @@ typedef struct {
} growable_buffer;
bool init_growable_buffer(growable_buffer *buffer);
-bool grow_growable_buffer(growable_buffer *buffer, size_t at_least);
+char *grow_growable_buffer(growable_buffer *buffer, size_t at_least);
+void append_growable_buffer(growable_buffer *buffer, char* content, size_t length);
void free_growable_buffer(growable_buffer *buffer);