lzio.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. ** $Id: lzio.h,v 1.21.1.1 2007/12/27 13:02:25 roberto Exp $
  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. typedef struct Mbuffer {
  15. char *buffer;
  16. size_t n;
  17. size_t buffsize;
  18. } Mbuffer;
  19. #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->n = 0, (buff)->buffsize = 0)
  20. #define luaZ_buffer(buff) ((buff)->buffer)
  21. #define luaZ_sizebuffer(buff) ((buff)->buffsize)
  22. #define luaZ_bufflen(buff) ((buff)->n)
  23. #define luaZ_resetbuffer(buff) ((buff)->n = 0)
  24. #define luaZ_resizebuffer(L, buff, size) \
  25. (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \
  26. (buff)->buffsize = size)
  27. #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0)
  28. #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0)
  29. #define luaZ_get_base_address(zio) ((const char *)((zio)->reader(NULL, (zio)->data, NULL)))
  30. #define luaZ_direct_mode(zio) (luaZ_get_base_address(zio) != NULL)
  31. #define luaZ_get_crt_address(zio) (luaZ_get_base_address(zio) + (zio)->i)
  32. LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n);
  33. LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader,
  34. void *data);
  35. LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */
  36. LUAI_FUNC int luaZ_lookahead (ZIO *z);
  37. /* --------- Private Part ------------------ */
  38. struct Zio {
  39. size_t n; /* bytes still unread */
  40. size_t i; /* buffer offset */
  41. const char *p; /* current position in buffer */
  42. lua_Reader reader;
  43. void* data; /* additional data */
  44. lua_State *L; /* Lua state (for reader) */
  45. };
  46. LUAI_FUNC int luaZ_fill (ZIO *z);
  47. #endif