aboutsummaryrefslogtreecommitdiff
path: root/liolib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2008-02-12 15:05:36 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2008-02-12 15:05:36 -0200
commitf86f4116c82654d24faa4a32a909d0c27f3d4746 (patch)
tree094b2ccb3cf4d77690f74cf0148d98dc15f3e89f /liolib.c
parent7f69f0efb07d54bfb9d91ed426d2774a703be5ab (diff)
downloadlua-f86f4116c82654d24faa4a32a909d0c27f3d4746.tar.gz
lua-f86f4116c82654d24faa4a32a909d0c27f3d4746.tar.bz2
lua-f86f4116c82654d24faa4a32a909d0c27f3d4746.zip
micro-optimization: avoid one API call in 'read_chars'
Diffstat (limited to 'liolib.c')
-rw-r--r--liolib.c21
1 files changed, 11 insertions, 10 deletions
diff --git a/liolib.c b/liolib.c
index 033282de..4895283f 100644
--- a/liolib.c
+++ b/liolib.c
@@ -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
312static int read_chars (lua_State *L, FILE *f, size_t n) { 312static 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
387static int io_readline (lua_State *L) { 388static 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);