lua-users home
lua-l archive

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


On Thu, Apr 8, 2010 at 3:41 PM, cynthia powers <powcyn@gmail.com> wrote:
> Hi,
>
> A newbie question, so I apologise in advance if its a trivial question.
> I have a c-lua-c setup, and I have a list of FILE* to pass from c world to
> Lua world, back to c world.
> How do I do this?
> In c world, I have say:
> FILE* fp1, fp2;
> array = {fp1, fp2} which is passed as an argument to the lua function, I
> make a lua table for the array, but how do I get the file pointers? I could
> not find anything like a lua_tofilepointers().

Look at how liolib.c (in Lua source) handles passing file pointers to
and from Lua. You will see that Lua uses a full userdata to hold these
pointers. Assuming the io library is included in your application, you
can add a new file from C using something like:

#include "lualib.h"

...

*(lua_newuserdata(L, sizeof(FILE *))) = fp;
luaL_getmetatable(L, LUA_FILEHANDLE);
lua_setmetable(L, -2);
/* you now have a Lua FILE object on the stack */

-- 
- Patrick Donnelly