be verbose
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 5 Jul 2008 13:52:56 +0000 (15:52 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 5 Jul 2008 13:52:56 +0000 (15:52 +0200)
GrowingBuffer.cpp

index e73350f..a260a3d 100644 (file)
@@ -2,22 +2,26 @@
  * © 2008 by David ‘Bombe’ Roden <bombe@pterodactylus.net>
  */
 
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stddef.h>
 #include "GrowingBuffer.h"
+#include "GlobalSettings.h"
 
 GrowingBuffer::GrowingBuffer(size_t initialSize) {
        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() {
@@ -34,11 +38,14 @@ size_t GrowingBuffer::getSize() {
 
 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.\n", __FILE__, __LINE__, bytesToCopy, length);
        memcpy(buffer, (ptrdiff_t*) 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;
 }
@@ -49,26 +56,31 @@ void GrowingBuffer::write(const void* buffer, size_t length) {
                do {
                        newSize *= 2;
                } 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);
                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);
        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;
        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;
 }
@@ -78,6 +90,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);