lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Whoa...I can't believe how brilliantly simple that is.  
I would never have thought of an approach of building an extension that
sets binary mode as a simple side-effect of doing a 'require' on it.

Thank you so much.

Gavin.


On Mon, 23 Oct 2006 18:42:45 -0400, "Jerome Vuarand"
<jerome.vuarand@ubisoft.com> said:
> I think you can simply modify stdin and stdout like you mentionned, but
> in a dll loaded by Lua. Example:
> 
> // binstd.c ///////////////////////////////////
> #include <lua.h>
> 
> LUA_API int luaopen_binstd(lua_State* L)
> {
>     _setmode(fileno(stdin), O_BINARY);
>     _setmode(fileno(stdout), O_BINARY);
>     return 0;
> }
> ///////////////////////////////////////////////
> Compile binstd.c to binstd.so or binstd.dll depending on your platform.
> 
> Test from a shell:
> % cat bingrep1.lua
> while true do
>    local line = io.read("*l")
>    if line:find((...)) then
>       io.write(line.."\n")
>    end
> end
> % lua -l binstd bingrep1.lua foo < input.bin > output.bin
> 
> With a handmade or embedded interpreter you have to explicitly load the
> module in the script:
> % cat bingrep2.lua
> require "binstd" -- This single additionnal line is required
> 
> while true do
>    local line = io.read("*l")
>    if line:find((...)) then
>       io.write(line.."\n")
>    end
> end
> % lua bingrep2.lua foo < input.bin > output.bin
> 
> These two scripts will run on a stock Lua interpreter, provided the
> binstd.(dll|so) file is in Lua's CPATH.
> 
> 
> [snip]