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