annotate src/lua/lzio.c @ 17:75e5bb1e0aa1
going to now integrate the gb src tree since it has no dependencies
author |
Robert McIntyre <rlm@mit.edu> |
date |
Sat, 03 Mar 2012 11:44:47 -0600 |
parents |
27763b933818 |
children |
|
rev |
line source |
rlm@1
|
1 /*
|
rlm@1
|
2 ** $Id: lzio.c,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $
|
rlm@1
|
3 ** a generic input stream interface
|
rlm@1
|
4 ** See Copyright Notice in lua.h
|
rlm@1
|
5 */
|
rlm@1
|
6
|
rlm@1
|
7
|
rlm@1
|
8 #include <string.h>
|
rlm@1
|
9
|
rlm@1
|
10 #define lzio_c
|
rlm@1
|
11 #define LUA_CORE
|
rlm@1
|
12
|
rlm@1
|
13 #include "lua.h"
|
rlm@1
|
14
|
rlm@1
|
15 #include "llimits.h"
|
rlm@1
|
16 #include "lmem.h"
|
rlm@1
|
17 #include "lstate.h"
|
rlm@1
|
18 #include "lzio.h"
|
rlm@1
|
19
|
rlm@1
|
20
|
rlm@1
|
21 int luaZ_fill (ZIO *z) {
|
rlm@1
|
22 size_t size;
|
rlm@1
|
23 lua_State *L = z->L;
|
rlm@1
|
24 const char *buff;
|
rlm@1
|
25 lua_unlock(L);
|
rlm@1
|
26 buff = z->reader(L, z->data, &size);
|
rlm@1
|
27 lua_lock(L);
|
rlm@1
|
28 if (buff == NULL || size == 0) return EOZ;
|
rlm@1
|
29 z->n = size - 1;
|
rlm@1
|
30 z->p = buff;
|
rlm@1
|
31 return char2int(*(z->p++));
|
rlm@1
|
32 }
|
rlm@1
|
33
|
rlm@1
|
34
|
rlm@1
|
35 int luaZ_lookahead (ZIO *z) {
|
rlm@1
|
36 if (z->n == 0) {
|
rlm@1
|
37 if (luaZ_fill(z) == EOZ)
|
rlm@1
|
38 return EOZ;
|
rlm@1
|
39 else {
|
rlm@1
|
40 z->n++; /* luaZ_fill removed first byte; put back it */
|
rlm@1
|
41 z->p--;
|
rlm@1
|
42 }
|
rlm@1
|
43 }
|
rlm@1
|
44 return char2int(*z->p);
|
rlm@1
|
45 }
|
rlm@1
|
46
|
rlm@1
|
47
|
rlm@1
|
48 void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
|
rlm@1
|
49 z->L = L;
|
rlm@1
|
50 z->reader = reader;
|
rlm@1
|
51 z->data = data;
|
rlm@1
|
52 z->n = 0;
|
rlm@1
|
53 z->p = NULL;
|
rlm@1
|
54 }
|
rlm@1
|
55
|
rlm@1
|
56
|
rlm@1
|
57 /* --------------------------------------------------------------- read --- */
|
rlm@1
|
58 size_t luaZ_read (ZIO *z, void *b, size_t n) {
|
rlm@1
|
59 while (n) {
|
rlm@1
|
60 size_t m;
|
rlm@1
|
61 if (luaZ_lookahead(z) == EOZ)
|
rlm@1
|
62 return n; /* return number of missing bytes */
|
rlm@1
|
63 m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
|
rlm@1
|
64 memcpy(b, z->p, m);
|
rlm@1
|
65 z->n -= m;
|
rlm@1
|
66 z->p += m;
|
rlm@1
|
67 b = (char *)b + m;
|
rlm@1
|
68 n -= m;
|
rlm@1
|
69 }
|
rlm@1
|
70 return 0;
|
rlm@1
|
71 }
|
rlm@1
|
72
|
rlm@1
|
73 /* ------------------------------------------------------------------------ */
|
rlm@1
|
74 char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) {
|
rlm@1
|
75 if (n > buff->buffsize) {
|
rlm@1
|
76 if (n < LUA_MINBUFFER) n = LUA_MINBUFFER;
|
rlm@1
|
77 luaZ_resizebuffer(L, buff, n);
|
rlm@1
|
78 }
|
rlm@1
|
79 return buff->buffer;
|
rlm@1
|
80 }
|
rlm@1
|
81
|
rlm@1
|
82
|