X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=GrowingBuffer.cpp;h=b5dea96c24910a9d35b868b4aebbb2d0b7fba082;hb=403ab22b5e05c4d1cdfd3b6c5993bd0240c8d4d8;hp=492f2442488e8608d57da8a93320d7d3ef5a7ae8;hpb=b24b03abb90553821d627e87dac5ec07d81fd09c;p=ecparse.git diff --git a/GrowingBuffer.cpp b/GrowingBuffer.cpp index 492f244..b5dea96 100644 --- a/GrowingBuffer.cpp +++ b/GrowingBuffer.cpp @@ -4,6 +4,7 @@ #include #include +#include #include "GrowingBuffer.h" GrowingBuffer::GrowingBuffer(size_t initialSize) { @@ -37,23 +38,30 @@ void GrowingBuffer::seek(size_t position) { void* GrowingBuffer::read(void* buffer, size_t length) { size_t bytesToCopy = (length > (limit - position)) ? (limit - position) : length; - memcpy(buffer, (char*) data + position, bytesToCopy); + memcpy(buffer, (ptrdiff_t*) data + 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)); void* newData = malloc(newSize); memcpy(newData, data, position); free(data); data = newData; } - memcpy((char*) data + position, buffer, length); + memcpy((ptrdiff_t*) data + limit, buffer, length); + limit += length; +} + +void GrowingBuffer::cut() { + memcpy(data, (ptrdiff_t*) data + position, position); + limit = position; + position = 0; } void GrowingBuffer::truncate() {