rewrote io.open because it was bad

This commit is contained in:
Izaya 2019-01-09 16:15:31 +11:00
parent bbacdc6dbd
commit 64e4229c42

View File

@ -31,35 +31,34 @@ function io.newfd() -- creates a new file descriptor and returns it plus its ID
fd[nfd] = {} fd[nfd] = {}
return nfd,fd[nfd] return nfd,fd[nfd]
end end
function io.open(f,m) -- opens file *f* with mode *m* local function fdfile(f,m) -- create a fd from a file
local t={["close"]=fdc}
if type(f) == "string" then
local e,fobj = pcall(fs.open,f,m) local e,fobj = pcall(fs.open,f,m)
if not e then return false, fobj end if e and fobj then
if fobj then local fdi, fdo =io.newfd()
local fdi,nfd = io.newfd() if fobj.read then
f=fdi function fdo.read(d)
if fobj.write then
function nfd.write(d)
fobj:write(d)
end
elseif fobj.read then
function nfd.read(d)
return fobj:read(d) return fobj:read(d)
end end
elseif fobj.write then
function fdo.write(d)
return fobj:write(d)
end end
function nfd.close() end
function fdo.close()
fobj:close() fobj:close()
end end
return fdi
end end
return false
end end
if fd[f].read then function io.open(f,m) -- opens file or file descriptor *f* with mode *m*
t.read = fdr if type(f) == "string" then
f = fdfile(f,m)
end end
if fd[f].write then if fd[f] then
t.write = fdw local t = {["close"]=fdc,["read"]=fdr,["write"]=fdw,["fd"]=f,["mode"]=m}
end
t.fd = f
return t return t
end end
return false
end
end end