Created
August 28, 2011 19:42
-
-
Save romuloceccon/1177121 to your computer and use it in GitHub Desktop.
oo lua binding example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define LUA_LIB | |
#include <windows.h> | |
#include "lua.h" | |
#include "lauxlib.h" | |
#include "ldiriterator.h" | |
typedef struct DirectoryIterator | |
{ | |
HANDLE handle; | |
BOOL last_result; | |
WIN32_FIND_DATA find_data; | |
} DirectoryIterator; | |
static int newDirectoryIterator(lua_State *L) | |
{ | |
DirectoryIterator *p; | |
const char *s = luaL_checkstring(L, 1); | |
HANDLE handle; | |
WIN32_FIND_DATA find_data; | |
handle = FindFirstFileA(s, &find_data); | |
if (handle == INVALID_HANDLE_VALUE) | |
{ | |
lua_pushnil(L); | |
return 1; | |
} | |
p = (DirectoryIterator *) lua_newuserdata(L, sizeof(DirectoryIterator)); | |
luaL_getmetatable(L, "LuaBook.directoryiterator"); | |
lua_setmetatable(L, -2); | |
p->handle = handle; | |
p->last_result = TRUE; | |
memcpy(&p->find_data, &find_data, sizeof(find_data)); | |
return 1; | |
} | |
static int nextEntry(lua_State *L) | |
{ | |
void *ud; | |
DirectoryIterator *p; | |
ud = luaL_checkudata(L, 1, "LuaBook.directoryiterator"); | |
luaL_argcheck(L, ud != NULL, 1, "`directoryiterator' expected"); | |
p = (DirectoryIterator *) ud; | |
if (p->last_result) | |
lua_pushstring(L, p->find_data.cFileName); | |
else | |
lua_pushnil(L); | |
p->last_result = FindNextFile(p->handle, &p->find_data); | |
return 1; | |
} | |
static int closeIterator(lua_State *L) | |
{ | |
DirectoryIterator *p = (DirectoryIterator *) luaL_checkudata(L, 1, "LuaBook.directoryiterator"); | |
FindClose(p->handle); | |
return 0; | |
} | |
static const struct luaL_reg directoryiterator_f [] = | |
{ | |
{"new", newDirectoryIterator}, | |
{NULL, NULL} | |
}; | |
static const struct luaL_reg directoryiterator_m [] = | |
{ | |
{"next", nextEntry}, | |
{"__gc", closeIterator}, | |
{NULL, NULL} | |
}; | |
LUALIB_API int luaopen_diriterator(lua_State *L) | |
{ | |
luaL_newmetatable(L, "LuaBook.directoryiterator"); | |
lua_pushstring(L, "__index"); | |
lua_pushvalue(L, -2); | |
lua_settable(L, -3); | |
luaL_openlib(L, NULL, directoryiterator_m, 0); | |
luaL_openlib(L, "directoryiterator", directoryiterator_f, 0); | |
return 1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LUALIB_API int luaopen_diriterator(lua_State *L); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
** $Id: linit.c,v 1.14.1.1 2007/12/27 13:02:25 roberto Exp $ | |
** Initialization of libraries for lua.c | |
** See Copyright Notice in lua.h | |
*/ | |
#define linit_c | |
#define LUA_LIB | |
#include "lua.h" | |
#include "lualib.h" | |
#include "lauxlib.h" | |
#include "ldiriterator.h" | |
static const luaL_Reg lualibs[] = { | |
{"", luaopen_base}, | |
{LUA_LOADLIBNAME, luaopen_package}, | |
{LUA_TABLIBNAME, luaopen_table}, | |
{LUA_IOLIBNAME, luaopen_io}, | |
{LUA_OSLIBNAME, luaopen_os}, | |
{LUA_STRLIBNAME, luaopen_string}, | |
{LUA_MATHLIBNAME, luaopen_math}, | |
{LUA_DBLIBNAME, luaopen_debug}, | |
{"diriterator", luaopen_diriterator}, | |
{NULL, NULL} | |
}; | |
LUALIB_API void luaL_openlibs (lua_State *L) { | |
const luaL_Reg *lib = lualibs; | |
for (; lib->func; lib++) { | |
lua_pushcfunction(L, lib->func); | |
lua_pushstring(L, lib->name); | |
lua_call(L, 1, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment