OC-PsychOS/modules/lib/buffer.lua

75 lines
1.6 KiB
Lua

_G.buffer = {}
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())
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),s.b:sub(l+1)
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
end
end
end
end
function b.close(s)
s.s="closed"
end
return b
end
function buffer.bcreate()
local b1, b2 = {}, {}
b1.c, b2.c, b1.b, b2.b, b1.s, b2.s = 1, 2, "", "", "open","open"
local function wt(s,d)
if s.c == 1 then s = b2 else s = b1 end -- I'm a terrible person
if s.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 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
end
local function cl(s)
b1.s = "closed"
b2.s = "closed"
end
b1.write, b2.write = wt, wt
b1.read, b2.read = rd,rd
b1.close, b2.close = cl,cl
return b1, b2
end
buffer.create = buffer.ucreate