diff src/lua/src/lzio.c @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/lua/src/lzio.c	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,82 @@
     1.4 +/*
     1.5 +** $Id: lzio.c,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $
     1.6 +** a generic input stream interface
     1.7 +** See Copyright Notice in lua.h
     1.8 +*/
     1.9 +
    1.10 +
    1.11 +#include <string.h>
    1.12 +
    1.13 +#define lzio_c
    1.14 +#define LUA_CORE
    1.15 +
    1.16 +#include "lua.h"
    1.17 +
    1.18 +#include "llimits.h"
    1.19 +#include "lmem.h"
    1.20 +#include "lstate.h"
    1.21 +#include "lzio.h"
    1.22 +
    1.23 +
    1.24 +int luaZ_fill (ZIO *z) {
    1.25 +  size_t size;
    1.26 +  lua_State *L = z->L;
    1.27 +  const char *buff;
    1.28 +  lua_unlock(L);
    1.29 +  buff = z->reader(L, z->data, &size);
    1.30 +  lua_lock(L);
    1.31 +  if (buff == NULL || size == 0) return EOZ;
    1.32 +  z->n = size - 1;
    1.33 +  z->p = buff;
    1.34 +  return char2int(*(z->p++));
    1.35 +}
    1.36 +
    1.37 +
    1.38 +int luaZ_lookahead (ZIO *z) {
    1.39 +  if (z->n == 0) {
    1.40 +    if (luaZ_fill(z) == EOZ)
    1.41 +      return EOZ;
    1.42 +    else {
    1.43 +      z->n++;  /* luaZ_fill removed first byte; put back it */
    1.44 +      z->p--;
    1.45 +    }
    1.46 +  }
    1.47 +  return char2int(*z->p);
    1.48 +}
    1.49 +
    1.50 +
    1.51 +void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
    1.52 +  z->L = L;
    1.53 +  z->reader = reader;
    1.54 +  z->data = data;
    1.55 +  z->n = 0;
    1.56 +  z->p = NULL;
    1.57 +}
    1.58 +
    1.59 +
    1.60 +/* --------------------------------------------------------------- read --- */
    1.61 +size_t luaZ_read (ZIO *z, void *b, size_t n) {
    1.62 +  while (n) {
    1.63 +    size_t m;
    1.64 +    if (luaZ_lookahead(z) == EOZ)
    1.65 +      return n;  /* return number of missing bytes */
    1.66 +    m = (n <= z->n) ? n : z->n;  /* min. between n and z->n */
    1.67 +    memcpy(b, z->p, m);
    1.68 +    z->n -= m;
    1.69 +    z->p += m;
    1.70 +    b = (char *)b + m;
    1.71 +    n -= m;
    1.72 +  }
    1.73 +  return 0;
    1.74 +}
    1.75 +
    1.76 +/* ------------------------------------------------------------------------ */
    1.77 +char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) {
    1.78 +  if (n > buff->buffsize) {
    1.79 +    if (n < LUA_MINBUFFER) n = LUA_MINBUFFER;
    1.80 +    luaZ_resizebuffer(L, buff, n);
    1.81 +  }
    1.82 +  return buff->buffer;
    1.83 +}
    1.84 +
    1.85 +