mirror of
https://github.com/lua/lua
synced 2025-04-15 17:32:49 +03:00
io.lines() iterate over the standard input file
This commit is contained in:
parent
46b063ef59
commit
669129a6d8
35
liolib.c
35
liolib.c
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: liolib.c,v 2.18 2002/09/17 20:35:54 roberto Exp roberto $
|
** $Id: liolib.c,v 2.19 2002/09/19 20:12:47 roberto Exp roberto $
|
||||||
** Standard I/O (and system) library
|
** Standard I/O (and system) library
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@ -194,28 +194,39 @@ static int io_output (lua_State *L) {
|
|||||||
|
|
||||||
static int io_readline (lua_State *L);
|
static int io_readline (lua_State *L);
|
||||||
|
|
||||||
static int io_lines (lua_State *L) {
|
|
||||||
FILE *f = fopen(luaL_check_string(L, 1), "r");
|
static void aux_lines (lua_State *L, int index, int close) {
|
||||||
luaL_arg_check(L, f, 1, strerror(errno));
|
|
||||||
lua_pushliteral(L, FILEHANDLE);
|
lua_pushliteral(L, FILEHANDLE);
|
||||||
lua_rawget(L, LUA_REGISTRYINDEX);
|
lua_rawget(L, LUA_REGISTRYINDEX);
|
||||||
newfile(L, f);
|
lua_pushvalue(L, index);
|
||||||
lua_pushboolean(L, 1); /* must close file when finished */
|
lua_pushboolean(L, close); /* close/not close file when finished */
|
||||||
lua_pushcclosure(L, io_readline, 3);
|
lua_pushcclosure(L, io_readline, 3);
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int f_lines (lua_State *L) {
|
static int f_lines (lua_State *L) {
|
||||||
tofile(L, 1); /* check that it's a valid file handle */
|
tofile(L, 1); /* check that it's a valid file handle */
|
||||||
lua_pushliteral(L, FILEHANDLE);
|
aux_lines(L, 1, 0);
|
||||||
lua_rawget(L, LUA_REGISTRYINDEX);
|
|
||||||
lua_pushvalue(L, 1);
|
|
||||||
lua_pushboolean(L, 0); /* does not close file when finished */
|
|
||||||
lua_pushcclosure(L, io_readline, 3);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int io_lines (lua_State *L) {
|
||||||
|
if (lua_isnoneornil(L, 1)) { /* no arguments? */
|
||||||
|
lua_pushstring(L, IO_INPUT);
|
||||||
|
lua_rawget(L, lua_upvalueindex(1)); /* will iterate over default input */
|
||||||
|
return f_lines(L);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
FILE *f = fopen(luaL_check_string(L, 1), "r");
|
||||||
|
luaL_arg_check(L, f, 1, strerror(errno));
|
||||||
|
newfile(L, f);
|
||||||
|
aux_lines(L, lua_gettop(L), 1);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** {======================================================
|
** {======================================================
|
||||||
** READ
|
** READ
|
||||||
|
Loading…
x
Reference in New Issue
Block a user