summaryrefslogtreecommitdiff
path: root/liolib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2017-11-16 14:28:36 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2017-11-16 14:28:36 -0200
commitc47111bd4e33b2dcb4fc2cceb7b268ab02eea452 (patch)
treed29e6863dcb36e83f0341bd102c85bdeb2160f0b /liolib.c
parente4e5aa85a24bba6c243aeeaedc66ec712856c6db (diff)
downloadlua-github-c47111bd4e33b2dcb4fc2cceb7b268ab02eea452.tar.gz
'io.read' accepts multiple formats in a single string argument
Diffstat (limited to 'liolib.c')
-rw-r--r--liolib.c58
1 files changed, 31 insertions, 27 deletions
diff --git a/liolib.c b/liolib.c
index 19068f87..f3c914d7 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
/*
-** $Id: liolib.c,v 2.152 2017/02/09 14:50:05 roberto Exp roberto $
+** $Id: liolib.c,v 2.153 2017/11/16 13:19:06 roberto Exp roberto $
** Standard I/O (and system) library
** See Copyright Notice in lua.h
*/
@@ -528,40 +528,44 @@ static int read_chars (lua_State *L, FILE *f, size_t n) {
static int g_read (lua_State *L, FILE *f, int first) {
int nargs = lua_gettop(L) - 1;
- int success;
- int n;
+ int nresults, success;
clearerr(f);
if (nargs == 0) { /* no arguments? */
success = read_line(L, f, 1);
- n = first+1; /* to return 1 result */
+ nresults = 1;
}
- else { /* ensure stack space for all results and for auxlib's buffer */
- luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
+ else {
success = 1;
- for (n = first; nargs-- && success; n++) {
- if (lua_type(L, n) == LUA_TNUMBER) {
- size_t l = (size_t)luaL_checkinteger(L, n);
+ nresults = 0;
+ for (; nargs-- && success; first++) {
+ luaL_checkstack(L, LUA_MINSTACK, "too many arguments");
+ if (lua_type(L, first) == LUA_TNUMBER) {
+ size_t l = (size_t)luaL_checkinteger(L, first);
success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
+ nresults++;
}
else {
- const char *p = luaL_checkstring(L, n);
+ const char *p = luaL_checkstring(L, first);
if (*p == '*') p++; /* skip optional '*' (for compatibility) */
- switch (*p) {
- case 'n': /* number */
- success = read_number(L, f);
- break;
- case 'l': /* line */
- success = read_line(L, f, 1);
- break;
- case 'L': /* line with end-of-line */
- success = read_line(L, f, 0);
- break;
- case 'a': /* file */
- read_all(L, f); /* read entire file */
- success = 1; /* always success */
- break;
- default:
- return luaL_argerror(L, n, "invalid format");
+ for (; success && *p != '\0'; p++) {
+ nresults++;
+ switch (*p) {
+ case 'n': /* number */
+ success = read_number(L, f);
+ break;
+ case 'l': /* line */
+ success = read_line(L, f, 1);
+ break;
+ case 'L': /* line with end-of-line */
+ success = read_line(L, f, 0);
+ break;
+ case 'a': /* file */
+ read_all(L, f); /* read entire file */
+ success = 1; /* always success */
+ break;
+ default:
+ return luaL_argerror(L, first, "invalid format");
+ }
}
}
}
@@ -572,7 +576,7 @@ static int g_read (lua_State *L, FILE *f, int first) {
lua_pop(L, 1); /* remove last result */
lua_pushnil(L); /* push nil instead */
}
- return n - first;
+ return nresults;
}