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 = {}
|
_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,31 +52,30 @@ 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
|
||||||
@ -88,15 +87,19 @@ function buffer.bcreate()
|
|||||||
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
|
else return end
|
||||||
else return false end
|
else return 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
|
||||||
|
@ -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)
|
||||||
|
@ -6,20 +6,21 @@ 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 ~= "o" and bt.b == "" then fclose(h) break end
|
if bt.s ~= "open" and bt.b == "" then fclose(h) break end
|
||||||
local nd = bt:read(2048)
|
nd = bt.b
|
||||||
if nd ~= nil and nd ~= "" then
|
if nd ~= nil and nd ~= "" then
|
||||||
fs.write(h,nd)
|
fs.write(h,nd)
|
||||||
end
|
end
|
||||||
|
bt.b = ""
|
||||||
coroutine.yield()
|
coroutine.yield()
|
||||||
end
|
end
|
||||||
end) end,function(bt) bt.s="c" end)
|
end)
|
||||||
|
return bt
|
||||||
elseif h and m:sub(1,1) == "r" then
|
elseif h and m:sub(1,1) == "r" then
|
||||||
return buffer.create(function(bt)
|
bt = buffer.ucreate()
|
||||||
local sb = ""
|
local sb = ""
|
||||||
repeat
|
repeat
|
||||||
bt.b=bt.b..sb
|
bt.b=bt.b..sb
|
||||||
@ -27,7 +28,7 @@ function io.open(n,m)
|
|||||||
until sb == "" or sb == nil
|
until sb == "" or sb == nil
|
||||||
fs.close(h)
|
fs.close(h)
|
||||||
coroutine.yield()
|
coroutine.yield()
|
||||||
end,function() end)
|
return bt
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
return false
|
return false
|
||||||
|
Loading…
Reference in New Issue
Block a user