summaryrefslogtreecommitdiff
path: root/lib/Buffer.cc
diff options
context:
space:
mode:
authorpixel <pixel>2003-11-29 02:27:08 +0000
committerpixel <pixel>2003-11-29 02:27:08 +0000
commit2331eb22166b218e69d8d24f59d7f4ac42321106 (patch)
treeb9c25ccdd1739e09779ec342d7743263c2d6543d /lib/Buffer.cc
parentb65f63d0030ba04868f179d5b18589cfff1015da (diff)
Added write pointer to buffer
Binded it into LUA
Diffstat (limited to 'lib/Buffer.cc')
-rw-r--r--lib/Buffer.cc42
1 files changed, 35 insertions, 7 deletions
diff --git a/lib/Buffer.cc b/lib/Buffer.cc
index 18eb814..cd05467 100644
--- a/lib/Buffer.cc
+++ b/lib/Buffer.cc
@@ -5,13 +5,13 @@
#include "Buffer.h"
#include "generic.h"
-Buffer::Buffer(bool _seekable) : Handle(-1), buffer(0), zero(0), realsiz(0), bufsiz(0), ptr(0), seekable(_seekable) { }
+Buffer::Buffer(bool _seekable) : Handle(-1), buffer(0), zero(0), realsiz(0), bufsiz(0), ptr(0), wptr(0), seekable(_seekable) { }
Buffer::~Buffer() {
free(buffer);
}
-Buffer::Buffer(const Buffer & b) : Handle(-1), buffer(0), zero(b.zero), realsiz(b.realsiz), bufsiz(b.bufsiz), ptr(b.ptr), seekable(b.seekable) {
+Buffer::Buffer(const Buffer & b) : Handle(-1), buffer(0), zero(b.zero), realsiz(b.realsiz), bufsiz(b.bufsiz), ptr(b.ptr), wptr(b.wptr), seekable(b.seekable) {
buffer = (Byte *) malloc(bufsiz);
memcpy(buffer, b.buffer, bufsiz);
}
@@ -20,13 +20,17 @@ ssize_t Buffer::write(const void *buf, size_t count) throw (GeneralException) {
if (!count) {
return 0;
}
- if (count + realsiz > bufsiz) {
- int numblocks = (count + realsiz) / realloc_threshold;
- int remains = (count + realsiz) % realloc_threshold;
+ if (count + wptr > bufsiz) {
+ int numblocks = (count + wptr) / realloc_threshold;
+ int remains = (count + wptr) % realloc_threshold;
buffer = (Byte *) realloc(buffer, bufsiz = ((numblocks + (remains ? 1 : 0)) * realloc_threshold));
}
- memcpy(buffer + realsiz, buf, count);
- realsiz += count;
+ memcpy(buffer + wptr, buf, count);
+ wptr += count;
+
+ if (wptr > realsiz) {
+ realsiz = wptr;
+ }
return count;
}
@@ -71,6 +75,7 @@ Buffer Buffer::operator=(const Buffer & b) {
free(buffer);
realsiz = b.realsiz;
ptr = b.ptr;
+ wptr = b.wptr;
seekable = b.seekable;
if ((bufsiz = b.bufsiz)) {
buffer = (Byte *) malloc(bufsiz);
@@ -146,3 +151,26 @@ off_t Buffer::seek(off_t off, int wheel) throw (GeneralException) {
off_t Buffer::tell() const {
return ptr;
}
+
+off_t Buffer::wseek(off_t off, int wheel) throw (GeneralException) {
+ if (!seekable) {
+ throw GeneralException("This buffer is a fifo, thus is not seekable");
+ }
+ switch (wheel) {
+ case SEEK_SET:
+ wptr = off;
+ break;
+ case SEEK_CUR:
+ wptr += off;
+ break;
+ case SEEK_END:
+ wptr = realsiz + off;
+ break;
+ }
+ operator[](wptr);
+ return wptr;
+}
+
+off_t Buffer::wtell() const {
+ return wptr;
+}