Adding sample Unicode I/O library

git-svn-id: file:///var/svn/lua-iconv/trunk@58 9538949d-8f27-0410-946f-ce01ef448559
This commit is contained in:
Alexandre Erwin Ittner 2008-06-08 16:31:47 +00:00
parent 1e2ee8d113
commit a918b0109a

37
uniopen.lua Normal file
View File

@ -0,0 +1,37 @@
-- Simple (and incomplete) Unicode I/O layer.
module("uniopen", package.seeall)
require "iconv"
local mt = { __index = _M }
function 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 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 close(fp)
assert(fp and fp.fp, "Bad file descriptor")
fp.fp:close()
end