Пример #1
0
/*
    z:read(
        {number | '*l' | '*a'}*
    )
*/
static int lz_test_eof(lua_State *L, lz_stream *s) {
    lua_pushlstring(L, NULL, 0);
    if (s->o_buffer_len > 0) {
        return 1;
    } else if (s->eos) {
        return 0;
    } else {
        return lzstream_inflate_block(L, s);
    }
}
Пример #2
0
static int lz_read_chars(lua_State *L, lz_stream *s, size_t n) {
    luaL_Buffer b;
    luaL_buffinit(L, &b);

    if (s->o_buffer_len > 0 || !s->eos) do {
        size_t rlen = lzstream_flush_buffer(L, s, n, &b);
        n -= rlen;
    } while (n > 0 && lzstream_inflate_block(L, s));

    luaL_pushresult(&b);
    return n == 0 || lua_strlen(L, -1) > 0;
}
Пример #3
0
static int lz_read_line(lua_State *L, lz_stream *s) {
    luaL_Buffer b;
    size_t l = 0, n;

    luaL_buffinit(L, &b);

    if (s->o_buffer_len > 0 || !s->eos) do {
        char *p = s->o_buffer;
        size_t len = s->o_buffer_len;

        /* find newline in output buffer */
        for (n = 0; n < len; ++n, ++p) {
            if (*p == '\n' || *p == '\r') {
                int eat_nl = *p == '\r';
                luaL_addlstring(&b, s->o_buffer, n);
                lzstream_remove(s, n+1);
                l += n;

                if (eat_nl && lzstream_inflate_block(L, s)) {
                    if (s->o_buffer_len > 0 && *s->o_buffer == '\n') {
                        lzstream_remove(s, 1);
                    }
                }

                luaL_pushresult(&b);
                return 1;
            }
        }

        if (len > 0) {
            luaL_addlstring(&b, s->o_buffer, len);
            lzstream_remove(s, len);
            l += len;
        }
    } while (lzstream_inflate_block(L, s));

    luaL_pushresult(&b);
    return l > 0 || !s->eos || s->o_buffer_len > 0;
}