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
1 changed files with 23 additions and 24 deletions

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} local e,fobj = pcall(fs.open,f,m)
if type(f) == "string" then if e and fobj then
local e,fobj=pcall(fs.open,f,m) local fdi, fdo =io.newfd()
if not e then return false, fobj end if fobj.read then
if fobj then function fdo.read(d)
local fdi,nfd = io.newfd() return fobj:read(d)
f=fdi
if fobj.write then
function nfd.write(d)
fobj:write(d)
end
elseif fobj.read then
function nfd.read(d)
return fobj:read(d)
end
end end
function nfd.close() elseif fobj.write then
fobj:close() function fdo.write(d)
return fobj:write(d)
end end
end end
function fdo.close()
fobj:close()
end
return fdi
end end
if fd[f].read then return false
t.read = fdr end
function io.open(f,m) -- opens file or file descriptor *f* with mode *m*
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}
return t
end end
t.fd = f return false
return t
end end
end end