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

36
Makefile Normal file
View File

@ -0,0 +1,36 @@
# luaiconv - Performs character set conversions in Lua
# (c) 2005 Alexandre Erwin Ittner <aittner@netuno.com.br>
#
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
CC=gcc
OUTFILE=libluaiconv.so
CFLAGS=-Wall
LFLAGS=-shared -llua -liconv
all: $(OUTFILE)
libluagd.so: luaiconv.c
$(CC) -o $(OUTFILE) $(CFLAGS) $(LFLAGS) luaiconv.c
install: $(OUTFILE)
cp $(OUTFILE) /usr/lib/
clean:
rm -f $(OUTFILE) *.o

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;
}

17
test_iconv.lua Normal file
View File

@ -0,0 +1,17 @@
cd = iconv.new("utf-8", "iso-8859-1")
-- cd = iconv.new("utf-8//IGNORE", "iso-8859-1")
-- cd = iconv.new("utf-8//TRANSLIT", "iso-8859-1")
assert(cd, 'Invalid conversion')
ret, err = cd:iconv('Isso é um teste com acentuação')
if err == iconv.ERROR_INCOMPLETE then
print('Error: Incomplete input.')
elseif err == iconv.ERROR_INVALID then
print('Error: Invalid input.')
elseif err == nil then
print('Result: ', ret)
end