From: David ‘Bombe’ Roden Date: Sat, 5 Jul 2008 12:55:11 +0000 (+0200) Subject: use different type for pointer arithmetic X-Git-Tag: 0.1~51 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=6cdb1a3a8775e9816948c9366c75d212fbd986c4;p=ecparse.git use different type for pointer arithmetic add method to discard everything before the current position --- diff --git a/GrowingBuffer.cpp b/GrowingBuffer.cpp index 492f244..602084c 100644 --- a/GrowingBuffer.cpp +++ b/GrowingBuffer.cpp @@ -4,6 +4,7 @@ #include #include +#include #include "GrowingBuffer.h" GrowingBuffer::GrowingBuffer(size_t initialSize) { @@ -37,7 +38,7 @@ 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; } @@ -53,7 +54,13 @@ void GrowingBuffer::write(const void* buffer, size_t length) { free(data); data = newData; } - memcpy((char*) data + position, buffer, length); + memcpy((ptrdiff_t*) data + position, buffer, length); +} + +void GrowingBuffer::cut() { + memcpy(data, (ptrdiff_t*) data + position, position); + limit = position; + position = 0; } void GrowingBuffer::truncate() { diff --git a/GrowingBuffer.h b/GrowingBuffer.h index 894267d..3efa30d 100644 --- a/GrowingBuffer.h +++ b/GrowingBuffer.h @@ -37,6 +37,7 @@ public: void seek(size_t position); void* read(void* buffer, size_t length); void write(const void* buffer, size_t length); + void cut(); void truncate(); void clear(); void resize(double factor = 1.0);