18e3e3a7aSWarner Losh /* 2*0495ed39SKyle Evans ** $Id: lzio.h $ 38e3e3a7aSWarner Losh ** Buffered streams 48e3e3a7aSWarner Losh ** See Copyright Notice in lua.h 58e3e3a7aSWarner Losh */ 68e3e3a7aSWarner Losh 78e3e3a7aSWarner Losh 88e3e3a7aSWarner Losh #ifndef lzio_h 98e3e3a7aSWarner Losh #define lzio_h 108e3e3a7aSWarner Losh 118e3e3a7aSWarner Losh #include "lua.h" 128e3e3a7aSWarner Losh 138e3e3a7aSWarner Losh #include "lmem.h" 148e3e3a7aSWarner Losh 158e3e3a7aSWarner Losh 168e3e3a7aSWarner Losh #define EOZ (-1) /* end of stream */ 178e3e3a7aSWarner Losh 188e3e3a7aSWarner Losh typedef struct Zio ZIO; 198e3e3a7aSWarner Losh 208e3e3a7aSWarner Losh #define zgetc(z) (((z)->n--)>0 ? cast_uchar(*(z)->p++) : luaZ_fill(z)) 218e3e3a7aSWarner Losh 228e3e3a7aSWarner Losh 238e3e3a7aSWarner Losh typedef struct Mbuffer { 248e3e3a7aSWarner Losh char *buffer; 258e3e3a7aSWarner Losh size_t n; 268e3e3a7aSWarner Losh size_t buffsize; 278e3e3a7aSWarner Losh } Mbuffer; 288e3e3a7aSWarner Losh 298e3e3a7aSWarner Losh #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 308e3e3a7aSWarner Losh 318e3e3a7aSWarner Losh #define luaZ_buffer(buff) ((buff)->buffer) 328e3e3a7aSWarner Losh #define luaZ_sizebuffer(buff) ((buff)->buffsize) 338e3e3a7aSWarner Losh #define luaZ_bufflen(buff) ((buff)->n) 348e3e3a7aSWarner Losh 358e3e3a7aSWarner Losh #define luaZ_buffremove(buff,i) ((buff)->n -= (i)) 368e3e3a7aSWarner Losh #define luaZ_resetbuffer(buff) ((buff)->n = 0) 378e3e3a7aSWarner Losh 388e3e3a7aSWarner Losh 398e3e3a7aSWarner Losh #define luaZ_resizebuffer(L, buff, size) \ 408e3e3a7aSWarner Losh ((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \ 418e3e3a7aSWarner Losh (buff)->buffsize, size), \ 428e3e3a7aSWarner Losh (buff)->buffsize = size) 438e3e3a7aSWarner Losh 448e3e3a7aSWarner Losh #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 458e3e3a7aSWarner Losh 468e3e3a7aSWarner Losh 478e3e3a7aSWarner Losh LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 488e3e3a7aSWarner Losh void *data); 498e3e3a7aSWarner Losh LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */ 508e3e3a7aSWarner Losh 518e3e3a7aSWarner Losh 528e3e3a7aSWarner Losh 538e3e3a7aSWarner Losh /* --------- Private Part ------------------ */ 548e3e3a7aSWarner Losh 558e3e3a7aSWarner Losh struct Zio { 568e3e3a7aSWarner Losh size_t n; /* bytes still unread */ 578e3e3a7aSWarner Losh const char *p; /* current position in buffer */ 588e3e3a7aSWarner Losh lua_Reader reader; /* reader function */ 598e3e3a7aSWarner Losh void *data; /* additional data */ 608e3e3a7aSWarner Losh lua_State *L; /* Lua state (for reader) */ 618e3e3a7aSWarner Losh }; 628e3e3a7aSWarner Losh 638e3e3a7aSWarner Losh 648e3e3a7aSWarner Losh LUAI_FUNC int luaZ_fill (ZIO *z); 658e3e3a7aSWarner Losh 668e3e3a7aSWarner Losh #endif 67