X-Git-Url: https://git.pterodactylus.net/?p=ecparse.git;a=blobdiff_plain;f=GrowingBuffer.cpp;h=2d26251d66587b3335be8af9b0ce175cc0ae7cc8;hp=8e3bd4a8472207391893c4b1f873b28a1352e0e7;hb=ee4c05de3061d1d7abfd1710823442cbc0cfbf46;hpb=9f80044fab3651986bdd2651620003f8769647b0 diff --git a/GrowingBuffer.cpp b/GrowingBuffer.cpp index 8e3bd4a..2d26251 100644 --- a/GrowingBuffer.cpp +++ b/GrowingBuffer.cpp @@ -5,7 +5,6 @@ #include #include #include -#include #include "GrowingBuffer.h" #include "GlobalSettings.h" @@ -36,6 +35,10 @@ 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); @@ -43,8 +46,8 @@ void GrowingBuffer::seek(size_t 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.\n", __FILE__, __LINE__, bytesToCopy, length); - memcpy(buffer, (ptrdiff_t*) data + position, bytesToCopy); + 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; @@ -58,19 +61,20 @@ void GrowingBuffer::write(const void* buffer, size_t length) { } 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; } GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] writing %d bytes to buffer at position %d.\n", __FILE__, __LINE__, length, limit); - memcpy((ptrdiff_t*) data + limit, buffer, length); + 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, (ptrdiff_t*) data + position, position); - limit = position; + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] discarding %d bytes, %d bytes remaining.\n", __FILE__, __LINE__, position, limit - position); + memmove(data, (char*) data + position, limit); + limit -= position; position = 0; } @@ -101,10 +105,12 @@ void GrowingBuffer::resize(double factor) { size_t GrowingBuffer::indexOf(char c, size_t start) { void* foundIndex; - foundIndex = memchr((ptrdiff_t*) data + position + start, c, (limit - start)); + 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) { - return (ptrdiff_t) foundIndex - ((ptrdiff_t) data + position); + 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 -1; + return (size_t) -1; }