2 * © 2008 by David ‘Bombe’ Roden <bombe@pterodactylus.net>
9 #include "GrowingBuffer.h"
10 #include "GlobalSettings.h"
12 GrowingBuffer::GrowingBuffer(size_t initialSize) {
13 this->data = malloc(initialSize);
14 this->size = initialSize;
17 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] created new GrowingBuffer with a size of %d.\n", __FILE__, __LINE__, initialSize);
20 GrowingBuffer::~GrowingBuffer() {
24 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] destroyed GrowingBuffer.\n", __FILE__, __LINE__);
27 size_t GrowingBuffer::getPosition() {
31 size_t GrowingBuffer::getLimit() {
35 size_t GrowingBuffer::getSize() {
39 void GrowingBuffer::seek(size_t position) {
40 this->position = (position > limit) ? limit : position;
41 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] setting position to %d.\n", __FILE__, __LINE__, position);
44 void* GrowingBuffer::read(void* buffer, size_t length) {
45 size_t bytesToCopy = (length > (limit - position)) ? (limit - position) : length;
46 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] copying %d bytes (%d requested) into buffer.\n", __FILE__, __LINE__, bytesToCopy, length);
47 memcpy(buffer, (ptrdiff_t*) data + position, bytesToCopy);
48 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] advancing position from %d to %d.\n", __FILE__, __LINE__, position, position + bytesToCopy);
49 position += bytesToCopy;
53 void GrowingBuffer::write(const void* buffer, size_t length) {
54 if (length > (size - limit)) {
58 } while (length > (newSize - limit));
59 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] resizing buffer from %d to %d bytes to fit in %d bytes.\n", __FILE__, __LINE__, size, newSize, length);
60 void* newData = malloc(newSize);
61 memcpy(newData, data, limit);
65 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] writing %d bytes to buffer at position %d.\n", __FILE__, __LINE__, length, limit);
66 memcpy((ptrdiff_t*) data + limit, buffer, length);
70 void GrowingBuffer::cut() {
71 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] discarding %d bytes.\n", __FILE__, __LINE__, position);
72 memcpy(data, (ptrdiff_t*) data + position, position);
77 void GrowingBuffer::truncate() {
78 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] truncating %d bytes at position %d.\n", __FILE__, __LINE__, (limit - position), position);
82 void GrowingBuffer::clear() {
83 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] clearing buffer, discarding %d bytes.\n", __FILE__, __LINE__, limit);
88 void GrowingBuffer::resize(double factor) {
92 if (size != (limit * factor)) {
93 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] resizing buffer from %d to %d bytes.\n", __FILE__, __LINE__, size, (size_t) (limit * factor));
94 void* newData = malloc((size_t) (limit * factor));
95 memcpy(newData, data, limit);
101 size_t GrowingBuffer::indexOf(char c, size_t start) {
104 foundIndex = memchr((ptrdiff_t*) data + position + start, c, (limit - start));
106 return (ptrdiff_t) foundIndex - ((ptrdiff_t) data + position);