2020-03-16 17:30:22 +11:00
io = { }
2020-05-12 17:55:05 +10:00
function io . open ( path , mode ) -- string string -- table -- Open file *path* in *mode*. Returns a buffer object.
2020-03-16 17:30:22 +11:00
local f , e = fs.open ( path , mode )
if not f then return false , e end
return buffer.new ( mode , f )
end
2020-05-12 17:55:05 +10:00
function io . input ( fd ) -- table -- table -- Sets the default input stream to *fd* if provided, either as a buffer as a path. Returns the default input stream.
2020-03-16 17:30:22 +11:00
if type ( fd ) == " string " then
2020-06-11 12:55:19 +10:00
fd = io.open ( fd , " rbt " )
2020-03-16 17:30:22 +11:00
end
if fd then
os.setenv ( " STDIN " , fd )
end
return os.getenv ( " STDIN " )
end
2020-05-12 17:55:05 +10:00
function io . output ( fd ) -- table -- table -- Sets the default output stream to *fd* if provided, either as a buffer as a path. Returns the default output stream.
2020-03-16 17:30:22 +11:00
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 ( ... )
2020-03-16 17:30:22 +11:00
end
2020-03-20 13:01:50 +11:00
function io . write ( ... ) -- Writes its arguments to the default output stream.
2020-03-16 17:30:22 +11:00
io.output ( ) : write ( ... )
end
2023-07-31 08:11:24 +10:00
function print ( ... ) -- Writes each argument to the default output stream, separated by space.
2020-03-16 17:30:22 +11:00
for k , v in ipairs ( { ... } ) do
2023-08-07 13:26:20 +10:00
io.write ( ( k > 1 and " \t " or " " ) .. tostring ( v ) )
2020-03-16 17:30:22 +11:00
end
2023-07-31 08:11:24 +10:00
io.write ( " \n " )
2020-03-16 17:30:22 +11:00
end