|
| 1 | +#ifdef _MSC_VER |
| 2 | +# define _CRT_NONSTDC_NO_WARNINGS |
| 3 | +# define _CRT_SECURE_NO_WARNINGS |
| 4 | +#endif |
| 5 | + |
1 | 6 | #define LUA_LIB
|
2 | 7 | #include <lua.h>
|
3 | 8 | #include <lauxlib.h>
|
@@ -394,16 +399,15 @@ static int Lbuf_add(lua_State *L) {
|
394 | 399 |
|
395 | 400 | static int Lbuf_clear(lua_State *L) {
|
396 | 401 | pb_Buffer *buff = (pb_Buffer*)luaL_checkudata(L, 1, LPB_BUFTYPE);
|
397 |
| - lua_Integer n = luaL_optinteger(L, 2, buff->used); |
398 |
| - if (n > buff->used) n = buff->used; |
399 |
| - buff->used -= n; |
400 |
| - lua_pushlstring(L, &buff->buff[buff->used], n); |
| 402 | + size_t sz = (size_t)luaL_optinteger(L, 2, buff->used); |
| 403 | + if (sz > buff->used) sz = buff->used; |
| 404 | + buff->used -= sz; |
| 405 | + lua_pushlstring(L, &buff->buff[buff->used], sz); |
401 | 406 | return 1;
|
402 | 407 | }
|
403 | 408 |
|
404 | 409 | static int Lbuf_concat(lua_State *L) {
|
405 | 410 | pb_Buffer *buff = (pb_Buffer*)luaL_checkudata(L, 1, LPB_BUFTYPE);
|
406 |
| - pb_Buffer *other; |
407 | 411 | int i, top = lua_gettop(L);
|
408 | 412 | for (i = 2; i < top; ++i) {
|
409 | 413 | size_t len;
|
@@ -841,6 +845,79 @@ LUALIB_API int luaopen_pb_decoder(lua_State *L) {
|
841 | 845 | return 1;
|
842 | 846 | }
|
843 | 847 |
|
| 848 | +/* io routines */ |
| 849 | + |
| 850 | +#ifdef _WIN32 |
| 851 | +# include <io.h> |
| 852 | +# include <fcntl.h> |
| 853 | +#else |
| 854 | +# define setmode(a,b) ((void)0) |
| 855 | +#endif |
| 856 | + |
| 857 | +static int io_write(lua_State *L, FILE *f, int arg) { |
| 858 | + int nargs = lua_gettop(L) - arg; |
| 859 | + int status = 1; |
| 860 | + for (; nargs--; arg++) { |
| 861 | + size_t l; |
| 862 | + const char *s = pb_tolbuffer(L, arg, &l); |
| 863 | + status = status && (fwrite(s, sizeof(char), l, f) == l); |
| 864 | + } |
| 865 | + if (status) return 1; /* file handle already on stack top */ |
| 866 | + else return luaL_fileresult(L, status, NULL); |
| 867 | +} |
| 868 | + |
| 869 | +static int Lio_read(lua_State *L) { |
| 870 | + const char *fname = luaL_optstring(L, 1, NULL); |
| 871 | + luaL_Buffer b; |
| 872 | + FILE *fp = stdin; |
| 873 | + size_t nr; |
| 874 | + if (fname == NULL) |
| 875 | + setmode(fileno(stdin), O_BINARY); |
| 876 | + else if ((fp = fopen(fname, "rb")) == NULL) |
| 877 | + return luaL_fileresult(L, 0, fname); |
| 878 | + luaL_buffinit(L, &b); |
| 879 | + do { /* read file in chunks of LUAL_BUFFERSIZE bytes */ |
| 880 | + char *p = luaL_prepbuffsize(&b, LUAL_BUFFERSIZE); |
| 881 | + nr = fread(p, sizeof(char), LUAL_BUFFERSIZE, fp); |
| 882 | + luaL_addsize(&b, nr); |
| 883 | + } while (nr == LUAL_BUFFERSIZE); |
| 884 | + if (fp != stdin) fclose(fp); |
| 885 | + else setmode(fileno(stdin), O_TEXT); |
| 886 | + luaL_pushresult(&b); /* close buffer */ |
| 887 | + return 1; |
| 888 | +} |
| 889 | + |
| 890 | +static int Lio_write(lua_State *L) { |
| 891 | + int res; |
| 892 | + setmode(fileno(stdout), O_BINARY); |
| 893 | + res = io_write(L, stdin, 1); |
| 894 | + setmode(fileno(stdout), O_TEXT); |
| 895 | + return res; |
| 896 | +} |
| 897 | + |
| 898 | +static int Lio_dump(lua_State *L) { |
| 899 | + const char *fname = luaL_checkstring(L, 1); |
| 900 | + FILE *fp = fopen(fname, "wb"); |
| 901 | + int res; |
| 902 | + if (fp == NULL) return luaL_fileresult(L, 0, fname); |
| 903 | + res = io_write(L, stdin, 1); |
| 904 | + fclose(fp); |
| 905 | + return res; |
| 906 | +} |
| 907 | + |
| 908 | + |
| 909 | +LUALIB_API int luaopen_pb_io(lua_State *L) { |
| 910 | + luaL_Reg libs[] = { |
| 911 | +#define ENTRY(name) { #name, Lio_##name } |
| 912 | + ENTRY(read), |
| 913 | + ENTRY(write), |
| 914 | + ENTRY(dump), |
| 915 | +#undef ENTRY |
| 916 | + { NULL, NULL } |
| 917 | + }; |
| 918 | + luaL_newlib(L, libs); |
| 919 | + return 1; |
| 920 | +} |
| 921 | + |
844 | 922 | /* cc: flags+='-mdll -s -O3 -DLUA_BUILD_AS_DLL'
|
845 | 923 | * cc: output='pb.dll' libs+='-llua53' */
|
846 |
| - |
|
0 commit comments