2017-07-30 19:12:10 +10:00
|
|
|
_G.buffer = {}
|
|
|
|
function buffer.create(w,c) -- worker, close
|
|
|
|
local t={}
|
|
|
|
t.b=""
|
|
|
|
function t.w(s,d)
|
2017-07-31 09:40:02 +10:00
|
|
|
s.b=s.b..tostring(d)
|
2017-07-30 19:12:10 +10:00
|
|
|
end
|
|
|
|
t.write = t.w
|
|
|
|
function t.r(s,l)
|
|
|
|
if type(l) == "number" then
|
|
|
|
local ns,bs=s.b:sub(1,l+1),s.b:sub(l+2)
|
|
|
|
s.b=bs
|
|
|
|
return ns
|
|
|
|
elseif type(l) == "string" then
|
|
|
|
local oS=s.b
|
|
|
|
if l == "*a" then
|
|
|
|
s.b=""
|
|
|
|
return oS
|
|
|
|
elseif l == "*l" then
|
|
|
|
S=s.b:find("\n") or #s.b
|
|
|
|
s.b=s.b:sub(S+1)
|
|
|
|
rS = oS:sub(1,S-1)
|
|
|
|
if rS:len() < 1 then return nil end
|
|
|
|
return rS
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
t.read = t.r
|
|
|
|
t.close = c
|
|
|
|
w(t)
|
|
|
|
return t
|
|
|
|
end
|
2017-08-03 00:27:16 +10:00
|
|
|
function buffer.ucreate()
|
|
|
|
local b = {}
|
|
|
|
b.b,b.s = "","open"
|
|
|
|
function b.write(s,d)
|
|
|
|
if b.s == "open" then
|
|
|
|
s.b = s.b .. tostring(d)
|
|
|
|
return(tostring(d):len())
|
|
|
|
else return false end
|
|
|
|
end
|
|
|
|
function b.read(s,l)
|
|
|
|
if b.s == "open" then
|
|
|
|
if type(l) == "number" then
|
|
|
|
local ns,bs=s.b:sub(1,l+1),s.b:sub(l+2)
|
|
|
|
s.b = bs
|
|
|
|
return ns
|
|
|
|
elseif type(l) == "string" then
|
|
|
|
if l == "*a" then
|
|
|
|
local oS = s.b
|
|
|
|
s.b = ""
|
|
|
|
return oS
|
|
|
|
elseif l == "*l" then
|
|
|
|
local S=s.b:find("\n") or #s.b
|
|
|
|
local rs = sb:sub(1,S-1)
|
|
|
|
s.b=s.b:sub(S+1)
|
|
|
|
return rs
|
|
|
|
else return false end
|
|
|
|
else return false end
|
|
|
|
else return false end
|
|
|
|
end
|
|
|
|
function b.close(s)
|
|
|
|
s="closed"
|
|
|
|
end
|
|
|
|
return b
|
|
|
|
end
|
|
|
|
function buffer.bcreate()
|
|
|
|
local b1, b2 = {}, {}
|
|
|
|
b1.c, b2.c, b1.b, b2.b = 1, 2, "", ""
|
|
|
|
local function wt(s,d)
|
|
|
|
if s.c == 1 then s = b2 else s = b1 end -- I'm a terrible person
|
|
|
|
if b.s == "open" then
|
|
|
|
s.b = s.b .. tostring(d)
|
|
|
|
return(tostring(d):len())
|
|
|
|
else return false end
|
|
|
|
end
|
|
|
|
local function rd(s,d)
|
|
|
|
if s.c == 1 then s = b2 else s = b1 end
|
|
|
|
if b.s == "open" then
|
|
|
|
if type(l) == "number" then
|
|
|
|
local ns,bs=s.b:sub(1,l+1),s.b:sub(l+2)
|
|
|
|
s.b = bs
|
|
|
|
return ns
|
|
|
|
elseif type(l) == "string" then
|
|
|
|
if l == "*a" then
|
|
|
|
local oS = s.b
|
|
|
|
s.b = ""
|
|
|
|
return oS
|
|
|
|
elseif l == "*l" then
|
|
|
|
local S=s.b:find("\n") or #s.b
|
|
|
|
local rs = sb:sub(1,S-1)
|
|
|
|
s.b=s.b:sub(S+1)
|
|
|
|
return rs
|
|
|
|
else return false end
|
|
|
|
else return false end
|
|
|
|
else return false end
|
|
|
|
end
|
|
|
|
local function cl(s)
|
|
|
|
b1.s = "closed"
|
|
|
|
b2.s = "closed"
|
|
|
|
end
|
|
|
|
end
|