2 * © 2008 by David ‘Bombe’ Roden <bombe@pterodactylus.net>
8 #include "GrowingBuffer.h"
10 GrowingBuffer::GrowingBuffer(size_t initialSize) {
11 this->data = malloc(1024);
17 GrowingBuffer::~GrowingBuffer() {
23 size_t GrowingBuffer::getPosition() {
27 size_t GrowingBuffer::getLimit() {
31 size_t GrowingBuffer::getSize() {
35 void GrowingBuffer::seek(size_t position) {
36 this->position = (position > limit) ? limit : position;
39 void* GrowingBuffer::read(void* buffer, size_t length) {
40 size_t bytesToCopy = (length > (limit - position)) ? (limit - position) : length;
41 memcpy(buffer, (ptrdiff_t*) data + position, bytesToCopy);
42 position += bytesToCopy;
46 void GrowingBuffer::write(const void* buffer, size_t length) {
47 if (length > (size - limit)) {
51 } while (length > (newSize - limit));
52 void* newData = malloc(newSize);
53 memcpy(newData, data, position);
57 memcpy((ptrdiff_t*) data + limit, buffer, length);
61 void GrowingBuffer::cut() {
62 memcpy(data, (ptrdiff_t*) data + position, position);
67 void GrowingBuffer::truncate() {
71 void GrowingBuffer::clear() {
76 void GrowingBuffer::resize(double factor) {
80 if (size != (limit * factor)) {
81 void* newData = malloc((size_t) (limit * factor));
82 memcpy(newData, data, limit);