forked from lunarmodules/lua-iconv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuniopen.lua
37 lines (30 loc) · 802 Bytes
/
uniopen.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
-- Simple (and incomplete) Unicode I/O layer.
local iconv = require("iconv")
local m = { }
local mti = { }
local mt = { __index = mti }
function m.open(fname, mode, tocharset, fromcharset)
assert(mode == "r" or mode == "rb", "Only read modes are supported yet")
local cd = assert(iconv.new(tocharset, fromcharset), "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