lua-users home
lua-l archive

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


It was thus said that the Great HyperHacker once stated:
> With 5.2 getting so near completion, I feel it's worth bringing up a
> couple small enhancement ideas I had in hopes they might be considered
> for inclusion in the official release:

  Another useful enhancement, given the recent talk about LUA_FILEHANDLE
(and just today being bit by that---sigh), a proposed Lua API for extending
LUA_FILEHANDLE.

  I think the minimum you would need is:

	/****************************************************************
	* Define the LUA_FILEHANDLE user data type.  The only defined 
	* field is 'fp', which shall be of FILE *.  There may be more, but
	* 'fp' is the only defined field that must be there.
	*****************************************************************/

	typedef struct
	{
	  FILE *fp;
	} lua_File;

	/***************************************************************
	* Associate a close function with given file.  If the function is
	* NULL, then use a default function (say, io_fclose() for example)
	* shall be used.
	*****************************************************************/

	void luaL_fileclose(
		lua_State     *L,
		lua_File      *uf,
		lua_CFunction  cls
	);

  Then, it becomes a simple matter of:

	lua_File *uf;

	uf = lua_newuserdata(L,sizeof(lua_File));
	luaL_fileclose(L,uf,my_close);
	luaL_getmetatable(L,LUA_FILEHANDLE);
	lua_setmetatable(L,-2);

  -spc (That seems to do it, I think ... )