`io.lines' also can give the file name in its error message
This commit is contained in:
17
liolib.c
17
liolib.c
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: liolib.c,v 2.54 2004/07/09 15:47:48 roberto Exp roberto $
|
** $Id: liolib.c,v 2.55 2004/07/09 16:01:38 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
|
||||||
*/
|
*/
|
||||||
@@ -44,6 +44,12 @@ static int pushresult (lua_State *L, int i, const char *filename) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void fileerror (lua_State *L, int arg, const char *filename) {
|
||||||
|
lua_pushfstring(L, "%s: %s", filename, strerror(errno));
|
||||||
|
luaL_argerror(L, arg, lua_tostring(L, -1));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static FILE **topfile (lua_State *L) {
|
static FILE **topfile (lua_State *L) {
|
||||||
FILE **f = (FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE);
|
FILE **f = (FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE);
|
||||||
if (f == NULL) luaL_argerror(L, 1, "bad file");
|
if (f == NULL) luaL_argerror(L, 1, "bad file");
|
||||||
@@ -156,10 +162,8 @@ static int g_iofile (lua_State *L, int f, const char *mode) {
|
|||||||
if (filename) {
|
if (filename) {
|
||||||
FILE **pf = newfile(L);
|
FILE **pf = newfile(L);
|
||||||
*pf = fopen(filename, mode);
|
*pf = fopen(filename, mode);
|
||||||
if (*pf == NULL) {
|
if (*pf == NULL)
|
||||||
lua_pushfstring(L, "%s: %s", filename, strerror(errno));
|
fileerror(L, 1, filename);
|
||||||
luaL_argerror(L, 1, lua_tostring(L, -1));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
tofile(L); /* check that it's a valid file handle */
|
tofile(L); /* check that it's a valid file handle */
|
||||||
@@ -212,7 +216,8 @@ static int io_lines (lua_State *L) {
|
|||||||
const char *filename = luaL_checkstring(L, 1);
|
const char *filename = luaL_checkstring(L, 1);
|
||||||
FILE **pf = newfile(L);
|
FILE **pf = newfile(L);
|
||||||
*pf = fopen(filename, "r");
|
*pf = fopen(filename, "r");
|
||||||
luaL_argcheck(L, *pf, 1, strerror(errno));
|
if (*pf == NULL)
|
||||||
|
fileerror(L, 1, filename);
|
||||||
aux_lines(L, lua_gettop(L), 1);
|
aux_lines(L, lua_gettop(L), 1);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user