annotate lib/lua/lua-5.2.2/lzio.c @ 1904:1a1c4040bee4

m
author Ritor1
date Mon, 21 Oct 2013 17:23:59 +0600
parents 41cc4dd3c122
children
rev   line source
1866
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
2 ** $Id: lzio.c,v 1.35 2012/05/14 13:34:18 roberto Exp $
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
3 ** Buffered streams
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
4 ** See Copyright Notice in lua.h
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
5 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
6
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
7
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
8 #include <string.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
9
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
10 #define lzio_c
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
11 #define LUA_CORE
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
12
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
13 #include "lua.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
14
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
15 #include "llimits.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
16 #include "lmem.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
17 #include "lstate.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
18 #include "lzio.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
19
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
20
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
21 int luaZ_fill (ZIO *z) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
22 size_t size;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
23 lua_State *L = z->L;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
24 const char *buff;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
25 lua_unlock(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
26 buff = z->reader(L, z->data, &size);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
27 lua_lock(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
28 if (buff == NULL || size == 0)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
29 return EOZ;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
30 z->n = size - 1; /* discount char being returned */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
31 z->p = buff;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
32 return cast_uchar(*(z->p++));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
33 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
34
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
35
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
36 void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
37 z->L = L;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
38 z->reader = reader;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
39 z->data = data;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
40 z->n = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
41 z->p = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
42 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
43
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
44
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
45 /* --------------------------------------------------------------- read --- */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
46 size_t luaZ_read (ZIO *z, void *b, size_t n) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
47 while (n) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
48 size_t m;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
49 if (z->n == 0) { /* no bytes in buffer? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
50 if (luaZ_fill(z) == EOZ) /* try to read more */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
51 return n; /* no more input; return number of missing bytes */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
52 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
53 z->n++; /* luaZ_fill consumed first byte; put it back */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
54 z->p--;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
55 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
56 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
57 m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
58 memcpy(b, z->p, m);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
59 z->n -= m;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
60 z->p += m;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
61 b = (char *)b + m;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
62 n -= m;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
63 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
64 return 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
65 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
66
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
67 /* ------------------------------------------------------------------------ */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
68 char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
69 if (n > buff->buffsize) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
70 if (n < LUA_MINBUFFER) n = LUA_MINBUFFER;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
71 luaZ_resizebuffer(L, buff, n);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
72 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
73 return buff->buffer;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
74 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
75
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
76