also ported the MultICE io API

This commit is contained in:
Izaya 2017-07-31 12:10:24 +10:00
parent d477d9a069
commit 67789cccae

35
modules/lib/io.lua Normal file
View File

@ -0,0 +1,35 @@
-- not gonna lie, this was just ported from MultICE. Code is terrible and I should rewrite it.
_G.io = {}
io.write = write
function io.open(n,m)
m=m or "rb"
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()
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)
coroutine.yield()
end,function() end)
end
else
return false
end
end