1 /* $NetBSD: lzio.h,v 1.9 2023/04/16 20:46:17 nikita Exp $ */ 2 3 /* 4 ** Id: lzio.h 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 20 typedef struct Zio ZIO; 21 22 #define zgetc(z) (((z)->n--)>0 ? cast_uchar(*(z)->p++) : luaZ_fill(z)) 23 24 25 typedef struct Mbuffer { 26 char *buffer; 27 size_t n; 28 size_t buffsize; 29 } Mbuffer; 30 31 #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 32 33 #define luaZ_buffer(buff) ((buff)->buffer) 34 #define luaZ_sizebuffer(buff) ((buff)->buffsize) 35 #define luaZ_bufflen(buff) ((buff)->n) 36 37 #define luaZ_buffremove(buff,i) ((buff)->n -= (i)) 38 #define luaZ_resetbuffer(buff) ((buff)->n = 0) 39 40 41 #define luaZ_resizebuffer(L, buff, size) \ 42 ((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \ 43 (buff)->buffsize, size), \ 44 (buff)->buffsize = size) 45 46 #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 47 48 49 LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 50 void *data); 51 LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */ 52 53 54 55 /* --------- Private Part ------------------ */ 56 57 struct Zio { 58 size_t n; /* bytes still unread */ 59 const char *p; /* current position in buffer */ 60 lua_Reader reader; /* reader function */ 61 void *data; /* additional data */ 62 lua_State *L; /* Lua state (for reader) */ 63 }; 64 65 66 LUAI_FUNC int luaZ_fill (ZIO *z); 67 68 #endif 69