Class: Ropencc

Inherits:
Object
  • Object
show all
Defined in:
lib/ropencc.rb,
lib/ropencc/version.rb

Defined Under Namespace

Modules: LibC, LibOpenCC

Constant Summary collapse

CONFIGS =
['zhs2zhtw_p.ini', 'zhs2zhtw_v.ini', 'zhs2zhtw_vp.ini', 'zht2zhtw_p.ini', 
'zht2zhtw_v.ini', 'zht2zhtw_vp.ini', 'zhtw2zhs.ini', 'zhtw2zht.ini', 
'zhtw2zhcn_s.ini', 'zhtw2zhcn_t.ini', 'zhs2zht.ini', 'zht2zhs.ini']
DICTS =
{
    :simp_to_trad => 'zhs2zhtw_vp.ini',
    :trad_to_simp => 'zhtw2zhcn_s.ini'
}
VERSION =
'0.0.5'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#descriptorObject

module LibC



23
24
25
# File 'lib/ropencc.rb', line 23

def descriptor
  @descriptor
end

Class Method Details

.conv(dicttype, str) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ropencc.rb', line 57

def self.conv(dicttype, str)
    raise 'unknown simple conversion type' if DICTS.has_key?(dicttype)
    od = LibOpenCC.opencc_open DICTS[dicttype.to_sym]
    ptr = LibOpenCC.opencc_convert_utf8(od, str, str.bytesize)
    out_str = ptr.read_string
    LibC.free ptr
    LibOpenCC.opencc_close od
    out_str.force_encoding("UTF-8") if out_str.respond_to?(:force_encoding)
    out_str

end

.open(*args) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ropencc.rb', line 41

def self.open(*args)
    cc = new(*args)

    return cc unless block_given?

    begin
        yield cc
    ensure
        begin
            cc.close unless cc.closed?
        rescue StandardError
            # nothing, just swallow them
        end
    end
end

Instance Method Details

#closeObject



86
87
88
89
90
91
92
# File 'lib/ropencc.rb', line 86

def close
    LibOpenCC.opencc_close @descriptor
    @descriptor = nil
    @is_open = false

    return nil
end

#closed?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/ropencc.rb', line 82

def closed?
    @is_open == false
end

#convert(str) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ropencc.rb', line 69

def convert(str)
    if @is_open == false
        raise IOError, 'not opened for conversion'
    end

    ptr = LibOpenCC.opencc_convert_utf8(@descriptor, str, str.bytesize)
    out_str = ptr.read_string
    LibC.free ptr
    out_str.force_encoding("UTF-8") if out_str.respond_to?(:force_encoding)
    out_str
end