aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2010-11-09 14:57:49 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2010-11-09 14:57:49 -0200
commitfa1f72437858dfc1e756afd7f544b70c66cc3dd5 (patch)
tree0b6b2a75f1f68aebe3f67f486486f04e92aee57a
parent2a966116b1fe33e1ff18ec408b77311599633b52 (diff)
downloadlua-fa1f72437858dfc1e756afd7f544b70c66cc3dd5.tar.gz
lua-fa1f72437858dfc1e756afd7f544b70c66cc3dd5.tar.bz2
lua-fa1f72437858dfc1e756afd7f544b70c66cc3dd5.zip
when reading blocks with given size, try to read whole block
at once
-rw-r--r--liolib.c41
1 files changed, 24 insertions, 17 deletions
diff --git a/liolib.c b/liolib.c
index 6ae3aaac..6786201a 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 2.92 2010/10/25 19:01:37 roberto Exp roberto $ 2** $Id: liolib.c,v 2.93 2010/11/08 17:27:22 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*/
@@ -374,25 +374,32 @@ static int read_line (lua_State *L, FILE *f, int chop) {
374} 374}
375 375
376 376
377static int read_chars (lua_State *L, FILE *f, size_t n) { 377static void read_all (lua_State *L, FILE *f) {
378 size_t tbr = n; /* number of chars to be read */ 378 size_t rlen = LUAL_BUFFERSIZE; /* how much to read in each cycle */
379 size_t rlen; /* how much to read in each cycle */
380 size_t nr; /* number of chars actually read in each cycle */
381 luaL_Buffer b; 379 luaL_Buffer b;
382 luaL_buffinit(L, &b); 380 luaL_buffinit(L, &b);
383 rlen = LUAL_BUFFERSIZE / 2; /* to be doubled at 1st iteration */ 381 for (;;) {
384 do { 382 char *p = luaL_prepbuffsize(&b, rlen);
385 char *p; 383 size_t nr = fread(p, sizeof(char), rlen, f);
386 if (rlen < (MAX_SIZE_T / 2))
387 rlen *= 2; /* double buffer size at each iteration */
388 if (rlen > tbr) rlen = tbr; /* cannot read more than asked */
389 p = luaL_prepbuffsize(&b, rlen);
390 nr = fread(p, sizeof(char), rlen, f);
391 luaL_addsize(&b, nr); 384 luaL_addsize(&b, nr);
392 tbr -= nr; /* still have to read 'tbr' chars */ 385 if (nr < rlen) break; /* eof? */
393 } while (tbr > 0 && nr == rlen); /* until end of count or eof */ 386 else if (rlen <= (MAX_SIZE_T / 4)) /* avoid buffers too large */
387 rlen *= 2; /* double buffer size at each iteration */
388 }
389 luaL_pushresult(&b); /* close buffer */
390}
391
392
393static int read_chars (lua_State *L, FILE *f, size_t n) {
394 size_t nr; /* number of chars actually read */
395 char *p;
396 luaL_Buffer b;
397 luaL_buffinit(L, &b);
398 p = luaL_prepbuffsize(&b, n); /* prepare buffer to read whole block */
399 nr = fread(p, sizeof(char), n, f); /* try to read 'n' chars */
400 luaL_addsize(&b, nr);
394 luaL_pushresult(&b); /* close buffer */ 401 luaL_pushresult(&b); /* close buffer */
395 return (tbr < n); /* true iff read something */ 402 return (nr > 0); /* true iff read something */
396} 403}
397 404
398 405
@@ -427,7 +434,7 @@ static int g_read (lua_State *L, FILE *f, int first) {
427 success = read_line(L, f, 0); 434 success = read_line(L, f, 0);
428 break; 435 break;
429 case 'a': /* file */ 436 case 'a': /* file */
430 read_chars(L, f, MAX_SIZE_T); /* read MAX_SIZE_T chars */ 437 read_all(L, f); /* read entire file */
431 success = 1; /* always success */ 438 success = 1; /* always success */
432 break; 439 break;
433 default: 440 default: