From ec5bf22d3a923d87cba7d2915d062f437ca5312d Mon Sep 17 00:00:00 2001 From: Alexandre Erwin Ittner Date: Fri, 21 Apr 2006 14:34:44 +0000 Subject: [PATCH] Updating for Lua 5.1 git-svn-id: file:///var/svn/lua-iconv/trunk@26 9538949d-8f27-0410-946f-ce01ef448559 --- COPYING | 2 +- Makefile | 43 ++++++++++++++--------- README | 53 ++++++++++++----------------- iconv.lua | 52 ---------------------------- install51.lua | 92 -------------------------------------------------- luaiconv.c | 67 ++++++++++++------------------------ makestr.lua | 4 +-- maketargz.sh | 5 ++- test_iconv.lua | 43 ++++++++++------------- 9 files changed, 92 insertions(+), 269 deletions(-) delete mode 100644 iconv.lua delete mode 100644 install51.lua diff --git a/COPYING b/COPYING index 16fd449..2bf33e4 100644 --- a/COPYING +++ b/COPYING @@ -1,4 +1,4 @@ -Lua-iconv is (c) 2005 Alexandre Erwin Ittner +Lua-iconv is (c) 2005-06 Alexandre Erwin Ittner Lua-iconv is copyrighted free software: it can be used for both academic and commercial purposes at absolutely no cost. There are no royalties diff --git a/Makefile b/Makefile index c5cc1f4..10fdd88 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ # luaiconv - Performs character set conversions in Lua -# (c) 2005 Alexandre Erwin Ittner +# (c) 2005-06 Alexandre Erwin Ittner # # Permission is hereby granted, free of charge, to any person obtaining # 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 # documentation would be greatly appreciated (but it is not required). +#CC = gcc +#RM = rm -CC=gcc -OUTFILE=libluaiconv.so -CFLAGS=-Wall -LFLAGS=-shared -llua -#WLFLAGS=-liconv +# Name of .pc file. "lua5.1" on Debian/Ubuntu +LUAPKG = lua5.1 +CFLAGS = `pkg-config $(LUAPKG) --cflags` -O3 -Wall +INSTALL_PATH = `pkg-config $(LUAPKG) --variable=INSTALL_CMOD` +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 - $(CC) -o $(OUTFILE) $(CFLAGS) $(LFLAGS) $(WLFLAGS) luaiconv.c - -install: $(OUTFILE) - cp $(OUTFILE) /usr/lib/ - -install51: $(OUTFILE) - lua install51.lua $(OUTFILE) iconv +install: iconv.so + make test + install -s iconv.so $(INSTALL_PATH) clean: - rm -f $(OUTFILE) *.o + $(RM) iconv.so + +test: iconv.so test_iconv.lua + lua test_iconv.lua + +all: iconv.so diff --git a/README b/README index 2981ec8..6f3d1f8 100644 --- a/README +++ b/README @@ -1,11 +1,9 @@ Lua-iconv: performs character set conversions in Lua - (c) 2005 Alexandre Erwin Ittner + (c) 2005-06 Alexandre Erwin Ittner Project page: http://lua-iconv.luaforge.net/ - - === INTRODUCTION === 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 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: 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 === -Lua-iconv is (c) 2005 Alexandre Erwin Ittner +Lua-iconv is (c) 2005-06 Alexandre Erwin Ittner Lua-iconv is copyrighted free software: it can be used for both academic 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/ 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 -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: +interpreter using the new module system in Lua 5.1. + +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 -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 - - 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. +Compiling on systems without pkg-config requires manual changes on the +Makefile. === LOADING AND INITIALIZATION === Lua-iconv is a shared library that exports a single function called -luaopen_iconv that must be called by the Lua interpreter. You must -use loadlib function to for that. Using Lua 5.0, under Unix and with -libluaiconv.so on your LUA_PATH (or also your LD_PATH) you can just do: +luaopen_iconv that must be called by the Lua interpreter. Lua 5.1 +uses the new package system, so you can simply do a - assert(loadlib("libluaiconv.so", "luaopen_iconv"))() + require "iconv" -And, under Windows and with libluaiconv.dll on your path, you can do - - 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). +call to load up the library (that, of course, must be installed in a +directory from package.cpath). diff --git a/iconv.lua b/iconv.lua deleted file mode 100644 index f712c22..0000000 --- a/iconv.lua +++ /dev/null @@ -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 - diff --git a/install51.lua b/install51.lua deleted file mode 100644 index 5440fbe..0000000 --- a/install51.lua +++ /dev/null @@ -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) diff --git a/luaiconv.c b/luaiconv.c index e431b60..451e737 100644 --- a/luaiconv.c +++ b/luaiconv.c @@ -1,6 +1,6 @@ /* * luaiconv - Performs character set conversions in Lua - * (c) 2005 Alexandre Erwin Ittner + * (c) 2005-06 Alexandre Erwin Ittner * * 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 #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: n */ lua_remove(L, -1); - for(i = 0; i < cnt; i++) - { + for(i = 0; i < cnt; i++) { /* Stack> */ 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); diff --git a/makestr.lua b/makestr.lua index cb53d42..1b47409 100644 --- a/makestr.lua +++ b/makestr.lua @@ -5,10 +5,10 @@ fp:close() local i local c = 0 -local ostr = "local xxxxx = \"" +local ostr = "local xxxxxxxxxxx = \"" for i = 1, string.len(str) do 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") ostr = ".. \"" end diff --git a/maketargz.sh b/maketargz.sh index 1bdedf1..907bec3 100755 --- a/maketargz.sh +++ b/maketargz.sh @@ -1,7 +1,7 @@ #!/bin/sh PACKAGE=lua-iconv -VERSION=r1 +VERSION=r2 DIRNAME=$PACKAGE-$VERSION TGZNAME=$DIRNAME.tar.gz @@ -9,8 +9,7 @@ TGZNAME=$DIRNAME.tar.gz rm -f $TGZNAME mkdir $DIRNAME -cp COPYING Makefile README luaiconv.c iconv.lua test_iconv.lua \ - install51.lua $DIRNAME +cp COPYING Makefile README luaiconv.c test_iconv.lua $DIRNAME tar -czf $TGZNAME $DIRNAME rm -rf $DIRNAME diff --git a/test_iconv.lua b/test_iconv.lua index a2a6aba..0cc9e3a 100644 --- a/test_iconv.lua +++ b/test_iconv.lua @@ -1,27 +1,23 @@ require "iconv" -- Set your terminal encoding here -local termcs = "iso-8859-1" - - -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 termcs = "iso-8859-1" +local termcs = "utf-8" +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" .. "\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) print("\n-- Testing conversion from " .. from .. " to " .. to) local cd = iconv.new(to .. "//TRANSLIT", from) - if not cd then - printf("FAILED!") - return - end + assert(cd, "Failed to create a converter object.") local ostr, err = cd:iconv(text) if err == iconv.ERROR_INCOMPLETE then @@ -96,7 +89,6 @@ function check_one(to, from, text) elseif err == iconv.ERROR_UNKNOWN then print("ERROR: There was an unknown error.") end - print(ostr) end @@ -105,4 +97,3 @@ check_one(termcs, "utf8", utf8) check_one(termcs, "utf16", utf16) check_one(termcs, "EBCDIC-CP-ES", ebcdic) -