2 * © 2008 by David ‘Bombe’ Roden <bombe@pterodactylus.net>
8 #include "GrowingBuffer.h"
9 #include "GlobalSettings.h"
11 GrowingBuffer::GrowingBuffer(size_t initialSize) {
12 this->data = malloc(initialSize);
13 this->size = initialSize;
16 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] created new GrowingBuffer with a size of %d.\n", __FILE__, __LINE__, initialSize);
19 GrowingBuffer::~GrowingBuffer() {
23 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] destroyed GrowingBuffer.\n", __FILE__, __LINE__);
26 size_t GrowingBuffer::getPosition() {
30 size_t GrowingBuffer::getLimit() {
34 size_t GrowingBuffer::getSize() {
38 size_t GrowingBuffer::getRemaining() {
39 return limit - position;
42 void GrowingBuffer::seek(size_t position) {
43 this->position = (position > limit) ? limit : position;
44 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] setting position to %d.\n", __FILE__, __LINE__, position);
47 void* GrowingBuffer::read(void* buffer, size_t length) {
48 size_t bytesToCopy = (length > (limit - position)) ? (limit - position) : length;
49 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] copying %d bytes (%d requested) into buffer from position %d.\n", __FILE__, __LINE__, bytesToCopy, length, position);
50 memcpy(buffer, (char*) data + position, bytesToCopy);
51 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] advancing position from %d to %d.\n", __FILE__, __LINE__, position, position + bytesToCopy);
52 position += bytesToCopy;
56 void GrowingBuffer::write(const void* buffer, size_t length) {
57 if (length > (size - limit)) {
61 } while (length > (newSize - limit));
62 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] resizing buffer from %d to %d bytes to fit in %d bytes.\n", __FILE__, __LINE__, size, newSize, length);
63 void* newData = malloc(newSize);
64 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] copying %d bytes into new buffer.\n", __FILE__, __LINE__, limit);
65 memcpy(newData, data, limit);
69 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] writing %d bytes to buffer at position %d.\n", __FILE__, __LINE__, length, limit);
70 memcpy((char*) data + limit, buffer, length);
74 void GrowingBuffer::cut() {
75 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] discarding %d bytes.\n", __FILE__, __LINE__, position);
76 memcpy(data, (char*) data + position, position);
81 void GrowingBuffer::truncate() {
82 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] truncating %d bytes at position %d.\n", __FILE__, __LINE__, (limit - position), position);
86 void GrowingBuffer::clear() {
87 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] clearing buffer, discarding %d bytes.\n", __FILE__, __LINE__, limit);
92 void GrowingBuffer::resize(double factor) {
96 if (size != (limit * factor)) {
97 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] resizing buffer from %d to %d bytes.\n", __FILE__, __LINE__, size, (size_t) (limit * factor));
98 void* newData = malloc((size_t) (limit * factor));
99 memcpy(newData, data, limit);
105 size_t GrowingBuffer::indexOf(char c, size_t start) {
108 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] searching for (char) 0x%02x from %d to %d.\n", __FILE__, __LINE__, c, position + start, position + (limit - start));
109 foundIndex = memchr((char*) data + position + start, c, (limit - (position + start)));
111 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] found character at %d.\n", __FILE__, __LINE__, (char*) foundIndex - ((char*) data + position));
112 return (char*) foundIndex - ((char*) data - position);