rewrote buffers, made io use the new buffers, fixed an fs bug or two.
This commit is contained in:
parent
b47fb83d73
commit
d116491528
@ -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,31 +52,30 @@ 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
|
||||
@ -88,15 +87,19 @@ function buffer.bcreate()
|
||||
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
|
||||
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
|
||||
|
@ -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)
|
||||
|
@ -6,20 +6,21 @@ 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"
|
||||
bt=buffer.ucreate()
|
||||
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 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
|
||||
bt.b = ""
|
||||
coroutine.yield()
|
||||
end
|
||||
end) end,function(bt) bt.s="c" end)
|
||||
end)
|
||||
return bt
|
||||
elseif h and m:sub(1,1) == "r" then
|
||||
return buffer.create(function(bt)
|
||||
bt = buffer.ucreate()
|
||||
local sb = ""
|
||||
repeat
|
||||
bt.b=bt.b..sb
|
||||
@ -27,7 +28,7 @@ function io.open(n,m)
|
||||
until sb == "" or sb == nil
|
||||
fs.close(h)
|
||||
coroutine.yield()
|
||||
end,function() end)
|
||||
return bt
|
||||
end
|
||||
else
|
||||
return false
|
||||
|
Loading…
Reference in New Issue
Block a user