lzio.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. ** $Id: lzio.h,v 1.18 2003/08/28 14:38:46 roberto Exp roberto $
  3. ** Buffered streams
  4. ** See Copyright Notice in lua.h
  5. */
  6. #ifndef lzio_h
  7. #define lzio_h
  8. #include "lua.h"
  9. #include "lmem.h"
  10. #define EOZ (-1) /* end of stream */
  11. typedef struct Zio ZIO;
  12. #define char2int(c) cast(int, cast(unsigned char, (c)))
  13. #define zgetc(z) (((z)->n--)>0 ? char2int(*(z)->p++) : luaZ_fill(z))
  14. void luaZ_init (lua_State *L, ZIO *z, lua_Chunkreader reader, void *data);
  15. size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */
  16. int luaZ_lookahead (ZIO *z);
  17. typedef struct Mbuffer {
  18. char *buffer;
  19. size_t n;
  20. size_t buffsize;
  21. } Mbuffer;
  22. char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n);
  23. #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0)
  24. #define luaZ_buffer(buff) ((buff)->buffer)
  25. #define luaZ_sizebuffer(buff) ((buff)->buffsize)
  26. #define luaZ_bufflen(buff) ((buff)->n)
  27. #define luaZ_resetbuffer(buff) ((buff)->n = 0)
  28. #define luaZ_resizebuffer(L, buff, size) \
  29. (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \
  30. (buff)->buffsize = size)
  31. #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0)
  32. /* --------- Private Part ------------------ */
  33. struct Zio {
  34. size_t n; /* bytes still unread */
  35. const char *p; /* current position in buffer */
  36. lua_Chunkreader reader;
  37. void* data; /* additional data */
  38. lua_State *L; /* Lua state (for reader) */
  39. };
  40. int luaZ_fill (ZIO *z);
  41. #endif