Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2. ** $Id: lzio.h,v 1.22 2009/05/18 17:26:25 roberto Exp $
  3. ** Buffered streams
  4. ** See Copyright Notice in lua.h
  5. */
  6.  
  7.  
  8. #ifndef lzio_h
  9. #define lzio_h
  10.  
  11. #include "lua.h"
  12.  
  13. #include "lmem.h"
  14.  
  15.  
  16. #define EOZ     (-1)                    /* end of stream */
  17.  
  18. typedef struct Zio ZIO;
  19.  
  20. #define char2int(c)     cast(int, cast(unsigned char, (c)))
  21.  
  22. #define zgetc(z)  (((z)->n--)>0 ?  char2int(*(z)->p++) : luaZ_fill(z))
  23.  
  24. #define zungetc(z)      ((z)->n++, (z)->p--)
  25.  
  26.  
  27. typedef struct Mbuffer {
  28.   char *buffer;
  29.   size_t n;
  30.   size_t buffsize;
  31. } Mbuffer;
  32.  
  33. #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0)
  34.  
  35. #define luaZ_buffer(buff)       ((buff)->buffer)
  36. #define luaZ_sizebuffer(buff)   ((buff)->buffsize)
  37. #define luaZ_bufflen(buff)      ((buff)->n)
  38.  
  39. #define luaZ_resetbuffer(buff) ((buff)->n = 0)
  40.  
  41.  
  42. #define luaZ_resizebuffer(L, buff, size) \
  43.         (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \
  44.         (buff)->buffsize = size)
  45.  
  46. #define luaZ_freebuffer(L, buff)        luaZ_resizebuffer(L, buff, 0)
  47.  
  48.  
  49. LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n);
  50. LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader,
  51.                                         void *data);
  52. LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */
  53. LUAI_FUNC int luaZ_lookahead (ZIO *z);
  54.  
  55.  
  56.  
  57. /* --------- Private Part ------------------ */
  58.  
  59. struct Zio {
  60.   size_t n;                     /* bytes still unread */
  61.   const char *p;                /* current position in buffer */
  62.   lua_Reader reader;
  63.   void* data;                   /* additional data */
  64.   lua_State *L;                 /* Lua state (for reader) */
  65. };
  66.  
  67.  
  68. LUAI_FUNC int luaZ_fill (ZIO *z);
  69.  
  70. #endif
  71.