lua-iconv/uniopen.lua
Alexandre Erwin Ittner e9f3a5e377 Make module usage cleaner, removing all global symbols
The 'module' function was also deprecated in Lua 5.2.0.
2010-11-17 12:37:49 -02:00

39 lines
836 B
Lua

-- Simple (and incomplete) Unicode I/O layer.
local iconv = require("iconv")
local m = { }
local mti = { }
local mt = { __index = mti }
function m.open(fname, mode, fromcharset, tocharset)
assert(mode == "r" or mode == "rb", "Only read modes are supported yet")
tocharset = tocharset or "utf8"
local cd = assert(iconv.new(fromcharset, tocharset), "Bad charset")
local fp = io.open(fname, mode)
if not fp then
return nil
end
local o = { fp = fp, cd = cd }
setmetatable(o, mt)
return o;
end
function mti.read(fp, mod)
assert(fp and fp.fp and fp.cd, "Bad file descriptor")
local ret = fp.fp:read(mod)
if ret then
return fp.cd:iconv(ret) -- returns: string, error code
else
return nil
end
end
function mti.close(fp)
assert(fp and fp.fp, "Bad file descriptor")
fp.fp:close()
end
return m