1/*	$NetBSD$	*/
2
3/*
4** Id: lzio.h,v 1.21.1.1 2007/12/27 13:02:25 roberto Exp
5** Buffered streams
6** See Copyright Notice in lua.h
7*/
8
9
10#ifndef lzio_h
11#define lzio_h
12
13#include "lua.h"
14
15#include "lmem.h"
16
17
18#define EOZ	(-1)			/* end of stream */
19
20typedef struct Zio ZIO;
21
22#define char2int(c)	cast(int, cast(unsigned char, (c)))
23
24#define zgetc(z)  (((z)->n--)>0 ?  char2int(*(z)->p++) : luaZ_fill(z))
25
26typedef struct Mbuffer {
27  char *buffer;
28  size_t n;
29  size_t buffsize;
30} Mbuffer;
31
32#define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0)
33
34#define luaZ_buffer(buff)	((buff)->buffer)
35#define luaZ_sizebuffer(buff)	((buff)->buffsize)
36#define luaZ_bufflen(buff)	((buff)->n)
37
38#define luaZ_resetbuffer(buff) ((buff)->n = 0)
39
40
41#define luaZ_resizebuffer(L, buff, size) \
42	(luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \
43	(buff)->buffsize = size)
44
45#define luaZ_freebuffer(L, buff)	luaZ_resizebuffer(L, buff, 0)
46
47
48LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n);
49LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader,
50                                        void *data);
51LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n);	/* read next n bytes */
52LUAI_FUNC int luaZ_lookahead (ZIO *z);
53
54
55
56/* --------- Private Part ------------------ */
57
58struct Zio {
59  size_t n;			/* bytes still unread */
60  const char *p;		/* current position in buffer */
61  lua_Reader reader;
62  void* data;			/* additional data */
63  lua_State *L;			/* Lua state (for reader) */
64};
65
66
67LUAI_FUNC int luaZ_fill (ZIO *z);
68
69#endif
70