lua-users home
lua-l archive

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


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

Note that I am wrote input and not file. Because if its a file I can
check the filesize and so detect if the last byte of the file is an \n
or an other char. But what if I read from io.stdin?

Ok next try read('all*') it reads the whole stuff and its fast. But I
don't like to use it here because most of the time the incomming data
from io.stdin is only a few kilobytes but possible are also 20,30,40...
Megabytes at once and I am not ensure that my process owns enough
memory. Furthermore what if more streams are processed at the same time?
Ok I can program also an read(buffersize) _without *line_ and then
process this buffer in lines using gsub. But I feel very uncomfortable
becaus the end can be found in buffer, not found in buffer or
truncated due buffering... :-(

Back to the facts:

The input stream is divided in lines so its exaclty what I need. But how
I can check if the last byte was an \n or an other char?

If you look at the Lua sources (src/lib/liolib.c), the read_line function, you can see that there is no way to retreive the information you are looking for, it is dropped.

If you are stuck with standard Lua, I fear you have to use the read(buffersize) solution.

Otherwise, you can either write your own function or tweak the read function to return a flag as additional parameter.

--
Philippe Lhoste
--  (near) Paris -- France
--  Professional programmer and amateur artist
--  http://Phi.Lho.free.fr
--  --  --  --  --  --  --  --  --  --  --  --