X-Git-Url: https://git.pterodactylus.net/?p=ecparse.git;a=blobdiff_plain;f=GrowingBuffer.cpp;h=b1c5dbc7a1eb2cc96651580b71b9da2560dfcfd9;hp=492f2442488e8608d57da8a93320d7d3ef5a7ae8;hb=b85b2b73e8493ad0f379da96f87d0d3e12c56413;hpb=b24b03abb90553821d627e87dac5ec07d81fd09c diff --git a/GrowingBuffer.cpp b/GrowingBuffer.cpp index 492f244..b1c5dbc 100644 --- a/GrowingBuffer.cpp +++ b/GrowingBuffer.cpp @@ -2,21 +2,25 @@ * © 2008 by David ‘Bombe’ Roden */ +#include #include #include #include "GrowingBuffer.h" +#include "GlobalSettings.h" GrowingBuffer::GrowingBuffer(size_t initialSize) { - this->data = malloc(1024); - this->size = 1024; + this->data = malloc(initialSize); + this->size = initialSize; this->limit = 0; this->position = 0; + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] created new GrowingBuffer with a size of %d.\n", __FILE__, __LINE__, initialSize); } GrowingBuffer::~GrowingBuffer() { if (this->data) { free(this->data); } + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] destroyed GrowingBuffer.\n", __FILE__, __LINE__); } size_t GrowingBuffer::getPosition() { @@ -31,36 +35,56 @@ size_t GrowingBuffer::getSize() { return size; } +size_t GrowingBuffer::getRemaining() { + return limit - position; +} + void GrowingBuffer::seek(size_t position) { this->position = (position > limit) ? limit : position; + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] setting position to %d.\n", __FILE__, __LINE__, position); } void* GrowingBuffer::read(void* buffer, size_t length) { size_t bytesToCopy = (length > (limit - position)) ? (limit - position) : length; + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] copying %d bytes (%d requested) into buffer from position %d.\n", __FILE__, __LINE__, bytesToCopy, length, position); memcpy(buffer, (char*) data + position, bytesToCopy); + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] advancing position from %d to %d.\n", __FILE__, __LINE__, position, position + bytesToCopy); position += bytesToCopy; return buffer; } void GrowingBuffer::write(const void* buffer, size_t length) { - if (length > (size - position)) { + if (length > (size - limit)) { int newSize = size; do { newSize *= 2; - } while (length > (newSize - position)); + } while (length > (newSize - limit)); + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] resizing buffer from %d to %d bytes to fit in %d bytes.\n", __FILE__, __LINE__, size, newSize, length); void* newData = malloc(newSize); - memcpy(newData, data, position); + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] copying %d bytes into new buffer.\n", __FILE__, __LINE__, limit); + memcpy(newData, data, limit); free(data); data = newData; } - memcpy((char*) data + position, buffer, length); + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] writing %d bytes to buffer at position %d.\n", __FILE__, __LINE__, length, limit); + memcpy((char*) data + limit, buffer, length); + limit += length; +} + +void GrowingBuffer::cut() { + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] discarding %d bytes.\n", __FILE__, __LINE__, position); + memcpy(data, (char*) data + position, position); + limit -= position; + position = 0; } void GrowingBuffer::truncate() { + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] truncating %d bytes at position %d.\n", __FILE__, __LINE__, (limit - position), position); limit = position; } void GrowingBuffer::clear() { + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] clearing buffer, discarding %d bytes.\n", __FILE__, __LINE__, limit); position = 0; limit = 0; } @@ -70,6 +94,7 @@ void GrowingBuffer::resize(double factor) { factor = 1.0; } if (size != (limit * factor)) { + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] resizing buffer from %d to %d bytes.\n", __FILE__, __LINE__, size, (size_t) (limit * factor)); void* newData = malloc((size_t) (limit * factor)); memcpy(newData, data, limit); free(data); @@ -77,3 +102,15 @@ void GrowingBuffer::resize(double factor) { } } +size_t GrowingBuffer::indexOf(char c, size_t start) { + void* foundIndex; + + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] searching for (char) 0x%02x from %d to %d.\n", __FILE__, __LINE__, c, position + start, position + (limit - start)); + foundIndex = memchr((char*) data + position + start, c, (limit - (position + start))); + if (foundIndex) { + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] found character at %d.\n", __FILE__, __LINE__, (char*) foundIndex - ((char*) data + position)); + return (char*) foundIndex - ((char*) data - position); + } + return (size_t) -1; +} +