Updating for Lua 5.1

git-svn-id: file:///var/svn/lua-iconv/trunk@26 9538949d-8f27-0410-946f-ce01ef448559
This commit is contained in:
Alexandre Erwin Ittner
2006-04-21 14:34:44 +00:00
parent 4ed127ac23
commit ec5bf22d3a
9 changed files with 92 additions and 269 deletions

View File

@@ -1,6 +1,6 @@
/*
* luaiconv - Performs character set conversions in Lua
* (c) 2005 Alexandre Erwin Ittner <aittner@netuno.com.br>
* (c) 2005-06 Alexandre Erwin Ittner <aittner@netuno.com.br>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@@ -37,7 +37,7 @@
#include <errno.h>
#define LIB_NAME "iconv"
#define LIB_VERSION LIB_NAME " r1"
#define LIB_VERSION LIB_NAME " r2"
#define ICONV_TYPENAME "iconv_t"
@@ -68,18 +68,15 @@
#define ERROR_UNKNOWN 4
static void push_iconv_t(lua_State *L, iconv_t cd)
{
static void push_iconv_t(lua_State *L, iconv_t cd) {
boxptr(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)
{
static iconv_t get_iconv_t(lua_State *L, int i) {
if(luaL_checkudata(L, i, ICONV_TYPENAME) != NULL) {
iconv_t cd = unboxptr(L, i);
if(cd == (iconv_t) NULL)
luaL_error(L, "attempt to use an invalid " ICONV_TYPENAME);
@@ -90,8 +87,7 @@ static iconv_t get_iconv_t(lua_State *L, int i)
}
static int Liconv_open(lua_State *L)
{
static int Liconv_open(lua_State *L) {
const char *tocode = luaL_checkstring(L, 1);
const char *fromcode = luaL_checkstring(L, 2);
iconv_t cd = iconv_open(tocode, fromcode);
@@ -103,8 +99,7 @@ static int Liconv_open(lua_State *L)
}
static int Liconv(lua_State *L)
{
static int Liconv(lua_State *L) {
iconv_t cd = get_iconv_t(L, 1);
size_t ibleft = lua_strlen(L, 2);
char *inbuf = (char*) luaL_checkstring(L, 2);
@@ -116,49 +111,38 @@ static int Liconv(lua_State *L)
int hasone = 0;
outbuf = (char*) malloc(obsize * sizeof(char));
if(outbuf == NULL)
{
if(outbuf == NULL) {
lua_pushstring(L, "");
lua_pushnumber(L, ERROR_NO_MEMORY);
return 2;
}
outbufs = outbuf;
do
{
do {
ret = iconv(cd, &inbuf, &ibleft, &outbuf, &obleft);
if(ret == (size_t)(-1))
{
if(ret == (size_t)(-1)) {
lua_pushlstring(L, outbufs, obsize - obleft);
if(hasone == 1)
lua_concat(L, 2);
hasone = 1;
if(errno == EILSEQ)
{
if(errno == EILSEQ) {
lua_pushnumber(L, ERROR_INVALID);
free(outbufs);
return 2; /* Invalid character sequence */
}
else if(errno == EINVAL)
{
} else if(errno == EINVAL) {
lua_pushnumber(L, ERROR_INCOMPLETE);
free(outbufs);
return 2; /* Incomplete character sequence */
}
else if(errno == E2BIG)
{
} else if(errno == E2BIG) {
obleft = obsize;
outbuf = outbufs;
}
else
{
} else {
lua_pushnumber(L, ERROR_UNKNOWN);
free(outbufs);
return 2; /* Unknown error */
}
}
}
while(ret != (size_t) 0);
} while(ret != (size_t) 0);
lua_pushlstring(L, outbufs, obsize - obleft);
if(hasone == 1)
@@ -171,16 +155,14 @@ static int Liconv(lua_State *L)
#ifdef HAS_ICONVLIST /* a GNU extension? */
static int push_one(unsigned int cnt, char *names[], void *data)
{
static int push_one(unsigned int cnt, char *names[], void *data) {
lua_State *L = (lua_State*) data;
int n = (int) lua_tonumber(L, -1);
int i;
/* Stack: <tbl> n */
lua_remove(L, -1);
for(i = 0; i < cnt; i++)
{
for(i = 0; i < cnt; i++) {
/* Stack> <tbl> */
lua_pushnumber(L, n++);
lua_pushstring(L, names[i]);
@@ -193,8 +175,7 @@ static int push_one(unsigned int cnt, char *names[], void *data)
}
static int Liconvlist(lua_State *L)
{
static int Liconvlist(lua_State *L) {
lua_newtable(L);
lua_pushnumber(L, 1);
@@ -209,8 +190,7 @@ static int Liconvlist(lua_State *L)
#endif
static int Liconv_close(lua_State *L)
{
static int Liconv_close(lua_State *L) {
iconv_t cd = get_iconv_t(L, 1);
if(iconv_close(cd) == 0)
lua_pushboolean(L, 1); /* ok */
@@ -220,8 +200,7 @@ static int Liconv_close(lua_State *L)
}
static const luaL_reg inconvFuncs[] =
{
static const luaL_reg inconvFuncs[] = {
{ "open", Liconv_open },
{ "new", Liconv_open },
{ "iconv", Liconv },
@@ -232,15 +211,13 @@ static const luaL_reg inconvFuncs[] =
};
static const luaL_reg iconvMT[] =
{
static const luaL_reg iconvMT[] = {
{ "__gc", Liconv_close },
{ NULL, NULL }
};
int luaopen_iconv(lua_State *L)
{
int luaopen_iconv(lua_State *L) {
luaL_register(L, LIB_NAME, inconvFuncs);
tblseticons(L, "ERROR_NO_MEMORY", ERROR_NO_MEMORY);