add growing buffer
[ecparse.git] / GrowingBuffer.h
diff --git a/GrowingBuffer.h b/GrowingBuffer.h
new file mode 100644 (file)
index 0000000..894267d
--- /dev/null
@@ -0,0 +1,51 @@
+/**
+ * © 2008 by David ‘Bombe’ Roden <bombe@pterodactylus.net>
+ */
+
+#pragma once
+
+/**
+ * A buffer that will grow to accomodate the data being put into it.
+ */
+class GrowingBuffer {
+
+public:
+
+       /**
+        * Empty constructor. Creates a buffer with a default size of 1024 bytes.
+        *
+        * @param initialSize The initial size of the buffer in bytes
+        */
+       GrowingBuffer(size_t initialSize = 1024);
+
+       /**
+        * Destructor. Frees all used resources.
+        */
+       ~GrowingBuffer();
+
+       size_t getPosition();
+       size_t getLimit();
+       size_t getSize();
+
+       /**
+        * Sets the position of the buffer. The next {@link read(void*, size_t)} or
+        * {@link write(const void*, size_t)} operation will be applied at the new
+        * position.
+        *
+        * @param position The position to seek to
+        */
+       void seek(size_t position);
+       void* read(void* buffer, size_t length);
+       void write(const void* buffer, size_t length);
+       void truncate();
+       void clear();
+       void resize(double factor = 1.0);
+
+private:
+       void* data;
+       size_t size;
+       size_t limit;
+       size_t position;
+
+};
+