lzio.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #include <string.h>
00009
00010 #define lzio_c
00011 #define LUA_CORE
00012
00013 #include "lua.h"
00014
00015 #include "llimits.h"
00016 #include "lmem.h"
00017 #include "lstate.h"
00018 #include "lzio.h"
00019
00020
00021 int luaZ_fill (ZIO *z) {
00022 size_t size;
00023 lua_State *L = z->L;
00024 const char *buff;
00025 lua_unlock(L);
00026 buff = z->reader(L, z->data, &size);
00027 lua_lock(L);
00028 if (buff == NULL || size == 0) return EOZ;
00029 z->n = size - 1;
00030 z->p = buff;
00031 return char2int(*(z->p++));
00032 }
00033
00034
00035 int luaZ_lookahead (ZIO *z) {
00036 if (z->n == 0) {
00037 if (luaZ_fill(z) == EOZ)
00038 return EOZ;
00039 else {
00040 z->n++;
00041 z->p--;
00042 }
00043 }
00044 return char2int(*z->p);
00045 }
00046
00047
00048 void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
00049 z->L = L;
00050 z->reader = reader;
00051 z->data = data;
00052 z->n = 0;
00053 z->p = NULL;
00054 }
00055
00056
00057
00058 size_t luaZ_read (ZIO *z, void *b, size_t n) {
00059 while (n) {
00060 size_t m;
00061 if (luaZ_lookahead(z) == EOZ)
00062 return n;
00063 m = (n <= z->n) ? n : z->n;
00064 memcpy(b, z->p, m);
00065 z->n -= m;
00066 z->p += m;
00067 b = (char *)b + m;
00068 n -= m;
00069 }
00070 return 0;
00071 }
00072
00073
00074 char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) {
00075 if (n > buff->buffsize) {
00076 if (n < LUA_MINBUFFER) n = LUA_MINBUFFER;
00077 luaZ_resizebuffer(L, buff, n);
00078 }
00079 return buff->buffer;
00080 }