root/lib/lua/lzio.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. luaZ_fill
  2. luaZ_lookahead
  3. luaZ_init
  4. luaZ_read
  5. luaZ_openspace

   1 /*
   2 ** $Id: lzio.c,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $
   3 ** a generic input stream interface
   4 ** See Copyright Notice in lua.h
   5 */
   6 
   7 
   8 #include <string.h>
   9 
  10 #define lzio_c
  11 #define LUA_CORE
  12 
  13 #include "lua.h"
  14 
  15 #include "llimits.h"
  16 #include "lmem.h"
  17 #include "lstate.h"
  18 #include "lzio.h"
  19 
  20 
  21 LUAI_FUNC int luaZ_fill (ZIO *z) {
  22   size_t size;
  23   lua_State *L = z->L;
  24   const char *buff;
  25   if (z->eoz) return EOZ;
  26   lua_unlock(L);
  27   buff = z->reader(L, z->data, &size);
  28   lua_lock(L);
  29   if (buff == NULL || size == 0) {
  30     z->eoz = 1;  /* avoid calling reader function next time */
  31     return EOZ;
  32   }
  33   z->n = size - 1;
  34   z->p = buff;
  35   return char2int(*(z->p++));
  36 }
  37 
  38 
  39 LUAI_FUNC int luaZ_lookahead (ZIO *z) {
  40   if (z->n == 0) {
  41     if (luaZ_fill(z) == EOZ)
  42       return EOZ;
  43     else {
  44       z->n++;  /* luaZ_fill removed first byte; put back it */
  45       z->p--;
  46     }
  47   }
  48   return char2int(*z->p);
  49 }
  50 
  51 
  52 LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
  53   z->L = L;
  54   z->reader = reader;
  55   z->data = data;
  56   z->n = 0;
  57   z->p = NULL;
  58   z->eoz = 0;
  59 }
  60 
  61 
  62 /* --------------------------------------------------------------- read --- */
  63 LUAI_FUNC size_t luaZ_read (ZIO *z, void *b, size_t n) {
  64   while (n) {
  65     size_t m;
  66     if (luaZ_lookahead(z) == EOZ)
  67       return n;  /* return number of missing bytes */
  68     m = (n <= z->n) ? n : z->n;  /* min. between n and z->n */
  69     memcpy(b, z->p, m);
  70     z->n -= m;
  71     z->p += m;
  72     b = (char *)b + m;
  73     n -= m;
  74   }
  75   return 0;
  76 }
  77 
  78 /* ------------------------------------------------------------------------ */
  79 LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) {
  80   if (n > buff->buffsize) {
  81     if (n < LUA_MINBUFFER) n = LUA_MINBUFFER;
  82     luaZ_resizebuffer(L, buff, n);
  83   }
  84   return buff->buffer;
  85 }
  86 
  87 

/* [<][>][^][v][top][bottom][index][help] */