diff options
Diffstat (limited to 'lzio.c')
-rw-r--r-- | lzio.c | 56 |
1 files changed, 16 insertions, 40 deletions
@@ -1,69 +1,44 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lzio.c,v 1.15 2001/11/28 20:13:13 roberto Exp roberto $ | 2 | ** $Id: lzio.c,v 1.16 2002/04/29 12:37:41 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 | */ |
6 | 6 | ||
7 | 7 | ||
8 | |||
9 | #include <stdio.h> | ||
10 | #include <string.h> | 8 | #include <string.h> |
11 | 9 | ||
12 | #include "lua.h" | 10 | #include "lua.h" |
13 | 11 | ||
12 | #include "llimits.h" | ||
14 | #include "lzio.h" | 13 | #include "lzio.h" |
15 | 14 | ||
16 | 15 | ||
17 | 16 | ||
18 | /* ----------------------------------------------------- memory buffers --- */ | 17 | int luaZ_fill (ZIO *z) { |
19 | 18 | size_t size; | |
20 | static int zmfilbuf (ZIO* z) { | 19 | const char *buff = z->getblock(z->ud, &size); |
21 | (void)z; /* to avoid warnings */ | 20 | if (buff == NULL || size == 0) return EOZ; |
22 | return EOZ; | 21 | z->n = size - 1; |
23 | } | 22 | z->p = buff; |
24 | |||
25 | |||
26 | ZIO* zmopen (ZIO* z, const char* b, size_t size, const char *name) { | ||
27 | if (b==NULL) return NULL; | ||
28 | z->n = size; | ||
29 | z->p = (const unsigned char *)b; | ||
30 | z->filbuf = zmfilbuf; | ||
31 | z->u = NULL; | ||
32 | z->name = name; | ||
33 | return z; | ||
34 | } | ||
35 | |||
36 | |||
37 | /* -------------------------------------------------------------- FILEs --- */ | ||
38 | |||
39 | static int zffilbuf (ZIO* z) { | ||
40 | size_t n; | ||
41 | if (feof((FILE *)z->u)) return EOZ; | ||
42 | n = fread(z->buffer, 1, ZBSIZE, (FILE *)z->u); | ||
43 | if (n==0) return EOZ; | ||
44 | z->n = n-1; | ||
45 | z->p = z->buffer; | ||
46 | return *(z->p++); | 23 | return *(z->p++); |
47 | } | 24 | } |
48 | 25 | ||
49 | 26 | ||
50 | ZIO* zFopen (ZIO* z, FILE* f, const char *name) { | 27 | void luaZ_init (ZIO *z, lua_Getblock getblock, void *ud, const char *name) { |
51 | if (f==NULL) return NULL; | 28 | z->getblock = getblock; |
52 | z->n = 0; | 29 | z->ud = ud; |
53 | z->p = z->buffer; | ||
54 | z->filbuf = zffilbuf; | ||
55 | z->u = f; | ||
56 | z->name = name; | 30 | z->name = name; |
57 | return z; | 31 | z->n = 0; |
32 | z->p = NULL; | ||
58 | } | 33 | } |
59 | 34 | ||
60 | 35 | ||
61 | /* --------------------------------------------------------------- read --- */ | 36 | /* --------------------------------------------------------------- read --- */ |
62 | size_t zread (ZIO *z, void *b, size_t n) { | 37 | size_t luaZ_zread (ZIO *z, void *b, size_t n) { |
63 | while (n) { | 38 | while (n) { |
64 | size_t m; | 39 | size_t m; |
65 | if (z->n == 0) { | 40 | if (z->n == 0) { |
66 | if (z->filbuf(z) == EOZ) | 41 | if (luaZ_fill(z) == EOZ) |
67 | return n; /* return number of missing bytes */ | 42 | return n; /* return number of missing bytes */ |
68 | else { | 43 | else { |
69 | ++z->n; /* filbuf removed first byte; put back it */ | 44 | ++z->n; /* filbuf removed first byte; put back it */ |
@@ -79,3 +54,4 @@ size_t zread (ZIO *z, void *b, size_t n) { | |||
79 | } | 54 | } |
80 | return 0; | 55 | return 0; |
81 | } | 56 | } |
57 | |||