[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Redirecting output to non-files?
- From: Rici Lake <lua@...>
- Date: Thu, 1 Dec 2005 19:38:33 -0500
On 1-Dec-05, at 7:23 PM, Jens Alfke wrote:
OK. Then my next question is, what are the core bottlenecks that
output goes through? In other words, what's the minimal set of
functions I have to replace? There seem to be many different ways to
write to stdout:
print()
io.write()
io.output():write()
io.stdout():write()
From the reference, it looks like print() calls io.stdout():write(),
and io.write() calls io.output:write().
Unfortunately, that is not the case; print is implemented separately
(and is in the base library, not the io library.)
However, you can redefine print fairly easily:
function print(...)
-- uncomment the following line for 5.1
-- local arg = {..., n = select('#', ...)}
for i = 1, arg.n - 1 do io.write(tostring(arg[i]), "\t") end
io.write(tostring(arg[arg.n]), "\n")
end
Would it work to create my own 'object' (table) and add a 'write'
method to it, then make that the default output file?
Unfortunately (again) the io library was not really written with this
in mind. It probably should have been.
That sounds like it should work, unless there is code inside the Lua
VM that knows that files are userdata and gropes the FILE* pointers
out of them.
Not inside the Lua VM; the lua VM itself has no i/o at all. But inside
the standard io library, yes.
That said, there is nothing to stop you from replacing the functions in
the io library either. Or, probably better, just writing your own
interface-compatible replacement.