2017-07-30 19:12:10 +10:00
|
|
|
_G.buffer = {}
|
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())
|
2017-08-03 17:54:38 +10:00
|
|
|
end
|
2017-08-03 00:27:16 +10:00
|
|
|
end
|
|
|
|
function b.read(s,l)
|
|
|
|
if b.s == "open" then
|
|
|
|
if type(l) == "number" then
|
2017-08-03 17:54:38 +10:00
|
|
|
local ns,bs=s.b:sub(1,l),s.b:sub(l+1)
|
2017-08-03 00:27:16 +10:00
|
|
|
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
|
2017-08-03 17:54:38 +10:00
|
|
|
local rs = s.b:sub(1,S-1)
|
2017-08-03 00:27:16 +10:00
|
|
|
s.b=s.b:sub(S+1)
|
|
|
|
return rs
|
2017-08-03 17:54:38 +10:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-03 00:27:16 +10:00
|
|
|
end
|
|
|
|
function b.close(s)
|
2017-08-03 17:54:38 +10:00
|
|
|
s.s="closed"
|
2017-08-03 00:27:16 +10:00
|
|
|
end
|
|
|
|
return b
|
|
|
|
end
|
|
|
|
function buffer.bcreate()
|
|
|
|
local b1, b2 = {}, {}
|
2017-08-03 17:54:38 +10:00
|
|
|
b1.c, b2.c, b1.b, b2.b, b1.s, b2.s = 1, 2, "", "", "open","open"
|
2017-08-03 00:27:16 +10:00
|
|
|
local function wt(s,d)
|
|
|
|
if s.c == 1 then s = b2 else s = b1 end -- I'm a terrible person
|
2017-08-03 17:54:38 +10:00
|
|
|
if s.s == "open" then
|
2017-08-03 00:27:16 +10:00
|
|
|
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
|
2017-08-03 17:54:38 +10:00
|
|
|
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 = s.b:sub(1,S-1)
|
|
|
|
s.b=s.b:sub(S+1)
|
|
|
|
return rs
|
|
|
|
else return end
|
|
|
|
else return end
|
2017-08-03 00:27:16 +10:00
|
|
|
end
|
|
|
|
local function cl(s)
|
|
|
|
b1.s = "closed"
|
|
|
|
b2.s = "closed"
|
|
|
|
end
|
2017-08-03 17:54:38 +10:00
|
|
|
b1.write, b2.write = wt, wt
|
|
|
|
b1.read, b2.read = rd,rd
|
|
|
|
b1.close, b2.close = cl,cl
|
|
|
|
return b1, b2
|
2017-08-03 00:27:16 +10:00
|
|
|
end
|
2017-08-03 17:54:38 +10:00
|
|
|
buffer.create = buffer.ucreate
|