#include "buffer.h" #include #include 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; } 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); buffer->size = new_size; } 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]; } } 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; }