aboutsummaryrefslogtreecommitdiff
path: root/lzio.c
diff options
context:
space:
mode:
Diffstat (limited to 'lzio.c')
-rw-r--r--lzio.c33
1 files changed, 11 insertions, 22 deletions
diff --git a/lzio.c b/lzio.c
index 24826f68..a2720333 100644
--- a/lzio.c
+++ b/lzio.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lzio.c,v 1.31 2005/06/03 20:15:29 roberto Exp roberto $ 2** $Id: lzio.c,v 1.32 2011/02/17 17:34:16 roberto Exp roberto $
3** a generic input stream interface 3** a generic input stream interface
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -22,40 +22,23 @@ int luaZ_fill (ZIO *z) {
22 size_t size; 22 size_t size;
23 lua_State *L = z->L; 23 lua_State *L = z->L;
24 const char *buff; 24 const char *buff;
25 if (z->eoz) return EOZ;
26 lua_unlock(L); 25 lua_unlock(L);
27 buff = z->reader(L, z->data, &size); 26 buff = z->reader(L, z->data, &size);
28 lua_lock(L); 27 lua_lock(L);
29 if (buff == NULL || size == 0) { 28 if (buff == NULL || size == 0)
30 z->eoz = 1; /* avoid calling reader function next time */
31 return EOZ; 29 return EOZ;
32 } 30 z->n = size - 1; /* discount char being returned */
33 z->n = size - 1;
34 z->p = buff; 31 z->p = buff;
35 return char2int(*(z->p++)); 32 return char2int(*(z->p++));
36} 33}
37 34
38 35
39int luaZ_lookahead (ZIO *z) {
40 if (z->n == 0) {
41 if (luaZ_fill(z) == EOZ)
42 return EOZ;
43 else {
44 z->n++; /* luaZ_fill removed first byte; put back it */
45 z->p--;
46 }
47 }
48 return char2int(*z->p);
49}
50
51
52void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { 36void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
53 z->L = L; 37 z->L = L;
54 z->reader = reader; 38 z->reader = reader;
55 z->data = data; 39 z->data = data;
56 z->n = 0; 40 z->n = 0;
57 z->p = NULL; 41 z->p = NULL;
58 z->eoz = 0;
59} 42}
60 43
61 44
@@ -63,8 +46,14 @@ void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
63size_t luaZ_read (ZIO *z, void *b, size_t n) { 46size_t luaZ_read (ZIO *z, void *b, size_t n) {
64 while (n) { 47 while (n) {
65 size_t m; 48 size_t m;
66 if (luaZ_lookahead(z) == EOZ) 49 if (z->n == 0) { /* no bytes in buffer? */
67 return n; /* return number of missing bytes */ 50 if (luaZ_fill(z) == EOZ) /* try to read more */
51 return n; /* no more input; return number of missing bytes */
52 else {
53 z->n++; /* luaZ_fill consumed first byte; put it back */
54 z->p--;
55 }
56 }
68 m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ 57 m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
69 memcpy(b, z->p, m); 58 memcpy(b, z->p, m);
70 z->n -= m; 59 z->n -= m;