The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
lzio.cpp
Go to the documentation of this file.
1 /*
2 ** Buffered streams
3 ** See Copyright Notice in lua.h
4 */
5 
6 
7 #include <string.h>
8 
9 #define lzio_c
10 #define LUA_CORE
11 
12 #include "lua.h"
13 
14 #include "llimits.h"
15 #include "lmem.h"
16 #include "lstate.h"
17 #include "lzio.h"
18 
19 
20 int luaZ_fill (ZIO *z) {
21  size_t size;
22  lua_State *L = z->L;
23  const char *buff;
24  lua_unlock(L);
25  buff = z->reader(L, z->data, &size);
26  lua_lock(L);
27  if (buff == NULL || size == 0)
28  return EOZ;
29  z->n = size - 1; /* discount char being returned */
30  z->p = buff;
31  return cast_uchar(*(z->p++));
32 }
33 
34 
35 void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
36  z->L = L;
37  z->reader = reader;
38  z->data = data;
39  z->n = 0;
40  z->p = NULL;
41 }
42 
43 
44 /* --------------------------------------------------------------- read --- */
45 size_t luaZ_read (ZIO *z, void *b, size_t n) {
46  while (n) {
47  size_t m;
48  if (z->n == 0) { /* no bytes in buffer? */
49  if (luaZ_fill(z) == EOZ) /* try to read more */
50  return n; /* no more input; return number of missing bytes */
51  else {
52  z->n++; /* luaZ_fill consumed first byte; put it back */
53  z->p--;
54  }
55  }
56  m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
57  memcpy(b, z->p, m);
58  z->n -= m;
59  z->p += m;
60  b = (char *)b + m;
61  n -= m;
62  }
63  return 0;
64 }
65 
66 /* ------------------------------------------------------------------------ */
67 char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) {
68  if (n > buff->buffsize) {
69  if (n < LUA_MINBUFFER) n = LUA_MINBUFFER;
70  luaZ_resizebuffer(L, buff, n);
71  }
72  return buff->buffer;
73 }
74 
75 
GLdouble GLdouble z
Definition: glew.h:1527
const char * p
Definition: lzio.h:55
void * data
Definition: lzio.h:57
lua_Reader reader
Definition: lzio.h:56
lua_State * L
Definition: lzio.h:58
const char *(* lua_Reader)(lua_State *L, void *ud, size_t *sz)
Definition: lua.h:61
#define LUA_MINBUFFER
Definition: llimits.h:149
#define lua_unlock(L)
Definition: llimits.h:155
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
#define luaZ_resizebuffer(L, buff, size)
Definition: lzio.h:37
int luaZ_fill(ZIO *z)
Definition: lzio.cpp:20
GLdouble GLdouble GLdouble b
Definition: glew.h:6966
#define lua_lock(L)
Definition: llimits.h:154
void luaZ_init(lua_State *L, ZIO *z, lua_Reader reader, void *data)
Definition: lzio.cpp:35
#define cast_uchar(i)
Definition: llimits.h:97
Definition: lzio.h:53
char * buffer
Definition: lzio.h:23
static const char * reader(lua_State *L, void *ud, size_t *size)
Definition: luac.cpp:118
GLsizeiptr size
Definition: glew.h:1649
size_t luaZ_read(ZIO *z, void *b, size_t n)
Definition: lzio.cpp:45
GLclampd n
Definition: glew.h:5903
const GLdouble * m
Definition: glew.h:6968
#define EOZ
Definition: lzio.h:15
char * luaZ_openspace(lua_State *L, Mbuffer *buff, size_t n)
Definition: lzio.cpp:67
Definition: lzio.h:22
size_t n
Definition: lzio.h:54
size_t buffsize
Definition: lzio.h:25