OC-PsychOS/modules/lib/io.lua

37 lines
727 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
bt=buffer.ucreate()
spawn("io worker: "..n,function()
while true do
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)
2017-07-31 12:10:24 +10:00
end
bt.b = ""
2017-07-31 12:10:24 +10:00
coroutine.yield()
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
2017-07-31 12:10:24 +10:00
end
else
return false
end
end