rlm@1: /* rlm@1: ** $Id: lzio.c,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $ rlm@1: ** a generic input stream interface rlm@1: ** See Copyright Notice in lua.h rlm@1: */ rlm@1: rlm@1: rlm@1: #include rlm@1: rlm@1: #define lzio_c rlm@1: #define LUA_CORE rlm@1: rlm@1: #include "lua.h" rlm@1: rlm@1: #include "llimits.h" rlm@1: #include "lmem.h" rlm@1: #include "lstate.h" rlm@1: #include "lzio.h" rlm@1: rlm@1: rlm@1: int luaZ_fill (ZIO *z) { rlm@1: size_t size; rlm@1: lua_State *L = z->L; rlm@1: const char *buff; rlm@1: lua_unlock(L); rlm@1: buff = z->reader(L, z->data, &size); rlm@1: lua_lock(L); rlm@1: if (buff == NULL || size == 0) return EOZ; rlm@1: z->n = size - 1; rlm@1: z->p = buff; rlm@1: return char2int(*(z->p++)); rlm@1: } rlm@1: rlm@1: rlm@1: int luaZ_lookahead (ZIO *z) { rlm@1: if (z->n == 0) { rlm@1: if (luaZ_fill(z) == EOZ) rlm@1: return EOZ; rlm@1: else { rlm@1: z->n++; /* luaZ_fill removed first byte; put back it */ rlm@1: z->p--; rlm@1: } rlm@1: } rlm@1: return char2int(*z->p); rlm@1: } rlm@1: rlm@1: rlm@1: void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { rlm@1: z->L = L; rlm@1: z->reader = reader; rlm@1: z->data = data; rlm@1: z->n = 0; rlm@1: z->p = NULL; rlm@1: } rlm@1: rlm@1: rlm@1: /* --------------------------------------------------------------- read --- */ rlm@1: size_t luaZ_read (ZIO *z, void *b, size_t n) { rlm@1: while (n) { rlm@1: size_t m; rlm@1: if (luaZ_lookahead(z) == EOZ) rlm@1: return n; /* return number of missing bytes */ rlm@1: m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ rlm@1: memcpy(b, z->p, m); rlm@1: z->n -= m; rlm@1: z->p += m; rlm@1: b = (char *)b + m; rlm@1: n -= m; rlm@1: } rlm@1: return 0; rlm@1: } rlm@1: rlm@1: /* ------------------------------------------------------------------------ */ rlm@1: char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) { rlm@1: if (n > buff->buffsize) { rlm@1: if (n < LUA_MINBUFFER) n = LUA_MINBUFFER; rlm@1: luaZ_resizebuffer(L, buff, n); rlm@1: } rlm@1: return buff->buffer; rlm@1: } rlm@1: rlm@1: