From 3952102886f390310add6ecb5750073693beb8d1 Mon Sep 17 00:00:00 2001 From: Alexandre Erwin Ittner Date: Fri, 8 Jul 2005 02:25:50 +0000 Subject: [PATCH] *** empty log message *** git-svn-id: file:///var/svn/lua-iconv/trunk@16 9538949d-8f27-0410-946f-ce01ef448559 --- iconv.lua | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 iconv.lua diff --git a/iconv.lua b/iconv.lua new file mode 100644 index 0000000..f712c22 --- /dev/null +++ b/iconv.lua @@ -0,0 +1,52 @@ + +-- 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 +