misc/liblua/lzio.c
author nemo
Sun, 07 Nov 2010 12:37:29 -0500
changeset 4184 bc2b88eea8c4
parent 2812 0a24853de796
permissions -rw-r--r--
fix stray offset in resurrector, make resurrector use time
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     1
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     2
** $Id: lzio.c,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     3
** a generic input stream interface
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     4
** See Copyright Notice in lua.h
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     5
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     6
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     7
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     8
#include <string.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     9
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    10
#define lzio_c
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    11
#define LUA_CORE
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    12
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    13
#include "lua.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    14
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    15
#include "llimits.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    16
#include "lmem.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    17
#include "lstate.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    18
#include "lzio.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    19
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    20
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    21
int luaZ_fill (ZIO *z) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    22
  size_t size;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    23
  lua_State *L = z->L;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    24
  const char *buff;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    25
  lua_unlock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    26
  buff = z->reader(L, z->data, &size);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    27
  lua_lock(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    28
  if (buff == NULL || size == 0) return EOZ;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    29
  z->n = size - 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    30
  z->p = buff;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    31
  return char2int(*(z->p++));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    32
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    33
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    34
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    35
int luaZ_lookahead (ZIO *z) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    36
  if (z->n == 0) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    37
    if (luaZ_fill(z) == EOZ)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    38
      return EOZ;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    39
    else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    40
      z->n++;  /* luaZ_fill removed first byte; put back it */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    41
      z->p--;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    42
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    43
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    44
  return char2int(*z->p);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    45
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    46
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    47
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    48
void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    49
  z->L = L;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    50
  z->reader = reader;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    51
  z->data = data;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    52
  z->n = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    53
  z->p = NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    54
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    55
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    56
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    57
/* --------------------------------------------------------------- read --- */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    58
size_t luaZ_read (ZIO *z, void *b, size_t n) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    59
  while (n) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    60
    size_t m;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    61
    if (luaZ_lookahead(z) == EOZ)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    62
      return n;  /* return number of missing bytes */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    63
    m = (n <= z->n) ? n : z->n;  /* min. between n and z->n */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    64
    memcpy(b, z->p, m);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    65
    z->n -= m;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    66
    z->p += m;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    67
    b = (char *)b + m;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    68
    n -= m;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    69
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    70
  return 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    71
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    72
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    73
/* ------------------------------------------------------------------------ */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    74
char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    75
  if (n > buff->buffsize) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    76
    if (n < LUA_MINBUFFER) n = LUA_MINBUFFER;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    77
    luaZ_resizebuffer(L, buff, n);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    78
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    79
  return buff->buffer;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    80
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    81
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    82