OC-PsychOS/modules/lib/buffer.lua

103 lines
2.1 KiB
Lua

_G.buffer = {}
function buffer.create(w,c) -- worker, close
local t={}
t.b=""
function t.w(s,d)
s.b=s.b..tostring(d)
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
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