OC-PsychOS/modules/lib/io.lua

36 lines
809 B
Lua
Raw Normal View History

2017-07-31 12:10:24 +10:00
-- 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