lua-users home
lua-l archive

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


On Mon, Feb 17, 2014 at 9:10 AM, Luiz Henrique de Figueiredo
<lhf@tecgraf.puc-rio.br> wrote:
>> I just noticed that io:lines() does not cope with \0 in the lines, and thus just returns truncated lines (lua-5.2.3, but legacy 5.1 likewise).
>>
>> May I suggest replacing the call to fgets in src/liolib.c so that we can read lines with \0 data?
>
> At least in Mac OS X and Linux fgets works just fine: it reads bytes
> until it sees \n, as promised in its man page. Unfortunately, fgets does
> not tell you how many bytes it has read and you're left with having to
> call strlen to find this out. I guess we could avoid strlen and use
> memchr instead.

memchr doesn't work if fgets hits EOF before it hits \n, though.

getline(3) looks line it would work, but it's POSIX, not ISO C99, so I
don't think it's supported by MSVC.

I think the only possible implementation that complies with strict ISO
C99, doesn't lose data, and doesn't walk off the end of the buffer is
to do it all by hand, either buffering blocks of the file in memory,
or reading one character at a time.

/s/ Adam