lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Tue, Jul 20, 2004 at 12:36:24AM +0200, Markus Huber wrote:
> It seems to be impossible to detect if read('line*') stopped on an
> newline or fileend. Example:
> 
> input1: "blah blah" is 9 Bytes ending without a newline
> input2: "blah blah\n" is 10 Bytes ending with a newline

Just call read again. If it returns nil, you reached the end of the
file. If it returns another line, you didn't. If this approach doesn't
fit with your program structure, you can wrap up read in a function
that manages the look-ahead for you. Untested:

-- Creates a closure that returns two values on each call: the line
-- read, and a boolean indicating whether it is the last line in
-- the file.
function make_readline(handle)
	local line
	local next_line = handle:read()
	return function()
		line = next_line
		next_line = handle:read()
		return line, not next_line
	end
end		

-- Jamie Webb