[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Wrapping existing FILE *s as Lua file objects
- From: Mildred <ml.mildred593@...>
- Date: Sat, 13 Jan 2007 14:30:02 +0100
Le sam 13/01/2007 à 00:50 Reuben Thomas à écrit:
> I can't see a way to do this after looking at liolib.c and googling a
> bit. It's obviously fairly trivial to extend liolib.c to do it, but a
> pain as you can't do it with vanilla Lua.
>
> Am I missing something? I don't imagine it's that rare to want to
> pass a FILE * obtained from somewhere else into a Lua script without
> re-inventing the liolib wheel...
>
I looked at it yesteday and it is simple.
A file object is just a full userdata that contains a pointer to the
FILE* object associated with the metatable that have the name
LUA_FILEHANDLE in the registery
(LUA_FILEHANDLE seems to be defined in lualib.h)
So I think it would look like:
FILE** ud = lua_newuserdata(L, sizeof(FILE*));
lua_getfield(L, LUA_REGISTERYINDEX, LUA_FILEHANDLE);
lua_setmetatable(L, -2);
*ud = my_file_object;
and to get it back (found in liolib.c):
FILE* f = *((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE));
(source code not checked)
Mildred
--
Mildred <xmpp:mildred@jabber.fr> <http://mildred632.free.fr/>
Clef GPG : <hkp://pgp.mit.edu> ou <http://mildred632.free.fr/gpg_key>
Fingerprint : 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B]