static int lmpack_packer_pack(lua_State *L) { char *b; size_t bl; int result, argc; Packer *packer; luaL_Buffer buffer; if ((argc = lua_gettop(L)) != 2) return luaL_error(L, "expecting exactly 2 arguments"); packer = lmpack_check_packer(L, 1); packer->root = lmpack_ref(L, packer->reg); luaL_buffinit(L, &buffer); b = luaL_prepbuffer(&buffer); bl = LUAL_BUFFERSIZE; if (packer->packing) { return luaL_error(L, "Packer instance already working. Use another Packer " "or the module's \"pack\" function if you need to " "pack from the ext handler"); } do { size_t bl_init = bl; packer->packing = 1; result = mpack_unparse(packer->parser, &b, &bl, lmpack_unparse_enter, lmpack_unparse_exit); packer->packing = 0; if (result == MPACK_NOMEM) { packer->parser = lmpack_grow_parser(packer->parser); if (!packer->parser) { packer->packing = 0; return luaL_error(L, "Failed to grow Packer capacity"); } } luaL_addsize(&buffer, bl_init - bl); if (!bl) { /* buffer empty, resize */ b = luaL_prepbuffer(&buffer); bl = LUAL_BUFFERSIZE; } } while (result == MPACK_EOF || result == MPACK_NOMEM); lmpack_unref(L, packer->reg, packer->root); luaL_pushresult(&buffer); assert(lua_gettop(L) == argc); return 1; }
static int lmpack_pack(lua_State *L) { char *b; size_t bl; int result; Packer packer; mpack_parser_t parser; luaL_Buffer buffer; if (lua_gettop(L) != 1) return luaL_error(L, "expecting exactly 1 argument"); /* initialize packer */ lua_newtable(L); packer.reg = luaL_ref(L, LUA_REGISTRYINDEX); packer.ext = LUA_NOREF; packer.parser = &parser; mpack_parser_init(packer.parser, 0); packer.parser->data.p = &packer; packer.is_bin = 0; packer.L = L; packer.root = lmpack_ref(L, packer.reg); luaL_buffinit(L, &buffer); b = luaL_prepbuffer(&buffer); bl = LUAL_BUFFERSIZE; do { size_t bl_init = bl; result = mpack_unparse(packer.parser, &b, &bl, lmpack_unparse_enter, lmpack_unparse_exit); if (result == MPACK_NOMEM) { lmpack_unref(L, packer.reg, packer.root); luaL_unref(L, LUA_REGISTRYINDEX, packer.reg); return luaL_error(L, "object was too deep to pack"); } luaL_addsize(&buffer, bl_init - bl); if (!bl) { /* buffer empty, resize */ b = luaL_prepbuffer(&buffer); bl = LUAL_BUFFERSIZE; } } while (result == MPACK_EOF); lmpack_unref(L, packer.reg, packer.root); luaL_unref(L, LUA_REGISTRYINDEX, packer.reg); luaL_pushresult(&buffer); return 1; }
/* data [, errmsg] = socket:receive(size) */ static int cnt_receive (lua_State *L) { loski_NetDriver *drv = (loski_NetDriver *)lua_touserdata(L, lua_upvalueindex(1)); loski_Socket *socket = tosock(L, LOSKI_CONNSOCKET); luaL_Buffer lbuf; size_t sz, len; char *buf = newbuffer(L, 2, &lbuf, &sz); int res = loski_recvfromsocket(drv, socket, buf, sz, &len, NULL); if (res == 0) { luaL_addsize(&lbuf, len); luaL_pushresult(&lbuf); /* close buffer */ } return pushsockres(L, 1, res); }
/* Rockbox already defines read_line() */ static int _read_line (lua_State *L, FILE *f, int chop) { luaL_Buffer b; luaL_buffinit(L, &b); for (;;) { size_t l; size_t r; char *p = luaL_prepbuffer(&b); r = rb->read_line((int)f, p, LUAL_BUFFERSIZE); l = strlen(p); if (l == 0 || p[l-1] != '\n') luaL_addsize(&b, l); else { luaL_addsize(&b, l - chop); /* chop 'eol' if needed */ luaL_pushresult(&b); /* close buffer */ return 1; /* read at least an `eol' */ } if (r < LUAL_BUFFERSIZE) { /* eof? */ luaL_pushresult(&b); /* close buffer */ return (lua_rawlen(L, -1) > 0); /* check whether read something */ } } }
static int luaB__cgets (lua_State *L){ size_t nr; /* number of chars actually read */ char *p; luaL_Buffer b; luaL_buffinit(L, &b); p = luaL_prepbuffsize(&b, 255); /* prepare buffer to read whole block */ kgets(p, 255); nr = sizeof(p); /* try to read 'n' chars */ luaL_addsize(&b, nr); luaL_pushresult(&b); /* close buffer */ return (nr > 0); /* true iff read something */ }
static int lmz_inflator_deflator_impl(lua_State* L, lmz_stream_t* stream) { mz_streamp miniz_stream = &(stream->stream); size_t data_size; const char* data = luaL_checklstring(L, 2, &data_size); int flush = luaL_checkoption(L, 3, "no", flush_types); miniz_stream->avail_in = data_size; miniz_stream->next_in = (const unsigned char*)data; luaL_Buffer buf; luaL_buffinit(L, &buf); while (1) { char* buffer = luaL_prepbuffer(&buf); memset(buffer, 0, LUAL_BUFFERSIZE); miniz_stream->avail_out = LUAL_BUFFERSIZE; miniz_stream->next_out = (unsigned char*)buffer; size_t before = miniz_stream->total_out; int status; if (stream->mode) { status = mz_inflate(miniz_stream, flush); } else { status = mz_deflate(miniz_stream, flush); } size_t added = miniz_stream->total_out - before; luaL_addsize(&buf, added); switch (status) { case MZ_OK: case MZ_STREAM_END: luaL_pushresult(&buf); return 1; case MZ_STREAM_ERROR: case MZ_DATA_ERROR: case MZ_PARAM_ERROR: luaL_pushresult(&buf); lua_pushnil(L); lua_insert(L, -2); lua_pushstring(L, mz_error(status)); lua_insert(L, -2); return 3; case MZ_BUF_ERROR: if (stream->mode) { // not enough input luaL_pushresult(&buf); lua_pushnil(L); lua_insert(L, -2); lua_pushstring(L, "Not enough input data"); lua_insert(L, -2); return 3; } break; } } }
/* Binding to libbzip2's BZ2_bzReadOpen method */ static int lbz_read(lua_State *L) { int bzerror = BZ_OK; int len; luaL_Buffer b; lbz_state *state = lbz_check_state(L, 1); len = luaL_checkint(L, 2); if (!state->bz_stream && !state->buf) { /* The logical end of file has been reached -- there's no more data to * return, and the user should call the read_close method. */ lua_pushnil(L); lua_pushstring(L, "CLOSED"); return 2; } luaL_buffinit(L, &b); /* In case this function is being used alongsize the getline method, we * should use the buffers that getline is using */ if (state->buf_size) { int used_len = (state->buf_size < len) ? state->buf_size : len; luaL_addlstring(&b, state->buf, used_len); lbz_buffer_drain(state, used_len); len -= used_len; } /* Pull in chunks until all data read */ while(len > 0) { char *buf = luaL_prepbuffer(&b); int nextRead = len > LUAL_BUFFERSIZE ? LUAL_BUFFERSIZE : len; int read = BZ2_bzRead(&bzerror, state->bz_stream, buf, nextRead); if (read > 0) { luaL_addsize(&b, read); len -= read; } if (bzerror != BZ_OK) goto handle_error; } luaL_pushresult(&b); return 1; handle_error: if(BZ_STREAM_END == bzerror) { /* Push the data read already and mark the stream done */ luaL_pushresult(&b); lbz_perform_close(state, 0); return 1; } else { lua_pushnil(L); lua_pushstring(L, BZ2_bzerror(state->bz_stream, &bzerror)); return 2; } }
static void read_all(lua_State *L, FILE *f) { size_t rlen = LUAL_BUFFERSIZE; /* how much to read in each cycle */ luaL_Buffer b; luaL_buffinit(L, &b); for (;;) { char *p = luaL_prepbuffsize(&b, rlen); size_t nr = fread(p, sizeof(char), rlen, f); luaL_addsize(&b, nr); if (nr < rlen) break; /* eof? */ else if (rlen <= (MAX_SIZE_T / 4)) /* avoid buffers too large */ rlen *= 2; /* double buffer size at each iteration */ } luaL_pushresult(&b); /* close buffer */ }
/* ** Pack integer 'n' with 'size' bytes and 'islittle' endianness. ** The final 'if' handles the case when 'size' is larger than ** the size of a Lua integer, correcting the extra sign-extension ** bytes if necessary (by default they would be zeros). */ static void packint (luaL_Buffer *b, lua_Unsigned n, int islittle, int size, int neg) { char *buff = luaL_prepbuffsize(b, size); int i; buff[islittle ? 0 : size - 1] = (char)(n & MC); /* first byte */ for (i = 1; i < size; i++) { n >>= NB; buff[islittle ? i : size - 1 - i] = (char)(n & MC); } if (neg && size > SZINT) { /* negative number need sign extension? */ for (i = SZINT; i < size; i++) /* correct extra bytes */ buff[islittle ? i : size - 1 - i] = (char)MC; } luaL_addsize(b, size); /* add result to buffer */ }
/* data [, errmsg] = socket:receive(size [, getfrom]) */ static int dgm_receive (lua_State *L) { loski_NetDriver *drv = (loski_NetDriver *)lua_touserdata(L, lua_upvalueindex(1)); loski_Socket *socket = tosock(L, LOSKI_DGRMSOCKET); luaL_Buffer lbuf; size_t sz, len; char *buf = newbuffer(L, 2, &lbuf, &sz); int res, nr = 1; if (lua_toboolean(L, 3)) { loski_Address address; res = loski_recvfromsocket(drv, socket, buf, sz, &len, &address); if (res == 0) { luaL_addsize(&lbuf, len); luaL_pushresult(&lbuf); /* close buffer */ nr += pushaddress(drv, L, &address); } } else { res = loski_recvfromsocket(drv, socket, buf, sz, &len, NULL); if (res == 0) { luaL_addsize(&lbuf, len); luaL_pushresult(&lbuf); /* close buffer */ } } return pushsockres(L, nr, res); }
static int read_line(lua_State* L, FILE* f) { luaL_Buffer b; luaL_buffinit(L, &b); for (;;) { size_t l; char* p = luaL_prepbuffer(&b); if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) /* eof? */ { luaL_pushresult(&b); /* close buffer */ return (lua_objlen(L, -1) > 0); /* check whether read something */ } l = strlen(p); if (l == 0 || p[l - 1] != '\n') luaL_addsize(&b, l); else { luaL_addsize(&b, l - 1); /* do not include `eol' */ luaL_pushresult(&b); /* close buffer */ return 1; /* read at least an `eol' */ } } }
static int read_line (lua_State *L, ZZIP_FILE *f) { luaL_Buffer b; luaL_buffinit(L, &b); for (;;) { size_t l; char *p = luaL_prepbuffer(&b); if (zzip_fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */ luaL_pushresult(&b); /* close buffer */ #if LUA_VERSION_NUM < 501 return (lua_strlen(L, -1) > 0); /* check whether read something */ #else return (lua_rawlen(L, -1) > 0); /* check whether read something */ #endif } l = strlen(p); if (p[l-1] != '\n') luaL_addsize(&b, l); else { luaL_addsize(&b, l - 1); /* do not include `eol' */ luaL_pushresult(&b); /* close buffer */ return 1; /* read at least an `eol' */ } } }
int LuaIOLib::read_chars (lua_State *lua, FileHandle *file, size_t n) { auto process = proc(lua); auto &vfs = process->vfs(); //size_t nr; /* number of chars actually read */ char *p; luaL_Buffer b; luaL_buffinit(lua, &b); p = luaL_prepbuffsize(&b, n); /* prepare buffer to read whole block */ auto read_result = file->read(n, (uint8_t*)p); //nr = fread(p, sizeof(char), n, file); /* try to read 'n' chars */ luaL_addsize(&b, read_result.bytes()); luaL_pushresult(&b); /* close buffer */ return (read_result.bytes() > 0); /* true iff read something */ }
static int read_chars (lua_State *L, FILE *f, size_t n) { size_t rlen; /* how much to read */ size_t nr; /* number of chars actually read */ luaL_Buffer b; luaL_buffinit(L, &b); rlen = LUAL_BUFFERSIZE; /* try to read that much each time */ do { char *p = luaL_prepbuffer(&b); if (rlen > n) rlen = n; /* cannot read more than asked */ nr = fread(p, sizeof(char), rlen, f); luaL_addsize(&b, nr); n -= nr; /* still have to read `n' chars */ } while (n > 0 && nr == rlen); /* until end of count or eof */ luaL_pushresult(&b); /* close buffer */ return (n == 0 || lua_strlen(L, -1) > 0); }
static int execute(lua_State *L, struct filter *flt) { z_stream *stream = flt->stream; size_t avail_in; const char * source = lua_tolstring(L, 1, &avail_in); uint8_t chunk[CHUNK_IN]; int flags = Z_NO_FLUSH; stream->avail_in = 0; stream->avail_out = 0; luaL_Buffer buff; luaL_buffinit(L, &buff); int result; do { if (stream->avail_in == 0 && flags != Z_FINISH) { stream->next_in = (z_const Bytef *)chunk; if (avail_in <= CHUNK_IN) { flags = Z_FINISH; stream->avail_in = avail_in; } else { stream->avail_in = CHUNK_IN; } if (flt->crypt_type == CRYPT_IN) { rc4_crypt(flt->crypt_sbox, (const uint8_t *)source, chunk, stream->avail_in); stream->next_in = (z_const Bytef *)chunk; } else { stream->next_in = (z_const Bytef *)source; } source += stream->avail_in; avail_in -= stream->avail_in; } void *buffer = luaL_prepbuffer(&buff); stream->next_out = buffer; stream->avail_out = LUAL_BUFFERSIZE; result = flt->execute(stream, flags); lz_assert(L, result, flt); if (flt->crypt_type == CRYPT_OUT) { rc4_crypt(flt->crypt_sbox, buffer, buffer, LUAL_BUFFERSIZE - stream->avail_out); } luaL_addsize(&buff, LUAL_BUFFERSIZE - stream->avail_out); } while ( result != Z_STREAM_END ); luaL_pushresult(&buff); lua_pushinteger(L, stream->adler); flt->end(stream); return 2; }
static int openssl_xalgor_tostring(lua_State* L) { int type; void* val; ASN1_OBJECT *obj; X509_ALGOR* alg = CHECK_OBJECT(1, X509_ALGOR, "openssl.x509_algor"); X509_ALGOR_get0(&obj, &type, &val, alg); if (obj != NULL) { luaL_Buffer B; luaL_buffinit(L, &B); luaL_addsize(&B, OBJ_obj2txt(luaL_prepbuffer(&B), LUAL_BUFFERSIZE, obj, 0)); luaL_pushresult(&B); return 1; } return 0; }
// Lua: r = ow.read_bytes( id, size ) static int ow_read_bytes( lua_State *L ) { unsigned id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( ow, id ); u32 size = ( u32 )luaL_checkinteger( L, 2 ); if( size == 0 ) return 0; luaL_Buffer b; luaL_buffinit( L, &b ); char *p = luaL_prepbuffer(&b); onewire_read_bytes(id, (uint8_t *)p, size); luaL_addsize(&b, size); luaL_pushresult( &b ); return 1; }
static int os_date (lua_State *L) { const char *s = luaL_optstring(L, 1, "%c"); time_t t = luaL_opt(L, l_checktime, 2, time(NULL)); struct tm tmr, *stm; if (*s == '!') { /* UTC? */ stm = l_gmtime(&t, &tmr); s++; /* skip '!' */ } else stm = l_localtime(&t, &tmr); if (stm == NULL) /* invalid date? */ luaL_error(L, "time result cannot be represented in this installation"); if (strcmp(s, "*t") == 0) { lua_createtable(L, 0, 9); /* 9 = number of fields */ setfield(L, "sec", stm->tm_sec); setfield(L, "min", stm->tm_min); setfield(L, "hour", stm->tm_hour); setfield(L, "day", stm->tm_mday); setfield(L, "month", stm->tm_mon+1); setfield(L, "year", stm->tm_year+1900); setfield(L, "wday", stm->tm_wday+1); setfield(L, "yday", stm->tm_yday+1); setboolfield(L, "isdst", stm->tm_isdst); } else { char cc[4]; luaL_Buffer b; cc[0] = '%'; luaL_buffinit(L, &b); while (*s) { if (*s != '%') /* not a conversion specifier? */ luaL_addchar(&b, *s++); else { size_t reslen; char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT); s = checkoption(L, s + 1, cc); reslen = strftime(buff, SIZETIMEFMT, cc, stm); luaL_addsize(&b, reslen); } } luaL_pushresult(&b); } return 1; }
void LuaIOLib::read_all (lua_State *lua, FileHandle *file) { auto process = proc(lua); auto &vfs = process->vfs(); size_t rlen = LUAL_BUFFERSIZE; /* how much to read in each cycle */ luaL_Buffer b; luaL_buffinit(lua, &b); for (;;) { char *p = luaL_prepbuffsize(&b, rlen); //size_t nr = fread(p, sizeof(char), rlen, file); auto read_result = file->read(rlen, (uint8_t*)p); luaL_addsize(&b, read_result.bytes()); if (read_result.bytes() < rlen) break; /* eof? */ else if (rlen <= (MAX_SIZE_T / 4)) /* avoid buffers too large */ rlen *= 2; /* double buffer size at each iteration */ } luaL_pushresult(&b); /* close buffer */ }
// Lua: r = ow.search( id ) static int ow_search( lua_State *L ) { unsigned id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( ow, id ); luaL_Buffer b; luaL_buffinit( L, &b ); char *p = luaL_prepbuffer(&b); if(onewire_search(id, (uint8_t *)p)){ luaL_addsize(&b, 8); luaL_pushresult( &b ); } else { luaL_pushresult(&b); /* close buffer */ lua_pop(L,1); lua_pushnil(L); } return 1; }
static int read_line (lua_State *L, FILE *f, int chop) { luaL_Buffer b; int c = '\0'; luaL_buffinit(L, &b); while (c != EOF && c != '\n') { /* repeat until end of line */ char *buff = luaL_prepbuffer(&b); /* preallocate buffer */ int i = 0; l_lockfile(f); /* no memory errors can happen inside the lock */ while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n') buff[i++] = c; l_unlockfile(f); luaL_addsize(&b, i); } if (!chop && c == '\n') /* want a newline and have one? */ luaL_addchar(&b, c); /* add ending newline to result */ luaL_pushresult(&b); /* close buffer */ /* return ok if read something (either a newline or something else) */ return (c == '\n' || lua_rawlen(L, -1) > 0); }
static int read_chars(lua_State *L, FILE *f, size_t n) { size_t rlen; size_t nr; luaL_Buffer b; luaL_buffinit(L, &b); rlen = LUAL_BUFFERSIZE; do { char *p = luaL_prepbuffer(&b); if (rlen > n) rlen = n; nr = fread(p, sizeof(char), rlen, f); luaL_addsize(&b, nr); n -= nr; } while (n > 0 && nr == rlen); luaL_pushresult(&b); return (n == 0 || lua_strlen(L, -1) > 0); }
/** * Reads a file into a string, or a directory into a list of files therein. * - Arg 1: string containing the file name. * - Ret 1: string */ int intf_read_file(lua_State *L) { std::string p = luaL_checkstring(L, 1); if(!resolve_filename(p, get_calling_file(L))) { return luaL_argerror(L, -1, "file not found"); } if(filesystem::is_directory(p)) { std::vector<std::string> files, dirs; filesystem::get_files_in_dir(p, &files, &dirs); filesystem::default_blacklist.remove_blacklisted_files_and_dirs(files, dirs); std::size_t ndirs = dirs.size(); std::copy(files.begin(), files.end(), std::back_inserter(dirs)); lua_push(L, dirs); lua_pushnumber(L, ndirs); lua_setfield(L, -2, "ndirs"); return 1; } const std::unique_ptr<std::istream> fs(filesystem::istream_file(p)); fs->exceptions(std::ios_base::goodbit); std::size_t size = 0; fs->seekg(0, std::ios::end); if(!fs->good()) { return luaL_error(L, "Error when reading file"); } size = fs->tellg(); fs->seekg(0, std::ios::beg); if(!fs->good()) { return luaL_error(L, "Error when reading file"); } luaL_Buffer b; luaL_buffinit(L, &b); //throws an exception if malloc failed. char* out = luaL_prepbuffsize(&b, size); fs->read(out, size); if(fs->good()) { luaL_addsize(&b, size); } luaL_pushresult(&b); return 1; }
/** * Checks if a file exists (not necessarily a Lua script). * - Arg 1: string containing the file name. * - Ret 1: string */ int intf_read_file(lua_State *L) { std::string m = luaL_checkstring(L, 1); std::string current_dir = ""; lua_Debug ar; if(lua_getstack(L, 1, &ar)) { lua_getinfo(L, "S", &ar); if(ar.source[0] == '@') { current_dir = filesystem::directory_name(std::string(ar.source + 1)); } } if(!resolve_filename(m, current_dir)) { return luaL_argerror(L, -1, "file not found"); } std::string p = filesystem::get_wml_location(m); if(p.empty()) { return luaL_argerror(L, -1, "file not found"); } boost::scoped_ptr<std::istream> fs(filesystem::istream_file(p)); fs->exceptions(std::ios_base::goodbit); size_t size = 0; fs->seekg(0, std::ios::end); if(!fs->good()) { return luaL_error(L, "Error when reading file"); } size = fs->tellg(); fs->seekg(0, std::ios::beg); if(!fs->good()) { return luaL_error(L, "Error when reading file"); } luaL_Buffer b; luaL_buffinit(L, &b); //throws an exception if malloc failed. char* out = luaL_prepbuffsize(&b, size); fs->read(out, size); if(fs->good()) { luaL_addsize(&b, size); } luaL_pushresult(&b); return 1; }
// Lua: r = ow.read_bytes( id, size ) static int ow_read_bytes( lua_State *L ) { unsigned id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( ow, id ); u32 size = ( u32 )luaL_checkinteger( L, 2 ); if( size == 0 ) return 0; luaL_argcheck(L, size <= LUAL_BUFFERSIZE, 2, "Attempt to read too many characters"); luaL_Buffer b; luaL_buffinit( L, &b ); char *p = luaL_prepbuffer(&b); onewire_read_bytes(id, (uint8_t *)p, size); luaL_addsize(&b, size); luaL_pushresult( &b ); return 1; }
static int os_date (lua_State *L) { size_t slen; const char *s = luaL_optlstring(L, 1, "%c", &slen); time_t t = luaL_opt(L, l_checktime, 2, time(NULL)); const char *se = s + slen; /* 's' end */ struct tm tmr, *stm; if (*s == '!') { /* UTC? */ stm = l_gmtime(&t, &tmr); s++; /* skip '!' */ } else stm = l_localtime(&t, &tmr); if (stm == NULL) /* invalid date? */ return luaL_error(L, "time result cannot be represented in this installation"); if (strcmp(s, "*t") == 0) { lua_createtable(L, 0, 9); /* 9 = number of fields */ setallfields(L, stm); } else { char cc[4]; /* buffer for individual conversion specifiers */ luaL_Buffer b; cc[0] = '%'; luaL_buffinit(L, &b); while (s < se) { if (*s != '%') /* not a conversion specifier? */ luaL_addchar(&b, *s++); else { size_t reslen; char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT); s++; /* skip '%' */ s = checkoption(L, s, se - s, cc + 1); /* copy specifier to 'cc' */ reslen = strftime(buff, SIZETIMEFMT, cc, stm); luaL_addsize(&b, reslen); } } luaL_pushresult(&b); } return 1; }
static void read_all (lua_State *L, FILE *f) { size_t rlen = LUAL_BUFFERSIZE; /* how much to read in each cycle */ l_seeknum old, nrlen = 0; /* for testing file size */ luaL_Buffer b; luaL_buffinit(L, &b); /* speed up loading of not too large files: */ old = l_ftell(f); if ((l_fseek(f, 0, SEEK_END) >= 0) && ((nrlen = l_ftell(f)) > 0) && nrlen < 1000 * 1000 * 100) { rlen = nrlen; } l_fseek(f, old, SEEK_SET); for (;;) { char *p = luaL_prepbuffsize(&b, rlen); size_t nr = fread(p, sizeof(char), rlen, f); luaL_addsize(&b, nr); if (nr < rlen) break; /* eof? */ else if (rlen <= (MAX_SIZE_T / 4)) /* avoid buffers too large */ rlen *= 2; /* double buffer size at each iteration */ } luaL_pushresult(&b); /* close buffer */ }
static int read_line (lua_State *L, FILE *f, int chop) { luaL_Buffer b; int c = '\0'; luaL_buffinit(L, &b); while (c != EOF && c != '\n') { /* repeat until end of line */ char *buff = luaL_prepbuffer(&b); /* preallocate buffer */ int i = 0; l_lockfile(f); /* no memory errors can happen inside the lock */ #ifdef U8W_H if (f == stdin) { char *pb = u8fgets(buff, LUAL_BUFFERSIZE, f); if (pb != NULL) { char *pbn = strchr(buff, '\n'); if (pbn != NULL) { *pbn = '\0'; c = '\n'; } i = (int)strlen(pb); } else { c = EOF; } } else { #endif while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n') buff[i++] = c; #ifdef U8W_H } #endif l_unlockfile(f); luaL_addsize(&b, i); } if (!chop && c == '\n') /* want a newline and have one? */ luaL_addchar(&b, c); /* add ending newline to result */ luaL_pushresult(&b); /* close buffer */ /* return ok if read something (either a newline or something else) */ return (c == '\n' || lua_rawlen(L, -1) > 0); }
// g_read() static int file_g_read( lua_State* L, int n, int16_t end_char ) { if(n< 0 || n>LUAL_BUFFERSIZE) n = LUAL_BUFFERSIZE; if(end_char < 0 || end_char >255) end_char = EOF; int ec = (int)end_char; luaL_Buffer b; if((FS_OPEN_OK - 1)==file_fd) return luaL_error(L, "open a file first"); luaL_buffinit(L, &b); char *p = luaL_prepbuffer(&b); int c = EOF; int i = 0; do{ c = fs_getc(file_fd); if(c==EOF){ break; } p[i++] = (char)(0xFF & c); }while((c!=EOF) && (c!=ec) && (i<n) ); #if 0 if(i>0 && p[i-1] == '\n') i--; /* do not include `eol' */ #endif if(i==0){ luaL_pushresult(&b); /* close buffer */ return (lua_objlen(L, -1) > 0); /* check whether read something */ } luaL_addsize(&b, i); luaL_pushresult(&b); /* close buffer */ return 1; /* read at least an `eol' */ }
static int lmz_compress(lua_State *L, int start, lmz_Comp *c, int flush) { size_t len, offset = 0, output = 0; const char *s = luaL_checklstring(L, start, &len); luaL_Buffer b; luaL_buffinit(L, &b); for (;;) { size_t in_size = len - offset; size_t out_size = LUAL_BUFFERSIZE; tdefl_status status = tdefl_compress(c, s + offset, &in_size, (mz_uint8*)luaL_prepbuffer(&b), &out_size, flush); offset += in_size; output += out_size; luaL_addsize(&b, out_size); if (offset == len || status == TDEFL_STATUS_DONE) { luaL_pushresult(&b); lua_pushboolean(L, status == TDEFL_STATUS_DONE); lua_pushinteger(L, len); lua_pushinteger(L, output); return 4; } else if (status != TDEFL_STATUS_OKAY) luaL_error(L, "compress failure (%d)", status); } }