]> git.ipfire.org Git - thirdparty/LuaJIT.git/commitdiff
From Lua 5.2: fp:read("*L").
authorMike Pall <mike>
Sun, 20 Feb 2011 20:07:19 +0000 (21:07 +0100)
committerMike Pall <mike>
Sun, 20 Feb 2011 20:07:19 +0000 (21:07 +0100)
src/lib_io.c

index 66b174f8168b62145030a7318198596a025a9148..d3dac7a0f15aa426e28619afd2e81e782a9339dd 100644 (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))