"read" & "write" may receive a file handler as first argument

This commit is contained in:
Roberto Ierusalimschy 1997-10-30 18:29:09 -02:00
parent f8c95fa9e8
commit b7567b6673
1 changed files with 27 additions and 9 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: liolib.c,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ ** $Id: liolib.c,v 1.2 1997/09/23 14:12:44 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
*/ */
@ -56,20 +56,37 @@ static void pushresult (int i)
} }
static int ishandler (lua_Object f)
{
if (lua_isuserdata(f)) {
if (lua_tag(f) == closedtag)
lua_error("trying to access a closed file");
return lua_tag(f) == lua_tagio;
}
else return 0;
}
static FILE *getfile (char *name) static FILE *getfile (char *name)
{ {
lua_Object f = lua_getglobal(name); lua_Object f = lua_getglobal(name);
if (!lua_isuserdata(f) || lua_tag(f) != lua_tagio) { if (!ishandler(f))
if (lua_tag(f) == closedtag)
luaL_verror("file %s has been closed", name);
else
luaL_verror("global variable %s is not a file handle", name); luaL_verror("global variable %s is not a file handle", name);
}
return lua_getuserdata(f); return lua_getuserdata(f);
} }
static FILE *getfileparam (char *name, int *arg)
{
lua_Object f = lua_getparam(*arg);
if (ishandler(f)) {
(*arg)++;
return lua_getuserdata(f);
}
else
return getfile(name);
}
static void closefile (char *name) static void closefile (char *name)
{ {
FILE *f = getfile(name); FILE *f = getfile(name);
@ -154,9 +171,10 @@ static void io_appendto (void)
static void io_read (void) static void io_read (void)
{ {
FILE *f = getfile("_INPUT"); int arg = 1;
FILE *f = getfileparam("_INPUT", &arg);
char *buff; char *buff;
char *p = luaL_opt_string(1, "[^\n]*{\n}"); char *p = luaL_opt_string(arg, "[^\n]*{\n}");
int inskip = 0; /* to control {skips} */ int inskip = 0; /* to control {skips} */
int c = NEED_OTHER; int c = NEED_OTHER;
luaI_emptybuff(); luaI_emptybuff();
@ -204,8 +222,8 @@ static void io_read (void)
static void io_write (void) static void io_write (void)
{ {
FILE *f = getfile("_OUTPUT");
int arg = 1; int arg = 1;
FILE *f = getfileparam("_OUTPUT", &arg);
int status = 1; int status = 1;
char *s; char *s;
while ((s = luaL_opt_string(arg++, NULL)) != NULL) while ((s = luaL_opt_string(arg++, NULL)) != NULL)