summaryrefslogtreecommitdiffstats
path: root/lua/lzio.c
diff options
context:
space:
mode:
Diffstat (limited to 'lua/lzio.c')
-rw-r--r--lua/lzio.c32
1 files changed, 13 insertions, 19 deletions
diff --git a/lua/lzio.c b/lua/lzio.c
index 293edd5..354f94e 100644
--- a/lua/lzio.c
+++ b/lua/lzio.c
@@ -1,5 +1,5 @@
/*
-** $Id: lzio.c,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $
+** $Id: lzio.c,v 1.34 2011/07/15 12:35:32 roberto Exp $
** a generic input stream interface
** See Copyright Notice in lua.h
*/
@@ -25,23 +25,11 @@ int luaZ_fill (ZIO *z) {
lua_unlock(L);
buff = z->reader(L, z->data, &size);
lua_lock(L);
- if (buff == NULL || size == 0) return EOZ;
- z->n = size - 1;
+ if (buff == NULL || size == 0)
+ return EOZ;
+ z->n = size - 1; /* discount char being returned */
z->p = buff;
- return char2int(*(z->p++));
-}
-
-
-int luaZ_lookahead (ZIO *z) {
- if (z->n == 0) {
- if (luaZ_fill(z) == EOZ)
- return EOZ;
- else {
- z->n++; /* luaZ_fill removed first byte; put back it */
- z->p--;
- }
- }
- return char2int(*z->p);
+ return cast_uchar(*(z->p++));
}
@@ -58,8 +46,14 @@ void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
size_t luaZ_read (ZIO *z, void *b, size_t n) {
while (n) {
size_t m;
- if (luaZ_lookahead(z) == EOZ)
- return n; /* return number of missing bytes */
+ if (z->n == 0) { /* no bytes in buffer? */
+ if (luaZ_fill(z) == EOZ) /* try to read more */
+ return n; /* no more input; return number of missing bytes */
+ else {
+ z->n++; /* luaZ_fill consumed first byte; put it back */
+ z->p--;
+ }
+ }
m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
memcpy(b, z->p, m);
z->n -= m;