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,4 +1,4 @@
Lua-iconv is (c) 2005 Alexandre Erwin Ittner <aittner@netuno.com.br> Lua-iconv is (c) 2005-06 Alexandre Erwin Ittner <aittner@netuno.com.br>
Lua-iconv is copyrighted free software: it can be used for both academic Lua-iconv is copyrighted free software: it can be used for both academic
and commercial purposes at absolutely no cost. There are no royalties and commercial purposes at absolutely no cost. There are no royalties

View File

@ -1,5 +1,5 @@
# luaiconv - Performs character set conversions in Lua # 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 # Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the # a copy of this software and associated documentation files (the
@ -23,24 +23,35 @@
# If you use this package in a product, an acknowledgment in the product # If you use this package in a product, an acknowledgment in the product
# documentation would be greatly appreciated (but it is not required). # documentation would be greatly appreciated (but it is not required).
#CC = gcc
#RM = rm
CC=gcc # Name of .pc file. "lua5.1" on Debian/Ubuntu
OUTFILE=libluaiconv.so LUAPKG = lua5.1
CFLAGS=-Wall CFLAGS = `pkg-config $(LUAPKG) --cflags` -O3 -Wall
LFLAGS=-shared -llua INSTALL_PATH = `pkg-config $(LUAPKG) --variable=INSTALL_CMOD`
#WLFLAGS=-liconv LIBS = `pkg-config $(LUAPKG) --libs`
## If your system doesn't have pkg-config, comment out the previous lines and
## uncomment and change the following ones according to your building
## enviroment.
#CFLAGS = -I/usr/include/lua5.1/ -O3 -Wall
#LIBS = -llua5.1
#INSTALL_PATH = /usr/lib/lua/5.1
all: $(OUTFILE) iconv.so: luaiconv.c
$(CC) -o iconv.so -shared $(LIBS) $(CFLAGS) luaiconv.c
$(OUTFILE): luaiconv.c install: iconv.so
$(CC) -o $(OUTFILE) $(CFLAGS) $(LFLAGS) $(WLFLAGS) luaiconv.c make test
install -s iconv.so $(INSTALL_PATH)
install: $(OUTFILE)
cp $(OUTFILE) /usr/lib/
install51: $(OUTFILE)
lua install51.lua $(OUTFILE) iconv
clean: clean:
rm -f $(OUTFILE) *.o $(RM) iconv.so
test: iconv.so test_iconv.lua
lua test_iconv.lua
all: iconv.so

53
README
View File

@ -1,11 +1,9 @@
Lua-iconv: performs character set conversions in Lua Lua-iconv: 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>
Project page: http://lua-iconv.luaforge.net/ Project page: http://lua-iconv.luaforge.net/
=== INTRODUCTION === === INTRODUCTION ===
Lua-iconv is a Lua binding to the POSIX 'iconv' library. The iconv Lua-iconv is a Lua binding to the POSIX 'iconv' library. The iconv
@ -14,6 +12,9 @@ sequence of corresponding characters in another codeset. The codesets
are those specified in the iconv.new() call that returned the conversion are those specified in the iconv.new() call that returned the conversion
descriptor, cd. descriptor, cd.
Lua-iconv r2 *requires* Lua 5.1. If you are using Lua 5.0, please use the
previous version (lua-iconv-r1).
Details regarding iconv may be obtained from: Details regarding iconv may be obtained from:
http://www.opengroup.org/onlinepubs/007908799/xsh/iconv.h.html http://www.opengroup.org/onlinepubs/007908799/xsh/iconv.h.html
@ -21,7 +22,7 @@ http://www.opengroup.org/onlinepubs/007908799/xsh/iconv.h.html
=== LICENSE === === LICENSE ===
Lua-iconv is (c) 2005 Alexandre Erwin Ittner <aittner@netuno.com.br> Lua-iconv is (c) 2005-06 Alexandre Erwin Ittner <aittner@netuno.com.br>
Lua-iconv is copyrighted free software: it can be used for both academic Lua-iconv is copyrighted free software: it can be used for both academic
and commercial purposes at absolutely no cost. There are no royalties and commercial purposes at absolutely no cost. There are no royalties
@ -51,50 +52,38 @@ or GNU-like "copyleft" restrictions. The legal details are below:
=== DOWNLOAD AND INSTALLATION === === DOWNLOAD, COMPILATION AND INSTALLATION ===
Lua-iconv can be obtained from its Luaforge project page: Lua-iconv can be obtained from its LuaForge project page:
http://lua-iconv.luaforge.net/ http://lua-iconv.luaforge.net/
Lua-iconv must be compiled as a shared object and loaded by the Lua Lua-iconv must be compiled as a shared object and loaded by the Lua
interpreter using the 'loadlib' facility (or the new module system in interpreter using the new module system in Lua 5.1.
Lua 5.1). To compile the library, just fire up your favourite shell, untar
the distribution package and type, as root, within the program directory: If you are using a system with pkg-config (as many Linux distributions and
Unix flavors) just fire up your favourite shell, untar the distribution
package and type, as root, within the program directory:
make install make install
The library will be compiled and installed on the /usr/lib/ directory. The library will be compiled and installed on the in the correct path (that
is defined in lua5.1.pc.
To install the library with Lua 5.1 and above just type Compiling on systems without pkg-config requires manual changes on the
Makefile.
make install51
This will cause the Makefile to run a Lua script that will install the
library into a directory from package.cpath, allowing you to use the
package system to load the library.
=== LOADING AND INITIALIZATION === === LOADING AND INITIALIZATION ===
Lua-iconv is a shared library that exports a single function called Lua-iconv is a shared library that exports a single function called
luaopen_iconv that must be called by the Lua interpreter. You must luaopen_iconv that must be called by the Lua interpreter. Lua 5.1
use loadlib function to for that. Using Lua 5.0, under Unix and with uses the new package system, so you can simply do a
libluaiconv.so on your LUA_PATH (or also your LD_PATH) you can just do:
assert(loadlib("libluaiconv.so", "luaopen_iconv"))() require "iconv"
And, under Windows and with libluaiconv.dll on your path, you can do call to load up the library (that, of course, must be installed in a
directory from package.cpath).
assert(loadlib("libluaiconv.dll", "luaopen_iconv"))()
On the Lua 5.1, Lua-iconv uses the new package system that allows you
to simply do a
require "gd"
call to load up the library (that was installed on the right place by
install51.lua).

View File

@ -1,52 +0,0 @@
-- Common locations (add new ones if needed).
local locs = {
-- Unices
"libluaiconv.so",
"./libluaiconv.so",
"/usr/lib/libluaiconv.so",
"/usr/local/lib/libluaiconv.so",
"/usr/local/lib/lua/5.0/lib/libluaiconv.so",
-- Windows
"libluaiconv.dll",
"luaiconv.dll",
".\\luaiconv.dll",
".\\libluaiconv.dll",
"c:\\lua\\lib\\libluaiconv.dll"
}
local loadiconv, fname, ndx
for ndx, fname in ipairs(locs) do
loadiconv = loadlib(fname, "luaopen_iconv")
if loadiconv then
break
end
end
assert(loadiconv, "Can't load Lua-iconv")
loadiconv()
-- An useful extension: Detects automatically the charset of the string
-- and returns it on the chosen one.
iconv.commoncharsets = {
"ascii", "iso-8859-1", "utf-8", "utf-16", "windows-1252"
}
iconv.autoconvert = function(tocode, str)
local _, fromcode, cd, ret, err
for _, fromcode in pairs(iconv.commoncharsets) do
cd = iconv.new(tocode, fromcode)
ret, err = cd:iconv(str)
if not err then
return ret
end
end
return nil
end

View File

@ -1,92 +0,0 @@
#!/usr/bin/env lua
-- Easy way to put a library on the path. For Lua >= 5.1 only.
-- (c) 2005 Alexandre Erwin Ittner < aittner{at}netuno.com.br >
--
-- This file is distributed under the same license that Lua 5.1 itself.
--
-- $Id$
--
-- Splits a package.cpath into a table.
function splitpath(str)
local t = {}
local v
for v in string.gfind(str, "([^;]+);") do
table.insert(t, v)
end
return t
end
-- Checks if a given file exists. DUMB. Do not distinguish files from
-- symlinks, pipes, sockets, device drivers, etc.
function fileexists(fname)
local fp = io.open(fname, "r")
if fp then
fp:close()
return true
end
return false
end
-- Finds the first absolute path on the list. Works with *nix (MacOS X
-- included) and Windows paths.
function findplace(tbl)
local k, v
for k, v in ipairs(tbl) do
if v:sub(1, 1) == "/"
or v:sub(2, 3) == ":\\" then
return v
end
end
end
-- It's REALLY stupid, but also it's REALLY cross-platform.
function dumbcopy(from, to)
local bufsize = 512*1024 -- 512K at once
local ifp = io.open(from, "rb")
assert(ifp, "Can't open file for reading. File not found?")
local ofp = io.open(to, "wb")
assert(ofp, "Can't open file for writing. Permission denied?")
local buf
repeat
buf = ifp:read(bufsize)
if buf then
ofp:write(buf)
end
until not buf
ifp:close()
ofp:close()
end
-- Copies the file. 'to' MUST be an absolute path!
function copy(from, to)
if to:sub(1, 1) == "/" then -- Cool! We are on Unix, guys!
ret = os.execute('install -D "' .. from .. '" "' .. to .. '"')
if ret == 0 and fileexists(to) then
return
end
end
-- Let's try some violence.
dumbcopy(from, to)
end
-- Installs one file. Simple. Perfect.
function installfile(fname, name)
local tpath = splitpath(package.cpath)
local path = findplace(tpath)
assert(path, "Can't find a place to install")
local dest = path:gsub("%?", name)
print("Install:", dest)
copy(fname, dest)
end
assert(package, "No package module found. Are you REALLY using Lua >= 5.1?")
assert(arg[1] and arg[2], "No arguments provided")
installfile(arg[1], arg[2])
os.exit(0)

View File

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

View File

@ -5,10 +5,10 @@ fp:close()
local i local i
local c = 0 local c = 0
local ostr = "local xxxxx = \"" local ostr = "local xxxxxxxxxxx = \""
for i = 1, string.len(str) do for i = 1, string.len(str) do
ostr = ostr .. "\\" .. string.byte(string.sub(str, i, i+1)) ostr = ostr .. "\\" .. string.byte(string.sub(str, i, i+1))
if string.len(ostr) > 74 then if string.len(ostr) > 72 then
io.write(ostr .. "\"\n") io.write(ostr .. "\"\n")
ostr = ".. \"" ostr = ".. \""
end end

View File

@ -1,7 +1,7 @@
#!/bin/sh #!/bin/sh
PACKAGE=lua-iconv PACKAGE=lua-iconv
VERSION=r1 VERSION=r2
DIRNAME=$PACKAGE-$VERSION DIRNAME=$PACKAGE-$VERSION
TGZNAME=$DIRNAME.tar.gz TGZNAME=$DIRNAME.tar.gz
@ -9,8 +9,7 @@ TGZNAME=$DIRNAME.tar.gz
rm -f $TGZNAME rm -f $TGZNAME
mkdir $DIRNAME mkdir $DIRNAME
cp COPYING Makefile README luaiconv.c iconv.lua test_iconv.lua \ cp COPYING Makefile README luaiconv.c test_iconv.lua $DIRNAME
install51.lua $DIRNAME
tar -czf $TGZNAME $DIRNAME tar -czf $TGZNAME $DIRNAME
rm -rf $DIRNAME rm -rf $DIRNAME

View File

@ -1,27 +1,23 @@
require "iconv" require "iconv"
-- Set your terminal encoding here -- Set your terminal encoding here
local termcs = "iso-8859-1" -- local termcs = "iso-8859-1"
local termcs = "utf-8"
local iso88591 = [[
Ao longe, ao luar
No rio uma vela
Serena a passar,
Que é que me revela
Não sei, mas mue ser
Tornou-se-me estranho,
E eu sonho sem ver
Os sonhos que tenho.
Que angústia me enlaça?
Que amor não se explica?
É a vela que passa
Na noite que fica
-- Fernando Pessoa]]
local iso88591 = "\65\111\32\108\111\110\103\101\44\32\97\111\32\108\117"
.. "\97\114\10\78\111\32\114\105\111\32\117\109\97\32\118\101\108\97\10\83"
.. "\101\114\101\110\97\32\97\32\112\97\115\115\97\114\44\10\81\117\101\32"
.. "\233\32\113\117\101\32\109\101\32\114\101\118\101\108\97\10\10\78\227"
.. "\111\32\115\101\105\44\32\109\97\115\32\109\117\101\32\115\101\114\10"
.. "\84\111\114\110\111\117\45\115\101\45\109\101\32\101\115\116\114\97\110"
.. "\104\111\44\10\69\32\101\117\32\115\111\110\104\111\32\115\101\109\32"
.. "\118\101\114\10\79\115\32\115\111\110\104\111\115\32\113\117\101\32\116"
.. "\101\110\104\111\46\10\10\81\117\101\32\97\110\103\250\115\116\105\97"
.. "\32\109\101\32\101\110\108\97\231\97\63\10\81\117\101\32\97\109\111\114"
.. "\32\110\227\111\32\115\101\32\101\120\112\108\105\99\97\63\10\201\32\97"
.. "\32\118\101\108\97\32\113\117\101\32\112\97\115\115\97\10\78\97\32\110"
.. "\111\105\116\101\32\113\117\101\32\102\105\99\97\10\10\32\32\32\32\45"
.. "\45\32\70\101\114\110\97\110\100\111\32\80\101\115\115\111\97\10"
local utf8 = "\65\111\32\108\111\110\103\101\44\32\97\111\32\108\117\97\114" local utf8 = "\65\111\32\108\111\110\103\101\44\32\97\111\32\108\117\97\114"
.. "\10\78\111\32\114\105\111\32\117\109\97\32\118\101\108\97\10\83\101\114" .. "\10\78\111\32\114\105\111\32\117\109\97\32\118\101\108\97\10\83\101\114"
@ -81,10 +77,7 @@ local ebcdic = "\193\150\64\147\150\149\135\133\107\64\129\150\64\147\164\129"
function check_one(to, from, text) function check_one(to, from, text)
print("\n-- Testing conversion from " .. from .. " to " .. to) print("\n-- Testing conversion from " .. from .. " to " .. to)
local cd = iconv.new(to .. "//TRANSLIT", from) local cd = iconv.new(to .. "//TRANSLIT", from)
if not cd then assert(cd, "Failed to create a converter object.")
printf("FAILED!")
return
end
local ostr, err = cd:iconv(text) local ostr, err = cd:iconv(text)
if err == iconv.ERROR_INCOMPLETE then if err == iconv.ERROR_INCOMPLETE then
@ -96,7 +89,6 @@ function check_one(to, from, text)
elseif err == iconv.ERROR_UNKNOWN then elseif err == iconv.ERROR_UNKNOWN then
print("ERROR: There was an unknown error.") print("ERROR: There was an unknown error.")
end end
print(ostr) print(ostr)
end end
@ -105,4 +97,3 @@ check_one(termcs, "utf8", utf8)
check_one(termcs, "utf16", utf16) check_one(termcs, "utf16", utf16)
check_one(termcs, "EBCDIC-CP-ES", ebcdic) check_one(termcs, "EBCDIC-CP-ES", ebcdic)