+/**
+ * © 2008 by David ‘Bombe’ Roden <bombe@pterodactylus.net>
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include "GrowingBuffer.h"
+
+GrowingBuffer::GrowingBuffer(size_t initialSize) {
+ this->data = malloc(1024);
+ this->size = 1024;
+ this->limit = 0;
+ this->position = 0;
+}
+
+GrowingBuffer::~GrowingBuffer() {
+ if (this->data) {
+ free(this->data);
+ }
+}
+
+size_t GrowingBuffer::getPosition() {
+ return position;
+}
+
+size_t GrowingBuffer::getLimit() {
+ return limit;
+}
+
+size_t GrowingBuffer::getSize() {
+ return size;
+}
+
+void GrowingBuffer::seek(size_t position) {
+ this->position = (position > limit) ? limit : position;
+}
+
+void* GrowingBuffer::read(void* buffer, size_t length) {
+ size_t bytesToCopy = (length > (limit - position)) ? (limit - position) : length;
+ memcpy(buffer, (char*) data + position, bytesToCopy);
+ position += bytesToCopy;
+ return buffer;
+}
+
+void GrowingBuffer::write(const void* buffer, size_t length) {
+ if (length > (size - position)) {
+ int newSize = size;
+ do {
+ newSize *= 2;
+ } while (length > (newSize - position));
+ void* newData = malloc(newSize);
+ memcpy(newData, data, position);
+ free(data);
+ data = newData;
+ }
+ memcpy((char*) data + position, buffer, length);
+}
+
+void GrowingBuffer::truncate() {
+ limit = position;
+}
+
+void GrowingBuffer::clear() {
+ position = 0;
+ limit = 0;
+}
+
+void GrowingBuffer::resize(double factor) {
+ if (factor < 1.0) {
+ factor = 1.0;
+ }
+ if (size != (limit * factor)) {
+ void* newData = malloc((size_t) (limit * factor));
+ memcpy(newData, data, limit);
+ free(data);
+ data = newData;
+ }
+}
+