diff --git a/luaiconv.c b/luaiconv.c index 53827ed..213a2d3 100644 --- a/luaiconv.c +++ b/luaiconv.c @@ -150,6 +150,45 @@ static int Liconv(lua_State *L) return 1; /* Done */ } +#ifdef HAS_ICONVLIST + +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: n */ + lua_remove(L, -1); + for(i = 0; i < cnt; i++) + { + /* Stack> */ + lua_pushnumber(L, n++); + lua_pushstring(L, names[i]); + /* Stack: n */ + lua_settable(L, -3); + } + lua_pushnumber(L, n); + /* Stack: n */ + return 0; +} + + +static int Liconvlist(lua_State *L) +{ + lua_newtable(L); + lua_pushnumber(L, 1); + + /* Stack: 1 */ + iconvlist(push_one, (void*) L); + + /* Stack: n */ + lua_remove(L, -1); + return 1; +} + +#endif + static int Liconv_close(lua_State *L) { @@ -167,6 +206,9 @@ static const luaL_reg inconvFuncs[] = { "open", Liconv_open }, { "new", Liconv_open }, { "iconv", Liconv }, +#ifdef HAS_ICONVLIST + { "list", Liconvlist }, +#endif { NULL, NULL } }; diff --git a/test_iconv.lua b/test_iconv.lua index c7e8483..689cc79 100644 --- a/test_iconv.lua +++ b/test_iconv.lua @@ -1,4 +1,3 @@ - assert(loadlib("./libluaiconv.so", "luaopen_iconv"))() cd = iconv.new("utf-8", "iso-8859-1") @@ -8,7 +7,7 @@ cd = iconv.new("utf-8", "iso-8859-1") assert(cd, "Invalid conversion") --ret, err = cd:iconv("Isso é um teste com acentuação") -ret, err = cd:iconv("ç") +ret, err = cd:iconv("ÃÁÉÍÓÚÇÑÝÔÕ") if err == iconv.ERROR_INCOMPLETE then print("Error: Incomplete input.")