summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/lzio.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua/lzio.c')
-rw-r--r--apps/plugins/lua/lzio.c34
1 files changed, 20 insertions, 14 deletions
diff --git a/apps/plugins/lua/lzio.c b/apps/plugins/lua/lzio.c
index 20efea9..293edd5 100644
--- a/apps/plugins/lua/lzio.c
+++ b/apps/plugins/lua/lzio.c
@@ -1,6 +1,6 @@
/*
-** $Id: lzio.c,v 1.35.1.1 2013/04/12 18:48:47 roberto Exp $
-** Buffered streams
+** $Id: lzio.c,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $
+** a generic input stream interface
** See Copyright Notice in lua.h
*/
@@ -25,11 +25,23 @@ 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; /* discount char being returned */
+ if (buff == NULL || size == 0) return EOZ;
+ z->n = size - 1;
z->p = buff;
- return cast_uchar(*(z->p++));
+ 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);
}
@@ -46,14 +58,8 @@ 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 (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--;
- }
- }
+ if (luaZ_lookahead(z) == EOZ)
+ return n; /* return number of missing bytes */
m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
memcpy(b, z->p, m);
z->n -= m;