OC-PsychOS2/module/io.lua

40 lines
1.1 KiB
Lua
Raw Permalink Normal View History

io = {}
2020-03-20 13:01:50 +11:00
function io.open(path,mode) -- Open file *path* in *mode*. Returns a buffer object.
local f,e = fs.open(path, mode)
if not f then return false, e end
return buffer.new(mode,f)
end
2020-03-20 13:01:50 +11:00
function io.input(fd) -- Sets the default input stream to *fd* if provided, either as a buffer as a path. Returns the default input stream.
if type(fd) == "string" then
fd=io.open(fd,"rb")
end
if fd then
os.setenv("STDIN",fd)
end
return os.getenv("STDIN")
end
2020-03-20 13:01:50 +11:00
function io.output(fd) -- Sets the default output stream to *fd* if provided, either as a buffer as a path. Returns the default output stream.
if type(fd) == "string" then
fd=io.open(fd,"wb")
end
if fd then
os.setenv("STDOUT",fd)
end
return os.getenv("STDOUT")
end
2020-03-20 13:01:50 +11:00
function io.read(...) -- Reads from the default input stream.
return io.input():read(...)
end
2020-03-20 13:01:50 +11:00
function io.write(...) -- Writes its arguments to the default output stream.
io.output():write(...)
end
2020-03-20 13:01:50 +11:00
function print(...) -- Writes each argument to the default output stream, separated by newlines.
for k,v in ipairs({...}) do
io.write(tostring(v).."\n")
end
end