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

View File

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

View File

@ -6,28 +6,29 @@ function io.open(n,m)
local h=fs.open(n,m) local h=fs.open(n,m)
if h then if h then
if h and m:sub(1,1) == "w" then if h and m:sub(1,1) == "w" then
return buffer.create(function(bt) bt=buffer.ucreate()
bt.s="o" spawn("io worker: "..n,function()
spawn("io worker: "..n,function() while true do
while true do if bt.s ~= "open" and bt.b == "" then fclose(h) break end
if bt.s ~= "o" and bt.b == "" then fclose(h) break end nd = bt.b
local nd = bt:read(2048) if nd ~= nil and nd ~= "" then
if nd ~= nil and nd ~= "" then fs.write(h,nd)
fs.write(h,nd)
end
coroutine.yield()
end end
end) end,function(bt) bt.s="c" end) bt.b = ""
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)
coroutine.yield() 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 end
else else
return false return false