File: uniopen.lua

package info (click to toggle)
lua-iconv 7-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, stretch, trixie
  • size: 132 kB
  • ctags: 33
  • sloc: ansic: 167; makefile: 26; sh: 11
file content (37 lines) | stat: -rw-r--r-- 802 bytes parent folder | download | duplicates (3)
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