diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-08-09 11:35:59 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-08-09 11:35:59 -0300 |
commit | f87057690b3afa7812d0ef5251ce103902a37c96 (patch) | |
tree | a2720180713f0d26f303b55fff9ff28cd027a309 | |
parent | 9e6aa878c96485ba0658303c0da16adef56ba54c (diff) | |
download | lua-f87057690b3afa7812d0ef5251ce103902a37c96.tar.gz lua-f87057690b3afa7812d0ef5251ce103902a37c96.tar.bz2 lua-f87057690b3afa7812d0ef5251ce103902a37c96.zip |
`io.lines' also can give the file name in its error message
-rw-r--r-- | liolib.c | 17 |
1 files changed, 11 insertions, 6 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: liolib.c,v 2.54 2004/07/09 15:47:48 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 2.55 2004/07/09 16:01:38 roberto Exp roberto $ |
3 | ** Standard I/O (and system) library | 3 | ** Standard I/O (and system) library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -44,6 +44,12 @@ static int pushresult (lua_State *L, int i, const char *filename) { | |||
44 | } | 44 | } |
45 | 45 | ||
46 | 46 | ||
47 | static void fileerror (lua_State *L, int arg, const char *filename) { | ||
48 | lua_pushfstring(L, "%s: %s", filename, strerror(errno)); | ||
49 | luaL_argerror(L, arg, lua_tostring(L, -1)); | ||
50 | } | ||
51 | |||
52 | |||
47 | static FILE **topfile (lua_State *L) { | 53 | static FILE **topfile (lua_State *L) { |
48 | FILE **f = (FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE); | 54 | FILE **f = (FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE); |
49 | if (f == NULL) luaL_argerror(L, 1, "bad file"); | 55 | 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) { | |||
156 | if (filename) { | 162 | if (filename) { |
157 | FILE **pf = newfile(L); | 163 | FILE **pf = newfile(L); |
158 | *pf = fopen(filename, mode); | 164 | *pf = fopen(filename, mode); |
159 | if (*pf == NULL) { | 165 | if (*pf == NULL) |
160 | lua_pushfstring(L, "%s: %s", filename, strerror(errno)); | 166 | fileerror(L, 1, filename); |
161 | luaL_argerror(L, 1, lua_tostring(L, -1)); | ||
162 | } | ||
163 | } | 167 | } |
164 | else { | 168 | else { |
165 | tofile(L); /* check that it's a valid file handle */ | 169 | tofile(L); /* check that it's a valid file handle */ |
@@ -212,7 +216,8 @@ static int io_lines (lua_State *L) { | |||
212 | const char *filename = luaL_checkstring(L, 1); | 216 | const char *filename = luaL_checkstring(L, 1); |
213 | FILE **pf = newfile(L); | 217 | FILE **pf = newfile(L); |
214 | *pf = fopen(filename, "r"); | 218 | *pf = fopen(filename, "r"); |
215 | luaL_argcheck(L, *pf, 1, strerror(errno)); | 219 | if (*pf == NULL) |
220 | fileerror(L, 1, filename); | ||
216 | aux_lines(L, lua_gettop(L), 1); | 221 | aux_lines(L, lua_gettop(L), 1); |
217 | return 1; | 222 | return 1; |
218 | } | 223 | } |