lzio.h 1.6 KB

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