rewrote buffers, made io use the new buffers, fixed an fs bug or two.

This commit is contained in:
Izaya 2017-08-03 17:54:38 +10:00
parent b47fb83d73
commit d116491528
3 changed files with 56 additions and 51 deletions

View File

@ -1,5 +1,5 @@
_G.buffer = {}
function buffer.create(w,c) -- worker, close
--[[function buffer.create(w,c) -- worker, close
local t={}
t.b=""
function t.w(s,d)
@ -29,7 +29,7 @@ function buffer.create(w,c) -- worker, close
t.close = c
w(t)
return t
end
end]]--
function buffer.ucreate()
local b = {}
b.b,b.s = "","open"
@ -37,12 +37,12 @@ function buffer.ucreate()
if b.s == "open" then
s.b = s.b .. tostring(d)
return(tostring(d):len())
else return false end
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)
local ns,bs=s.b:sub(1,l),s.b:sub(l+1)
s.b = bs
return ns
elseif type(l) == "string" then
@ -52,51 +52,54 @@ function buffer.ucreate()
return oS
elseif l == "*l" then
local S=s.b:find("\n") or #s.b
local rs = sb:sub(1,S-1)
local rs = s.b: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
end
end
end
function b.close(s)
s="closed"
s.s="closed"
end
return b
end
function buffer.bcreate()
local b1, b2 = {}, {}
b1.c, b2.c, b1.b, b2.b = 1, 2, "", ""
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 b.s == "open" then
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 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
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

View File

@ -64,10 +64,11 @@ do
end
function fs.readall(f)
local s=""
repeat
while true do
c=fs.read(f,2048)
if not c then break end
s=s..c
until c==""
end
return s
end
function fs.write(h,d)

View File

@ -6,28 +6,29 @@ function io.open(n,m)
local h=fs.open(n,m)
if h then
if h and m:sub(1,1) == "w" then
return buffer.create(function(bt)
bt.s="o"
spawn("io worker: "..n,function()
while true do
if bt.s ~= "o" and bt.b == "" then fclose(h) break end
local nd = bt:read(2048)
if nd ~= nil and nd ~= "" then
fs.write(h,nd)
end
coroutine.yield()
bt=buffer.ucreate()
spawn("io worker: "..n,function()
while true do
if bt.s ~= "open" and bt.b == "" then fclose(h) break end
nd = bt.b
if nd ~= nil and nd ~= "" then
fs.write(h,nd)
end
end) end,function(bt) bt.s="c" end)
elseif h and m:sub(1,1) == "r" then
return buffer.create(function(bt)
local sb = ""
repeat
bt.b=bt.b..sb
sb=fs.read(h,2048)
until sb == "" or sb == nil
fs.close(h)
bt.b = ""
coroutine.yield()
end,function() end)
end
end)
return bt
elseif h and m:sub(1,1) == "r" then
bt = buffer.ucreate()
local sb = ""
repeat
bt.b=bt.b..sb
sb=fs.read(h,2048)
until sb == "" or sb == nil
fs.close(h)
coroutine.yield()
return bt
end
else
return false