diff options
-rw-r--r-- | include/Buffer.h | 4 | ||||
-rw-r--r-- | lib/Buffer.cc | 42 | ||||
-rw-r--r-- | lib/LuaHandle.cc | 44 |
3 files changed, 82 insertions, 8 deletions
diff --git a/include/Buffer.h b/include/Buffer.h index 8996b2d..fb2cab4 100644 --- a/include/Buffer.h +++ b/include/Buffer.h @@ -27,10 +27,12 @@ class Buffer : public Handle { virtual off_t tell() const; Byte operator[](size_t) const; Byte & operator[](size_t); + off_t wseek(off_t, int = SEEK_SET) throw (GeneralException); + off_t wtell() const; private: Byte * buffer, zero; - size_t realsiz, bufsiz, ptr; + size_t realsiz, bufsiz, ptr, wptr; bool seekable; }; 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; +} diff --git a/lib/LuaHandle.cc b/lib/LuaHandle.cc index fb0a228..15c3ee3 100644 --- a/lib/LuaHandle.cc +++ b/lib/LuaHandle.cc @@ -38,6 +38,8 @@ class sLuaHandle : public Base { static int setz(lua_State * L);
static int bindex(lua_State * L);
static int bnewindex(lua_State * L);
+ static int bseek(lua_State * L);
+ static int btell(lua_State * L);
private:
static int read(lua_State * L, int);
static int write(lua_State * L, int);
@@ -505,6 +507,46 @@ int sLuaHandle::bnewindex(lua_State * _L) { return 0;
}
+int sLuaHandle::btell(lua_State * _L) {
+ Lua * L = Lua::find(_L);
+ int n = L->gettop();
+ Buffer * h;
+
+ if (n != 1) {
+ L->error("Incorrect arguments to method `Buffer::wtell'");
+ }
+
+ h = (Buffer *) LuaObject::getme(L);
+ L->push((lua_Number) h->wtell());
+ return 1;
+}
+
+int sLuaHandle::bseek(lua_State * _L) {
+ Lua * L = Lua::find(_L);
+ int n = L->gettop();
+ off_t off;
+ int wheel;
+ Buffer * h;
+
+ if ((n < 2) || (n > 3) || !L->isnumber(2) || ((n == 3) && !L->isnumber(3))) {
+ L->error("Incorrect arguments to method `Buffer::wseek'");
+ }
+
+ h = (Buffer *) LuaObject::getme(L);
+
+ off = L->tonumber(2);
+
+ if (n == 3) {
+ wheel = L->tonumber(3);
+ } else {
+ wheel = SEEK_SET;
+ }
+
+ L->push((lua_Number) h->wseek(off, wheel));
+
+ return 1;
+}
+
void LuaInput::pushmembers(Lua * L) {
LuaHandle::pushmembers(L);
}
@@ -525,6 +567,8 @@ void LuaBuffer::pushmembers(Lua * L) { L->push(sLuaHandle::bnewindex);
L->settable();
L->setmetatable(1);
+ pushit(L, "wtell", sLuaHandle::btell);
+ pushit(L, "wseek", sLuaHandle::bseek);
}
void LuaHandle::pushmembers(Lua * L) {
|