accidentally delet'd the wrong file

This commit is contained in:
Izaya 2019-10-28 00:35:32 +11:00
parent aaccc053d3
commit 9022bfce78
3 changed files with 117 additions and 83 deletions

View File

@ -1,4 +1,4 @@
local buffer = {}
buffer = {}
local metatable = {
__index = buffer,
__metatable = "file",

116
module/io.lua Normal file
View File

@ -0,0 +1,116 @@
do
io = {}
function io.close(file)
return (file or io.output()):close()
end
function io.flush()
return io.output():flush()
end
function io.lines(filename, ...)
if filename then
local file, reason = io.open(filename)
if not file then
error(reason, 2)
end
local args = table.pack(...)
return function()
local result = table.pack(file:read(table.unpack(args, 1, args.n)))
if not result[1] then
if result[2] then
error(result[2], 2)
else -- eof
file:close()
return nil
end
end
return table.unpack(result, 1, result.n)
end
else
return io.input():lines()
end
end
function io.open(path, mode)
local stream, result = fs.open(path, mode)
if stream then
return buffer.new(mode, stream)
else
return nil, result
end
end
local fdt = {[0]="STDIN","STDOUT","STDERR"}
local function getfh(fd)
return os.getenv(fdt[fd] or "FILE"..tostring(fd))
end
local function setfh(fd,fh)
os.setenv(fdt[fd] or "FILE"..tostring(fd),fh)
end
function io.stream(fd,file,mode)
checkArg(1,fd,'number')
assert(fd>=0,'fd must be >= 0. 0 is input, 1 is stdout, 2 is stderr')
if file then
if type(file) == "string" then
local result, reason = io.open(file, mode)
if not result then
error(reason, 2)
end
file = result
elseif not io.type(file) then
error("bad argument #1 (string or file expected, got " .. type(file) .. ")", 2)
end
setfh(fd,file)
end
return getfh(fd)
end
function io.input(file)
return io.stream(0, file, 'r')
end
function io.output(file)
return io.stream(1, file,'w')
end
function io.error(file)
return io.stream(2, file,'w')
end
function io.read(...)
return io.input():read(...)
end
function io.tmpfile()
local name = os.tmpname()
if name then
return io.open(name, "a")
end
end
function io.type(object)
if type(object) == "table" then
if getmetatable(object) == "file" then
if object.stream.handle then
return "file"
else
return "closed file"
end
end
end
return nil
end
function io.write(...)
return io.output():write(...)
end
function print(...)
for k,v in ipairs({...}) do
io.write(tostring(v).."\n")
end
end
end

View File

@ -1,82 +0,0 @@
do
io = {}
function io.type(fh)
if type(fh) ~= "table" then return nil end
if fh.state == "open" then
return "file"
elseif fh.state == "closed" then
return "closed file"
end
return nil
end
function io.read(buf, n)
n = n or buf
buf = buf or io.input()
print("bread",type(buf),n)
if not buf.aread then return nil end
if not buf.abmode then
buffer.write(buf,buf.fh:read(buf.m - buf.b:len()))
end
local rv = buffer.read(buf,n)
buffer.write(buf,buf.fh:read(buf.m - buf.b:len()))
return rv
end
function io.write(buf, d)
d = d or buf
buf = buf or io.output()
print("bwrite",type(buf),d)
if not buf.awrite then return nil end
if buf.b:len() + d:len() > buf.m then
buf.fh:write(buffer.read(buf,buf.m))
end
local rv = buffer.write(buf,d)
if not buf.abmode then
buf.fh:write(buffer.read(buf,buf.m))
end
return rv
end
function io.close(fh)
fh.fh.close()
fh.state = "closed"
end
function io.flush()
end
function io.open(fname,mode)
mode=mode or "r"
local buf = buffer.new()
buf.fh, er = fs.open(fname,mode)
if not buf.fh then
error(er)
end
buf.state = "open"
buf.aread = mode:match("r")
buf.awrite = mode:match("w") or mode:match("a")
setmetatable(buf,{__index=io})
return buf
end
function print(...)
for k,v in ipairs({...}) do
io.write(string.format("%s\n",tostring(v)))
end
end
io.stdin = io.open("/dev/null")
io.stdout = io.open("/dev/null","w")
function io.input(fname)
if not fname then return os.getenv("STDIN") or io.stdin end
os.setenv("STDIN",io.open(fname))
end
function io.output(fname)
if not fname then return os.getenv("STDOUT") or io.stdout end
os.setenv("STDOUT",io.open(fname,"w"))
end
end