diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2017-11-16 14:28:36 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2017-11-16 14:28:36 -0200 |
commit | c47111bd4e33b2dcb4fc2cceb7b268ab02eea452 (patch) | |
tree | d29e6863dcb36e83f0341bd102c85bdeb2160f0b /liolib.c | |
parent | e4e5aa85a24bba6c243aeeaedc66ec712856c6db (diff) | |
download | lua-c47111bd4e33b2dcb4fc2cceb7b268ab02eea452.tar.gz lua-c47111bd4e33b2dcb4fc2cceb7b268ab02eea452.tar.bz2 lua-c47111bd4e33b2dcb4fc2cceb7b268ab02eea452.zip |
'io.read' accepts multiple formats in a single string argument
Diffstat (limited to 'liolib.c')
-rw-r--r-- | liolib.c | 58 |
1 files changed, 31 insertions, 27 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: liolib.c,v 2.152 2017/02/09 14:50:05 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 2.153 2017/11/16 13:19:06 roberto Exp roberto $ |
3 | ** Standard I/O (and system) library | 3 | ** Standard I/O (and system) library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -528,40 +528,44 @@ static int read_chars (lua_State *L, FILE *f, size_t n) { | |||
528 | 528 | ||
529 | static int g_read (lua_State *L, FILE *f, int first) { | 529 | static int g_read (lua_State *L, FILE *f, int first) { |
530 | int nargs = lua_gettop(L) - 1; | 530 | int nargs = lua_gettop(L) - 1; |
531 | int success; | 531 | int nresults, success; |
532 | int n; | ||
533 | clearerr(f); | 532 | clearerr(f); |
534 | if (nargs == 0) { /* no arguments? */ | 533 | if (nargs == 0) { /* no arguments? */ |
535 | success = read_line(L, f, 1); | 534 | success = read_line(L, f, 1); |
536 | n = first+1; /* to return 1 result */ | 535 | nresults = 1; |
537 | } | 536 | } |
538 | else { /* ensure stack space for all results and for auxlib's buffer */ | 537 | else { |
539 | luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); | ||
540 | success = 1; | 538 | success = 1; |
541 | for (n = first; nargs-- && success; n++) { | 539 | nresults = 0; |
542 | if (lua_type(L, n) == LUA_TNUMBER) { | 540 | for (; nargs-- && success; first++) { |
543 | size_t l = (size_t)luaL_checkinteger(L, n); | 541 | luaL_checkstack(L, LUA_MINSTACK, "too many arguments"); |
542 | if (lua_type(L, first) == LUA_TNUMBER) { | ||
543 | size_t l = (size_t)luaL_checkinteger(L, first); | ||
544 | success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); | 544 | success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); |
545 | nresults++; | ||
545 | } | 546 | } |
546 | else { | 547 | else { |
547 | const char *p = luaL_checkstring(L, n); | 548 | const char *p = luaL_checkstring(L, first); |
548 | if (*p == '*') p++; /* skip optional '*' (for compatibility) */ | 549 | if (*p == '*') p++; /* skip optional '*' (for compatibility) */ |
549 | switch (*p) { | 550 | for (; success && *p != '\0'; p++) { |
550 | case 'n': /* number */ | 551 | nresults++; |
551 | success = read_number(L, f); | 552 | switch (*p) { |
552 | break; | 553 | case 'n': /* number */ |
553 | case 'l': /* line */ | 554 | success = read_number(L, f); |
554 | success = read_line(L, f, 1); | 555 | break; |
555 | break; | 556 | case 'l': /* line */ |
556 | case 'L': /* line with end-of-line */ | 557 | success = read_line(L, f, 1); |
557 | success = read_line(L, f, 0); | 558 | break; |
558 | break; | 559 | case 'L': /* line with end-of-line */ |
559 | case 'a': /* file */ | 560 | success = read_line(L, f, 0); |
560 | read_all(L, f); /* read entire file */ | 561 | break; |
561 | success = 1; /* always success */ | 562 | case 'a': /* file */ |
562 | break; | 563 | read_all(L, f); /* read entire file */ |
563 | default: | 564 | success = 1; /* always success */ |
564 | return luaL_argerror(L, n, "invalid format"); | 565 | break; |
566 | default: | ||
567 | return luaL_argerror(L, first, "invalid format"); | ||
568 | } | ||
565 | } | 569 | } |
566 | } | 570 | } |
567 | } | 571 | } |
@@ -572,7 +576,7 @@ static int g_read (lua_State *L, FILE *f, int first) { | |||
572 | lua_pop(L, 1); /* remove last result */ | 576 | lua_pop(L, 1); /* remove last result */ |
573 | lua_pushnil(L); /* push nil instead */ | 577 | lua_pushnil(L); /* push nil instead */ |
574 | } | 578 | } |
575 | return n - first; | 579 | return nresults; |
576 | } | 580 | } |
577 | 581 | ||
578 | 582 | ||