diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-02-21 11:39:50 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-02-21 11:39:50 -0300 |
commit | 5715de344fbb32763995621913cbb3b0455a1fc4 (patch) | |
tree | e121d0c6acc7facbf9f2e03f938e45adf744f3a4 /liolib.c | |
parent | c86b9da02276652a22426b2a5a194ba1f070fcf4 (diff) | |
download | lua-5715de344fbb32763995621913cbb3b0455a1fc4.tar.gz lua-5715de344fbb32763995621913cbb3b0455a1fc4.tar.bz2 lua-5715de344fbb32763995621913cbb3b0455a1fc4.zip |
'read_all' does not need to grow buffer, as 'luaL_prepbuffsize'
already does that
Diffstat (limited to 'liolib.c')
-rw-r--r-- | liolib.c | 17 |
1 files changed, 6 insertions, 11 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: liolib.c,v 2.114 2013/06/07 19:01:35 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 2.115 2014/01/27 13:28:45 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 | */ |
@@ -403,20 +403,15 @@ static int read_line (lua_State *L, FILE *f, int chop) { | |||
403 | } | 403 | } |
404 | 404 | ||
405 | 405 | ||
406 | #define MAX_SIZE_T (~(size_t)0) | ||
407 | |||
408 | static void read_all (lua_State *L, FILE *f) { | 406 | static void read_all (lua_State *L, FILE *f) { |
409 | size_t rlen = LUAL_BUFFERSIZE; /* how much to read in each cycle */ | 407 | size_t nr; |
410 | luaL_Buffer b; | 408 | luaL_Buffer b; |
411 | luaL_buffinit(L, &b); | 409 | luaL_buffinit(L, &b); |
412 | for (;;) { | 410 | do { /* read file in chunks of LUAL_BUFFERSIZE bytes */ |
413 | char *p = luaL_prepbuffsize(&b, rlen); | 411 | char *p = luaL_prepbuffsize(&b, LUAL_BUFFERSIZE); |
414 | size_t nr = fread(p, sizeof(char), rlen, f); | 412 | nr = fread(p, sizeof(char), LUAL_BUFFERSIZE, f); |
415 | luaL_addsize(&b, nr); | 413 | luaL_addsize(&b, nr); |
416 | if (nr < rlen) break; /* eof? */ | 414 | } while (nr == LUAL_BUFFERSIZE); |
417 | else if (rlen <= (MAX_SIZE_T / 4)) /* avoid buffers too large */ | ||
418 | rlen *= 2; /* double buffer size at each iteration */ | ||
419 | } | ||
420 | luaL_pushresult(&b); /* close buffer */ | 415 | luaL_pushresult(&b); /* close buffer */ |
421 | } | 416 | } |
422 | 417 | ||