From Lua 5.2: fp:read("*L").

This commit is contained in:
Mike Pall 2011-02-20 21:07:19 +01:00
parent c823e26b8b
commit 2bae11a49a

View File

@ -3,7 +3,7 @@
** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h
**
** Major portions taken verbatim or adapted from the Lua interpreter.
** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
** Copyright (C) 1994-2011 Lua.org, PUC-Rio. See Copyright Notice in lua.h
*/
#include <errno.h>
@ -153,7 +153,7 @@ static int io_file_testeof(lua_State *L, FILE *fp)
return (c != EOF);
}
static int io_file_readline(lua_State *L, FILE *fp)
static int io_file_readline(lua_State *L, FILE *fp, size_t chop)
{
luaL_Buffer b;
luaL_buffinit(L, &b);
@ -168,7 +168,7 @@ static int io_file_readline(lua_State *L, FILE *fp)
if (len == 0 || p[len-1] != '\n') { /* Partial line? */
luaL_addsize(&b, len);
} else {
luaL_addsize(&b, len - 1); /* Don't include EOL. */
luaL_addsize(&b, len - chop); /* Keep or remove EOL. */
luaL_pushresult(&b);
return 1; /* Got at least an EOL. */
}
@ -198,7 +198,7 @@ static int io_file_read(lua_State *L, FILE *fp, int start)
int ok, n, nargs = (int)(L->top - L->base) - start;
clearerr(fp);
if (nargs == 0) {
ok = io_file_readline(L, fp);
ok = io_file_readline(L, fp, 1);
n = start+1; /* Return 1 result. */
} else {
/* The results plus the buffers go on top of the args. */
@ -211,8 +211,8 @@ static int io_file_read(lua_State *L, FILE *fp, int start)
lj_err_arg(L, n+1, LJ_ERR_INVOPT);
if (p[1] == 'n')
ok = io_file_readnum(L, fp);
else if (p[1] == 'l')
ok = io_file_readline(L, fp);
else if ((p[1] & ~0x20) == 'L')
ok = io_file_readline(L, fp, (p[1] == 'l'));
else if (p[1] == 'a')
io_file_readchars(L, fp, ~((size_t)0));
else
@ -459,7 +459,7 @@ LJLIB_CF(io_output)
LJLIB_NOREG LJLIB_CF(io_lines_iter)
{
IOFileUD *iof = io_tofile(L);
int ok = io_file_readline(L, iof->fp);
int ok = io_file_readline(L, iof->fp, 1);
if (ferror(iof->fp))
lj_err_callermsg(L, strerror(errno));
if (!ok && (iof->type & IOFILE_FLAG_CLOSE))