** Return an iteration function for 'io.lines'. If file has to be ** closed, also returns the file itself as a second result (to be ** closed as the state at the exit of a generic for). */
| 370 | ** closed as the state at the exit of a generic for). |
| 371 | */ |
| 372 | static int io_lines (lua_State *L) { |
| 373 | int toclose; |
| 374 | if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */ |
| 375 | if (lua_isnil(L, 1)) { /* no file name? */ |
| 376 | lua_getfield(L, LUA_REGISTRYINDEX, IO_INPUT); /* get default input */ |
| 377 | lua_replace(L, 1); /* put it at index 1 */ |
| 378 | tofile(L); /* check that it's a valid file handle */ |
| 379 | toclose = 0; /* do not close it after iteration */ |
| 380 | } |
| 381 | else { /* open a new file */ |
| 382 | const char *filename = luaL_checkstring(L, 1); |
| 383 | opencheck(L, filename, "r"); |
| 384 | lua_replace(L, 1); /* put file at index 1 */ |
| 385 | toclose = 1; /* close it after iteration */ |
| 386 | } |
| 387 | aux_lines(L, toclose); /* push iteration function */ |
| 388 | if (toclose) { |
| 389 | lua_pushnil(L); /* state */ |
| 390 | lua_pushnil(L); /* control */ |
| 391 | lua_pushvalue(L, 1); /* file is the to-be-closed variable (4th result) */ |
| 392 | return 4; |
| 393 | } |
| 394 | else |
| 395 | return 1; |
| 396 | } |
| 397 | |
| 398 | |
| 399 | /* |
nothing calls this directly
no test coverage detected