rewrote io.open because it was bad
This commit is contained in:
parent
bbacdc6dbd
commit
64e4229c42
@ -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()
|
|
||||||
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)
|
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
|
||||||
|
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].read then
|
if fd[f] then
|
||||||
t.read = fdr
|
local t = {["close"]=fdc,["read"]=fdr,["write"]=fdw,["fd"]=f,["mode"]=m}
|
||||||
end
|
|
||||||
if fd[f].write then
|
|
||||||
t.write = fdw
|
|
||||||
end
|
|
||||||
t.fd = f
|
|
||||||
return t
|
return t
|
||||||
|
end
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user