From 2331eb22166b218e69d8d24f59d7f4ac42321106 Mon Sep 17 00:00:00 2001 From: pixel Date: Sat, 29 Nov 2003 02:27:08 +0000 Subject: Added write pointer to buffer Binded it into LUA --- lib/Buffer.cc | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) (limited to 'lib/Buffer.cc') 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; +} -- cgit v1.2.3