2 * © 2008 by David ‘Bombe’ Roden <bombe@pterodactylus.net>
7 #include "GrowingBuffer.h"
9 GrowingBuffer::GrowingBuffer(size_t initialSize) {
10 this->data = malloc(1024);
16 GrowingBuffer::~GrowingBuffer() {
22 size_t GrowingBuffer::getPosition() {
26 size_t GrowingBuffer::getLimit() {
30 size_t GrowingBuffer::getSize() {
34 void GrowingBuffer::seek(size_t position) {
35 this->position = (position > limit) ? limit : position;
38 void* GrowingBuffer::read(void* buffer, size_t length) {
39 size_t bytesToCopy = (length > (limit - position)) ? (limit - position) : length;
40 memcpy(buffer, (char*) data + position, bytesToCopy);
41 position += bytesToCopy;
45 void GrowingBuffer::write(const void* buffer, size_t length) {
46 if (length > (size - position)) {
50 } while (length > (newSize - position));
51 void* newData = malloc(newSize);
52 memcpy(newData, data, position);
56 memcpy((char*) data + position, buffer, length);
59 void GrowingBuffer::truncate() {
63 void GrowingBuffer::clear() {
68 void GrowingBuffer::resize(double factor) {
72 if (size != (limit * factor)) {
73 void* newData = malloc((size_t) (limit * factor));
74 memcpy(newData, data, limit);