diff options
| author | Li Jin <dragon-fly@qq.com> | 2020-06-22 16:50:40 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2020-06-22 16:50:40 +0800 |
| commit | cd2b60b101a398cb9356d746364e70eaed1860f1 (patch) | |
| tree | a1fe71b76faabc4883f16905a94164ce5c23e692 /src/lua/lzio.c | |
| parent | 88c1052e700f38cf3d8ad82d469da4c487760b7e (diff) | |
| download | yuescript-cd2b60b101a398cb9356d746364e70eaed1860f1.tar.gz yuescript-cd2b60b101a398cb9356d746364e70eaed1860f1.tar.bz2 yuescript-cd2b60b101a398cb9356d746364e70eaed1860f1.zip | |
add support for local variable declared with attribute 'close' and 'const' for Lua 5.4.
Diffstat (limited to 'src/lua/lzio.c')
| -rw-r--r-- | src/lua/lzio.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/lua/lzio.c b/src/lua/lzio.c new file mode 100644 index 0000000..cd0a02d --- /dev/null +++ b/src/lua/lzio.c | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | /* | ||
| 2 | ** $Id: lzio.c $ | ||
| 3 | ** Buffered streams | ||
| 4 | ** See Copyright Notice in lua.h | ||
| 5 | */ | ||
| 6 | |||
| 7 | #define lzio_c | ||
| 8 | #define LUA_CORE | ||
| 9 | |||
| 10 | #include "lprefix.h" | ||
| 11 | |||
| 12 | |||
| 13 | #include <string.h> | ||
| 14 | |||
| 15 | #include "lua.h" | ||
| 16 | |||
| 17 | #include "llimits.h" | ||
| 18 | #include "lmem.h" | ||
| 19 | #include "lstate.h" | ||
| 20 | #include "lzio.h" | ||
| 21 | |||
| 22 | |||
| 23 | int luaZ_fill (ZIO *z) { | ||
| 24 | size_t size; | ||
| 25 | lua_State *L = z->L; | ||
| 26 | const char *buff; | ||
| 27 | lua_unlock(L); | ||
| 28 | buff = z->reader(L, z->data, &size); | ||
| 29 | lua_lock(L); | ||
| 30 | if (buff == NULL || size == 0) | ||
| 31 | return EOZ; | ||
| 32 | z->n = size - 1; /* discount char being returned */ | ||
| 33 | z->p = buff; | ||
| 34 | return cast_uchar(*(z->p++)); | ||
| 35 | } | ||
| 36 | |||
| 37 | |||
| 38 | void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { | ||
| 39 | z->L = L; | ||
| 40 | z->reader = reader; | ||
| 41 | z->data = data; | ||
| 42 | z->n = 0; | ||
| 43 | z->p = NULL; | ||
| 44 | } | ||
| 45 | |||
| 46 | |||
| 47 | /* --------------------------------------------------------------- read --- */ | ||
| 48 | size_t luaZ_read (ZIO *z, void *b, size_t n) { | ||
| 49 | while (n) { | ||
| 50 | size_t m; | ||
| 51 | if (z->n == 0) { /* no bytes in buffer? */ | ||
| 52 | if (luaZ_fill(z) == EOZ) /* try to read more */ | ||
| 53 | return n; /* no more input; return number of missing bytes */ | ||
| 54 | else { | ||
| 55 | z->n++; /* luaZ_fill consumed first byte; put it back */ | ||
| 56 | z->p--; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ | ||
| 60 | memcpy(b, z->p, m); | ||
| 61 | z->n -= m; | ||
| 62 | z->p += m; | ||
| 63 | b = (char *)b + m; | ||
| 64 | n -= m; | ||
| 65 | } | ||
| 66 | return 0; | ||
| 67 | } | ||
| 68 | |||
