Adicionadno arquivos

git-svn-id: file:///var/svn/lua-iconv/trunk@6 9538949d-8f27-0410-946f-ce01ef448559
This commit is contained in:
Alexandre Erwin Ittner
2005-07-06 00:23:39 +00:00
parent bb90e7e8eb
commit e1445ad0d6
3 changed files with 151 additions and 0 deletions

View File

@@ -23,3 +23,101 @@
*
*/
#include <lua.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <iconv.h>
#define LIB_NAME "iconv"
#define ICONV_TYPENAME "iconv_t"
#define getstring luaL_checkstring
#define getostring(L, i) luaL_optstring(L, i, NULL)
static void push_iconv_t(lua_State *L, iconv_t cd)
{
lua_boxpointer(L, cd);
luaL_getmetatable(L, ICONV_TYPENAME);
lua_setmetatable(L, -2);
}
static iconv_t get_iconv_t(lua_State *L, int i)
{
if(luaL_checkudata(L, i, ICONV_TYPENAME) != NULL)
{
iconv_t cd = lua_unboxpointer(L, i);
if(cd == (iconv_t) NULL)
luaL_error(L, "attempt to use an invalid " ICONV_TYPENAME);
return cd;
}
luaL_typerror(L, i, ICONV_TYPENAME);
return NULL;
}
static int Linconv_open(lua_State *L)
{
const char *fromcode = getstring(L, 1);
const char *tocode = getstring(L, 2);
iconv_t cd iconv_open(tocode, fromcode);
if(cd != (iconv_t)(-1))
push_iconv_t(L, cd); /* ok */
else
lua_pushnil(L); /* erro */
return 1;
}
static in Linconv_close(lua_State *L)
{
iconv_t cd = get_iconv_t(L, 1);
if(iconv_close(cd) == 0)
lua_pushboolean(L, 1); /* ok */
else
lua_pushnil(L); /* erro */
return 1;
}
static const luaL_reg inconvFuncs[] =
{
{ "open", Linconv_open },
{ "new", Linconv_open },
{ "iconv", Linconv_open },
{ NULL, NULL }
};
static const luaL_reg iconvMT[] =
{
{ "__gc", Liconv_close },
{ NULL, NULL }
};
int luaopen_iconv(lua_State *L)
{
luaL_openlib(L, LIB_NAME, inconvFuncs, 0);
lua_pushliteral(L, "metatable"); /* metatable */
luaL_newmetatable(L, ICONV_TYPENAME);
lua_pushliteral(L, "__index");
lua_pushvalue(L, -4);
lua_settable(L, -3);
luaL_openlib(L, NULL, iconvMT, 0);
lua_settable(L, -3);
return 0;
}