From cb7c8031ced6e9a393e8bdc3f409687741ef9feb Mon Sep 17 00:00:00 2001 From: Pixel Date: Sun, 20 Dec 2009 03:02:33 -0800 Subject: Factorizing some bits of code. --- dalos-binaryops.lua | 40 +++------------------------------------- dalos-hexview.lua | 43 ++++--------------------------------------- dalos-limiter.lua | 48 ++++++------------------------------------------ dalos-luahandle.lua | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ dalos.lua | 5 +++-- 5 files changed, 67 insertions(+), 120 deletions(-) create mode 100644 dalos-luahandle.lua diff --git a/dalos-binaryops.lua b/dalos-binaryops.lua index 356a58b..eda76e5 100644 --- a/dalos-binaryops.lua +++ b/dalos-binaryops.lua @@ -40,40 +40,8 @@ Maximize: %b[No,Yes]{Check if you want to maximize the output} maximize = self.extra.maximize, offset = 0, size = self.extra.maximize and math.max(h1:getsize(), h2:getsize()) or math.min(h1:getsize(), h2:getsize()), - canread = function (self) return true end, - canwrite = function (self) return false end, - canseek = function (self) return true end, - canwatch = function (self) return false end, - getname = function (self) return end, - tell = function (self) return self.offset end, - getsize = function (self) return self.size end, - getmodif = function (self) return 0 end, - flush = function (self) return true end, - seek = function (self, offset, wheel) - if wheel == SEEK_SET then - self.offset = offset - elseif wheel == SEEK_CUR then - self.offset = self.offset + offset - elseif wheel == SEEK_END then - self.offset = self.size + offset - else - error "Unknown wheel" - end - if self.offset < 0 then self.offset = 0 end - if self.offset >= self.size then self.offset = self.size end - return self.offset - end, - read = function (self, dummy, count) - count = math.min(count, self.size - self.offset) - - if count == 0 then - if self.got_eof then self.lh:close() end - self.got_eof = true - return 0 - end - - self.got_eof = false - + getname = function () return self.name end, + do_read = function (self, dummy, count) self.h1:seek(self.offset) self.h2:seek(self.offset) @@ -101,9 +69,7 @@ Maximize: %b[No,Yes]{Check if you want to maximize the output} return r, t end, } - local newh = HandleLua(obj) - obj.lh = newh - self:set_houtput(newh) + self:set_houtput(dalos.luahandle(obj)) self.dcanvas:draw() else self.color = cd.YELLOW diff --git a/dalos-hexview.lua b/dalos-hexview.lua index 8aa53bb..381ffd4 100644 --- a/dalos-hexview.lua +++ b/dalos-hexview.lua @@ -50,52 +50,17 @@ dalosp.hexview = { hvo = self, ind = ind, origin = cursor, - offset = 0, size = maxsize - cursor, - canread = function (self) return true end, - canwrite = function (self) return false end, - canseek = function (self) return true end, - canwatch = function (self) return self.h:canwatch() end, getname = function (self) return self.hvo.name .. ":" .. self.ind end, - tell = function (self) return self.offset end, - getsize = function (self) return self.size end, getmodif = function (self) return self.hvo:getmodif() end, - flush = function (self) return self.hvo:flush() end, - seek = function (self, offset, wheel) - if wheel == SEEK_SET then - self.offset = offset - elseif wheel == SEEK_CUR then - self.offset = self.offset + offset - elseif wheel == SEEK_END then - self.offset = self.size + offset - else - error "Unknown wheel" - end - if self.offset < 0 then self.offset = 0 end - if self.offset >= self.size then self.offset = self.size end + do_seek = function (self) self.h:seek(self.offset + self.origin) - return self.offset end, - read = function (self, userdata, count) - count = math.min(count, self.size - self.offset) - - if count == 0 then - if self.got_eof then self.lh:close() end - self.got_eof = true - return 0 - end - - self.got_eof = false - - local r = self.h:read(count, userdata) - self.offset = self.offset + r - if r == 0 then self.got_eof = true end - return r + do_read = function (self, userdata, count) + return self.h:read(count, userdata) end, } - local newh = HandleLua(obj) - obj.lh = newh - self:set_houtput(newh, ind) + self:set_houtput(dalos.luahandle(obj), ind) end end, diff --git a/dalos-limiter.lua b/dalos-limiter.lua index 8f1a863..b423d7a 100644 --- a/dalos-limiter.lua +++ b/dalos-limiter.lua @@ -15,51 +15,16 @@ Limit: %i{The actual size this limiter is going to produce} self.color = cd.GREEN local obj = { h = h, - offset = 0, size = math.max(h:getsize(), self.limit), - canread = function (self) return true end, - canwrite = function (self) return false end, - canseek = function (self) return true end, - canwatch = function (self) return false end, - getname = function (self) return end, - tell = function (self) return self.offset end, - getsize = function (self) return self.size end, - getmodif = function (self) return 0 end, - flush = function (self) return true end, - seek = function (self, offset, wheel) - if wheel == SEEK_SET then - self.offset = offset - elseif wheel == SEEK_CUR then - self.offset = self.offset + offset - elseif wheel == SEEK_END then - self.offset = self.size + offset - else - error "Unknown wheel" - end - if self.offset < 0 then self.offset = 0 end - if self.offset >= self.size then self.offset = self.size end - self.h:seek(self.offset) - return self.offset + getname = function () return self.name end, + do_read = function (self, userdata, count) + return self.h:read(count, userdata) end, - read = function (self, userdata, count) - count = math.min(count, self.size - self.offset) - - if count == 0 then - if self.got_eof then self.lh:close() end - self.got_eof = true - return 0 - end - - self.got_eof = false - - local r = self.h:read(count, userdata) - self.offset = self.offset + r - return r + do_seek = function (self) + self.h:seek(self.offset, SEEK_SET) end, } - local newh = HandleLua(obj) - obj.lh = newh - self:set_houtput(newh) + self:set_houtput(dalos.luahandle(obj)) self.dcanvas:draw() else self.color = cd.YELLOW @@ -68,7 +33,6 @@ Limit: %i{The actual size this limiter is going to produce} end end, - create = function (d, tab) tab.ninputs = 1 tab.noutputs = 1 diff --git a/dalos-luahandle.lua b/dalos-luahandle.lua new file mode 100644 index 0000000..3254490 --- /dev/null +++ b/dalos-luahandle.lua @@ -0,0 +1,51 @@ +dalosp.luahandle = { + create = function (tab) + local obj = { + offset = 0, + canread = function (self) return true end, + canwrite = function (self) return false end, + canseek = function (self) return true end, + canwatch = function (self) return false end, + tell = function (self) return self.offset end, + getsize = function (self) return self.size end, + getmodif = function (self) return 0 end, + flush = function (self) return true end, + seek = function (self, offset, wheel) + if wheel == SEEK_SET then + self.offset = offset + elseif wheel == SEEK_CUR then + self.offset = self.offset + offset + elseif wheel == SEEK_END then + self.offset = self.size + offset + else + error "Unknown wheel" + end + if self.offset < 0 then self.offset = 0 end + if self.offset >= self.size then self.offset = self.size end + if self.do_seek then self:do_seek() end + return self.offset + end, + read = function (self, userdata, count) + count = math.min(count, self.size - self.offset) + + if count == 0 then + if self.got_eof then self.lh:close() end + self.got_eof = true + return 0 + end + + self.got_eof = false + + local r, t = self:do_read(count, userdata) + self.offset = self.offset + r + return r, t + end, + } + for k, v in pairs(tab) do obj[k] = v end + local newh = HandleLua(obj) + obj.lh = newh + self:set_houtput(newh) + end, +} + +dalos.luahandle = dalosp.luahandle.create diff --git a/dalos.lua b/dalos.lua index b0b9aee..07b534f 100644 --- a/dalos.lua +++ b/dalos.lua @@ -550,11 +550,11 @@ dalosp.object = { end, default_activate = function (self) - print "activate" + print "default activate" end, default_configure = function (self) - print "configure" + print "default configure" end, change_curinput = function (self, delta) @@ -662,6 +662,7 @@ dalos.objtype = { ---------------- +load "dalos-luahandle.lua" load "dalos-hexview.lua" load "dalos-binaryops.lua" load "dalos-limiter.lua" -- cgit v1.2.3