diff options
| -rw-r--r-- | liolib.c | 21 |
1 files changed, 11 insertions, 10 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: liolib.c,v 2.77 2007/12/08 11:54:32 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 2.78 2008/02/12 16:51:03 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 | */ |
| @@ -310,20 +310,21 @@ static int read_line (lua_State *L, FILE *f) { | |||
| 310 | 310 | ||
| 311 | 311 | ||
| 312 | static int read_chars (lua_State *L, FILE *f, size_t n) { | 312 | static int read_chars (lua_State *L, FILE *f, size_t n) { |
| 313 | size_t rlen; /* how much to read */ | 313 | size_t tbr = n; /* number of chars to be read */ |
| 314 | size_t nr; /* number of chars actually read */ | 314 | size_t rlen; /* how much to read in each cycle */ |
| 315 | size_t nr; /* number of chars actually read in each cycle */ | ||
| 315 | luaL_Buffer b; | 316 | luaL_Buffer b; |
| 316 | luaL_buffinit(L, &b); | 317 | luaL_buffinit(L, &b); |
| 317 | rlen = LUAL_BUFFERSIZE; /* try to read that much each time */ | 318 | rlen = LUAL_BUFFERSIZE; /* try to read that much each time */ |
| 318 | do { | 319 | do { |
| 319 | char *p = luaL_prepbuffer(&b); | 320 | char *p = luaL_prepbuffer(&b); |
| 320 | if (rlen > n) rlen = n; /* cannot read more than asked */ | 321 | if (rlen > tbr) rlen = tbr; /* cannot read more than asked */ |
| 321 | nr = fread(p, sizeof(char), rlen, f); | 322 | nr = fread(p, sizeof(char), rlen, f); |
| 322 | luaL_addsize(&b, nr); | 323 | luaL_addsize(&b, nr); |
| 323 | n -= nr; /* still have to read `n' chars */ | 324 | tbr -= nr; /* still have to read 'tbr' chars */ |
| 324 | } while (n > 0 && nr == rlen); /* until end of count or eof */ | 325 | } while (tbr > 0 && nr == rlen); /* until end of count or eof */ |
| 325 | luaL_pushresult(&b); /* close buffer */ | 326 | luaL_pushresult(&b); /* close buffer */ |
| 326 | return (lua_objlen(L, -1) > 0); | 327 | return (tbr < n); /* true iff read something */ |
| 327 | } | 328 | } |
| 328 | 329 | ||
| 329 | 330 | ||
| @@ -386,13 +387,13 @@ static int f_read (lua_State *L) { | |||
| 386 | 387 | ||
| 387 | static int io_readline (lua_State *L) { | 388 | static int io_readline (lua_State *L) { |
| 388 | FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(1)); | 389 | FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(1)); |
| 389 | int sucess; | 390 | int success; |
| 390 | if (f == NULL) /* file is already closed? */ | 391 | if (f == NULL) /* file is already closed? */ |
| 391 | luaL_error(L, "file is already closed"); | 392 | luaL_error(L, "file is already closed"); |
| 392 | sucess = read_line(L, f); | 393 | success = read_line(L, f); |
| 393 | if (ferror(f)) | 394 | if (ferror(f)) |
| 394 | return luaL_error(L, "%s", strerror(errno)); | 395 | return luaL_error(L, "%s", strerror(errno)); |
| 395 | if (sucess) return 1; | 396 | if (success) return 1; |
| 396 | else { /* EOF */ | 397 | else { /* EOF */ |
| 397 | if (lua_toboolean(L, lua_upvalueindex(2))) { /* generator created file? */ | 398 | if (lua_toboolean(L, lua_upvalueindex(2))) { /* generator created file? */ |
| 398 | lua_settop(L, 0); | 399 | lua_settop(L, 0); |
