| 529 | |
| 530 | |
| 531 | static int g_read (lua_State *L, FILE *f, int first) { |
| 532 | int nargs = lua_gettop(L) - 1; |
| 533 | int success; |
| 534 | int n; |
| 535 | clearerr(f); |
| 536 | if (nargs == 0) { /* no arguments? */ |
| 537 | success = read_line(L, f, 1); |
| 538 | n = first+1; /* to return 1 result */ |
| 539 | } |
| 540 | else { /* ensure stack space for all results and for auxlib's buffer */ |
| 541 | luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); |
| 542 | success = 1; |
| 543 | for (n = first; nargs-- && success; n++) { |
| 544 | if (lua_type(L, n) == LUA_TNUMBER) { |
| 545 | size_t l = (size_t)luaL_checkinteger(L, n); |
| 546 | success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); |
| 547 | } |
| 548 | else { |
| 549 | const char *p = luaL_checkstring(L, n); |
| 550 | if (*p == '*') p++; /* skip optional '*' (for compatibility) */ |
| 551 | switch (*p) { |
| 552 | case 'n': /* number */ |
| 553 | success = read_number(L, f); |
| 554 | break; |
| 555 | case 'l': /* line */ |
| 556 | success = read_line(L, f, 1); |
| 557 | break; |
| 558 | case 'L': /* line with end-of-line */ |
| 559 | success = read_line(L, f, 0); |
| 560 | break; |
| 561 | case 'a': /* file */ |
| 562 | read_all(L, f); /* read entire file */ |
| 563 | success = 1; /* always success */ |
| 564 | break; |
| 565 | default: |
| 566 | return luaL_argerror(L, n, "invalid format"); |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | if (ferror(f)) |
| 572 | return luaL_fileresult(L, 0, NULL); |
| 573 | if (!success) { |
| 574 | lua_pop(L, 1); /* remove last result */ |
| 575 | lua_pushnil(L); /* push nil instead */ |
| 576 | } |
| 577 | return n - first; |
| 578 | } |
| 579 | |
| 580 | |
| 581 | static int io_read (lua_State *L) { |
no test coverage detected