lua/lzio.c

Go to the documentation of this file.
00001 /*
00002 ** $Id: lzio.c,v 1.34 2011/07/15 12:35:32 roberto 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 #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)
00029     return EOZ;
00030   z->n = size - 1;  /* discount char being returned */
00031   z->p = buff;
00032   return cast_uchar(*(z->p++));
00033 }
00034 
00035 
00036 void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
00037   z->L = L;
00038   z->reader = reader;
00039   z->data = data;
00040   z->n = 0;
00041   z->p = NULL;
00042 }
00043 
00044 
00045 /* --------------------------------------------------------------- read --- */
00046 size_t luaZ_read (ZIO *z, void *b, size_t n) {
00047   while (n) {
00048     size_t m;
00049     if (z->n == 0) {  /* no bytes in buffer? */
00050       if (luaZ_fill(z) == EOZ)  /* try to read more */
00051         return n;  /* no more input; return number of missing bytes */
00052       else {
00053         z->n++;  /* luaZ_fill consumed first byte; put it back */
00054         z->p--;
00055       }
00056     }
00057     m = (n <= z->n) ? n : z->n;  /* min. between n and z->n */
00058     memcpy(b, z->p, m);
00059     z->n -= m;
00060     z->p += m;
00061     b = (char *)b + m;
00062     n -= m;
00063   }
00064   return 0;
00065 }
00066 
00067 /* ------------------------------------------------------------------------ */
00068 char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) {
00069   if (n > buff->buffsize) {
00070     if (n < LUA_MINBUFFER) n = LUA_MINBUFFER;
00071     luaZ_resizebuffer(L, buff, n);
00072   }
00073   return buff->buffer;
00074 }
00075 
00076 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:05 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs