Last change
on this file since 1803 was
1803,
checked in by rgrieder, 16 years ago
|
added files for lua 5.1.3, lua 5.0.3, CEGUILua-0.6.1 and CEGUILua-0.5.0b
|
-
Property svn:eol-style set to
native
|
File size:
1.6 KB
|
Line | |
---|
1 | /* |
---|
2 | ** $Id: lzio.c,v 1.24 2003/03/20 16:00:56 roberto Exp $ |
---|
3 | ** a generic input stream interface |
---|
4 | ** See Copyright Notice in lua.h |
---|
5 | */ |
---|
6 | |
---|
7 | |
---|
8 | #include <string.h> |
---|
9 | |
---|
10 | #define lzio_c |
---|
11 | |
---|
12 | #include "lua.h" |
---|
13 | |
---|
14 | #include "llimits.h" |
---|
15 | #include "lmem.h" |
---|
16 | #include "lzio.h" |
---|
17 | |
---|
18 | |
---|
19 | int luaZ_fill (ZIO *z) { |
---|
20 | size_t size; |
---|
21 | const char *buff = z->reader(NULL, z->data, &size); |
---|
22 | if (buff == NULL || size == 0) return EOZ; |
---|
23 | z->n = size - 1; |
---|
24 | z->p = buff; |
---|
25 | return char2int(*(z->p++)); |
---|
26 | } |
---|
27 | |
---|
28 | |
---|
29 | int luaZ_lookahead (ZIO *z) { |
---|
30 | if (z->n == 0) { |
---|
31 | int c = luaZ_fill(z); |
---|
32 | if (c == EOZ) return c; |
---|
33 | z->n++; |
---|
34 | z->p--; |
---|
35 | } |
---|
36 | return char2int(*z->p); |
---|
37 | } |
---|
38 | |
---|
39 | |
---|
40 | void luaZ_init (ZIO *z, lua_Chunkreader reader, void *data, const char *name) { |
---|
41 | z->reader = reader; |
---|
42 | z->data = data; |
---|
43 | z->name = name; |
---|
44 | z->n = 0; |
---|
45 | z->p = NULL; |
---|
46 | } |
---|
47 | |
---|
48 | |
---|
49 | /* --------------------------------------------------------------- read --- */ |
---|
50 | size_t luaZ_read (ZIO *z, void *b, size_t n) { |
---|
51 | while (n) { |
---|
52 | size_t m; |
---|
53 | if (z->n == 0) { |
---|
54 | if (luaZ_fill(z) == EOZ) |
---|
55 | return n; /* return number of missing bytes */ |
---|
56 | else { |
---|
57 | ++z->n; /* filbuf removed first byte; put back it */ |
---|
58 | --z->p; |
---|
59 | } |
---|
60 | } |
---|
61 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ |
---|
62 | memcpy(b, z->p, m); |
---|
63 | z->n -= m; |
---|
64 | z->p += m; |
---|
65 | b = (char *)b + m; |
---|
66 | n -= m; |
---|
67 | } |
---|
68 | return 0; |
---|
69 | } |
---|
70 | |
---|
71 | /* ------------------------------------------------------------------------ */ |
---|
72 | char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) { |
---|
73 | if (n > buff->buffsize) { |
---|
74 | if (n < LUA_MINBUFFER) n = LUA_MINBUFFER; |
---|
75 | luaM_reallocvector(L, buff->buffer, buff->buffsize, n, char); |
---|
76 | buff->buffsize = n; |
---|
77 | } |
---|
78 | return buff->buffer; |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
Note: See
TracBrowser
for help on using the repository browser.