Skip to content

Commit b7a8ed5

Browse files
committed
add io module to support binary mode stdin/stdout operations on Windows.
1 parent 987fddf commit b7a8ed5

File tree

2 files changed

+84
-10
lines changed

2 files changed

+84
-10
lines changed

lpb.c

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
#ifdef _MSC_VER
2+
# define _CRT_NONSTDC_NO_WARNINGS
3+
# define _CRT_SECURE_NO_WARNINGS
4+
#endif
5+
16
#define LUA_LIB
27
#include <lua.h>
38
#include <lauxlib.h>
@@ -394,16 +399,15 @@ static int Lbuf_add(lua_State *L) {
394399

395400
static int Lbuf_clear(lua_State *L) {
396401
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);
401406
return 1;
402407
}
403408

404409
static int Lbuf_concat(lua_State *L) {
405410
pb_Buffer *buff = (pb_Buffer*)luaL_checkudata(L, 1, LPB_BUFTYPE);
406-
pb_Buffer *other;
407411
int i, top = lua_gettop(L);
408412
for (i = 2; i < top; ++i) {
409413
size_t len;
@@ -841,6 +845,79 @@ LUALIB_API int luaopen_pb_decoder(lua_State *L) {
841845
return 1;
842846
}
843847

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+
844922
/* cc: flags+='-mdll -s -O3 -DLUA_BUILD_AS_DLL'
845923
* cc: output='pb.dll' libs+='-llua53' */
846-

test.lua

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,10 +389,7 @@ end
389389

390390
--------------------------------------------------
391391

392-
local fh = assert(io.open("descriptor.pb", "rb"))
393-
local content = fh:read "*a"
394-
fh:close()
395-
392+
local content = require "pb.io".read "descriptor.pb"
396393
local t = decode(decoder.new(content), types.google.protobuf.FileDescriptorSet)
397394
load_fileset(t)
398395
io.output "types.lua"

0 commit comments

Comments
 (0)