diff options
| -rw-r--r-- | lauxlib.c | 18 |
1 files changed, 9 insertions, 9 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lauxlib.c,v 1.211 2010/05/17 18:30:27 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.212 2010/05/18 17:21:24 roberto Exp roberto $ |
| 3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -476,26 +476,26 @@ LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { | |||
| 476 | */ | 476 | */ |
| 477 | 477 | ||
| 478 | typedef struct LoadF { | 478 | typedef struct LoadF { |
| 479 | int first; | 479 | int first; /* pre-read character */ |
| 480 | FILE *f; | 480 | FILE *f; /* file being read */ |
| 481 | char buff[LUAL_BUFFERSIZE]; | 481 | char buff[LUAL_BUFFERSIZE]; /* area for reading file */ |
| 482 | } LoadF; | 482 | } LoadF; |
| 483 | 483 | ||
| 484 | 484 | ||
| 485 | static const char *getF (lua_State *L, void *ud, size_t *size) { | 485 | static const char *getF (lua_State *L, void *ud, size_t *size) { |
| 486 | LoadF *lf = (LoadF *)ud; | 486 | LoadF *lf = (LoadF *)ud; |
| 487 | (void)L; | 487 | (void)L; |
| 488 | if (lf->first != EOF) { | 488 | if (lf->first != EOF) { /* first character not read yet? */ |
| 489 | lf->buff[0] = (char)lf->first; /* return it */ | ||
| 489 | *size = 1; | 490 | *size = 1; |
| 490 | lf->buff[0] = (char)lf->first; | 491 | lf->first = EOF; /* now it has been read */ |
| 491 | lf->first = EOF; | ||
| 492 | } | 492 | } |
| 493 | else { | 493 | else { /* read a block from file */ |
| 494 | /* 'fread' can return > 0 *and* set the EOF flag. If next call to | 494 | /* 'fread' can return > 0 *and* set the EOF flag. If next call to |
| 495 | 'getF' called 'fread', it might still wait for user input. | 495 | 'getF' called 'fread', it might still wait for user input. |
| 496 | The next check avoids this problem. */ | 496 | The next check avoids this problem. */ |
| 497 | if (feof(lf->f)) return NULL; | 497 | if (feof(lf->f)) return NULL; |
| 498 | *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); | 498 | *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); /* read block */ |
| 499 | } | 499 | } |
| 500 | return lf->buff; | 500 | return lf->buff; |
| 501 | } | 501 | } |
