lua/lzio.c

Go to the documentation of this file.
00001 /*
00002 ** $Id: lzio.c,v 1.1 2004/03/16 21:58:30 niemeyer Exp $
00003 ** a generic input stream interface
00004 ** See Copyright Notice in lua.h
00005 */
00006 
00007 
00008 #include <string.h>
00009 
00010 #define lzio_c
00011 
00012 #include "lua.h"
00013 
00014 #include "llimits.h"
00015 #include "lmem.h"
00016 #include "lzio.h"
00017 
00018 
00019 int luaZ_fill (ZIO *z) {
00020   size_t size;
00021   const char *buff = z->reader(NULL, z->data, &size);
00022   if (buff == NULL || size == 0) return EOZ;
00023   z->n = size - 1;
00024   z->p = buff;
00025   return char2int(*(z->p++));
00026 }
00027 
00028 
00029 int luaZ_lookahead (ZIO *z) {
00030   if (z->n == 0) {
00031     int c = luaZ_fill(z);
00032     if (c == EOZ) return c;
00033     z->n++;
00034     z->p--;
00035   }
00036   return char2int(*z->p);
00037 }
00038 
00039 
00040 void luaZ_init (ZIO *z, lua_Chunkreader reader, void *data, const char *name) {
00041   z->reader = reader;
00042   z->data = data;
00043   z->name = name;
00044   z->n = 0;
00045   z->p = NULL;
00046 }
00047 
00048 
00049 /* --------------------------------------------------------------- read --- */
00050 size_t luaZ_read (ZIO *z, void *b, size_t n) {
00051   while (n) {
00052     size_t m;
00053     if (z->n == 0) {
00054       if (luaZ_fill(z) == EOZ)
00055         return n;  /* return number of missing bytes */
00056       else {
00057         ++z->n;  /* filbuf removed first byte; put back it */
00058         --z->p;
00059       }
00060     }
00061     m = (n <= z->n) ? n : z->n;  /* min. between n and z->n */
00062     memcpy(b, z->p, m);
00063     z->n -= m;
00064     z->p += m;
00065     b = (char *)b + m;
00066     n -= m;
00067   }
00068   return 0;
00069 }
00070 
00071 /* ------------------------------------------------------------------------ */
00072 char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) {
00073   if (n > buff->buffsize) {
00074     if (n < LUA_MINBUFFER) n = LUA_MINBUFFER;
00075     luaM_reallocvector(L, buff->buffer, buff->buffsize, n, char);
00076     buff->buffsize = n;
00077   }
00078   return buff->buffer;
00079 }
00080 
00081 

Generated on Fri Oct 12 08:44:54 2007 for rpm by  doxygen 1.5.2