@@ -11,24 +11,17 @@ const E2BIG = 7
11
11
const EINVAL = 22
12
12
const EILSEQ = 84
13
13
14
- type IConv
15
- p:: Ptr{Void}
16
- end
17
-
18
- function iconv_close (cd:: IConv )
19
- cd. p != C_NULL || return
20
- ret = ccall ((:iconv_close , :libc ), Cint, (Ptr{Void},), cd. p)
21
- ret == 0 || error (" failed to call iconv_close: error $(errno ()) ($(strerror (errno ())) )" )
22
- cd. p = C_NULL
23
- nothing
14
+ function iconv_close (cd:: Ptr{Void} )
15
+ if cd != C_NULL
16
+ ccall ((:iconv_close , :libc ), Cint, (Ptr{Void},), cd) == 0 ||
17
+ error (" failed to call iconv_close: error $(errno ()) ($(strerror (errno ())) )" )
18
+ end
24
19
end
25
20
26
21
function iconv_open (tocode, fromcode)
27
22
p = ccall ((:iconv_open , :libc ), Ptr{Void}, (Cstring, Cstring), tocode, fromcode)
28
23
if p != Ptr {Void} (- 1 )
29
- obj = IConv (p)
30
- finalizer (obj, iconv_close)
31
- return obj
24
+ return p
32
25
elseif errno () == EINVAL
33
26
error (" conversion from $fromcode to $tocode not supported by iconv implementation, check that specified encodings are correct" )
34
27
else
@@ -43,7 +36,7 @@ const BUFSIZE = 100
43
36
44
37
type StringEncoder{S<: IO } <: IO
45
38
ostream:: S
46
- cd:: IConv
39
+ cd:: Ptr{Void}
47
40
inbuf:: Vector{UInt8}
48
41
outbuf:: Vector{UInt8}
49
42
inbytesleft:: Ref{Csize_t}
52
45
53
46
type StringDecoder{S<: IO } <: IO
54
47
istream:: S
55
- cd:: IConv
48
+ cd:: Ptr{Void}
56
49
inbuf:: Vector{UInt8}
57
50
outbuf:: Vector{UInt8}
58
51
inbytesleft:: Ref{Csize_t}
59
52
outbytesleft:: Ref{Csize_t}
60
53
skip:: Int
61
54
end
62
55
63
- function iconv! (cd:: IConv , inbuf:: Vector{UInt8} , outbuf:: Vector{UInt8} ,
56
+ function iconv! (cd:: Ptr{Void} , inbuf:: Vector{UInt8} , outbuf:: Vector{UInt8} ,
64
57
inbytesleft:: Ref{Csize_t} , outbytesleft:: Ref{Csize_t} )
65
58
inbuf2_orig = pointer (inbuf, 1 )
66
59
outbuf2_orig = pointer (outbuf, 1 )
@@ -73,7 +66,7 @@ function iconv!(cd::IConv, inbuf::Vector{UInt8}, outbuf::Vector{UInt8},
73
66
74
67
ret = ccall ((:iconv , :libc ), Csize_t,
75
68
(Ptr{Void}, Ptr{Ptr{UInt8}}, Ref{Csize_t}, Ptr{Ptr{UInt8}}, Ref{Csize_t}),
76
- cd. p , pointer (inbuf2, 1 ), inbytesleft, pointer (outbuf2, 1 ), outbytesleft)
69
+ cd, pointer (inbuf2, 1 ), inbytesleft, pointer (outbuf2, 1 ), outbytesleft)
77
70
78
71
if ret == reinterpret (Csize_t, - 1 )
79
72
err = errno ()
99
92
# Reset iconv to initial state
100
93
# Returns the number of bytes written into the output buffer, if any
101
94
function iconv_reset! (s:: Union{StringEncoder, StringDecoder} )
102
- s. cd. p != C_NULL || return 0
95
+ s. cd == C_NULL && return 0
103
96
104
97
if is (s, StringDecoder)
105
98
s. skip = 0
@@ -109,7 +102,7 @@ function iconv_reset!(s::Union{StringEncoder, StringDecoder})
109
102
s. outbytesleft[] = BUFSIZE
110
103
ret = ccall ((:iconv , :libc ), Csize_t,
111
104
(Ptr{Void}, Ptr{Ptr{UInt8}}, Ref{Csize_t}, Ptr{Ptr{UInt8}}, Ref{Csize_t}),
112
- s. cd. p , C_NULL , C_NULL , pointer (outbuf2, 1 ), s. outbytesleft)
105
+ s. cd, C_NULL , C_NULL , pointer (outbuf2, 1 ), s. outbytesleft)
113
106
114
107
if ret == reinterpret (Csize_t, - 1 )
115
108
err = errno ()
149
142
# Flush input buffer and convert it into output buffer
150
143
# Returns the number of bytes written to output buffer
151
144
function flush (s:: StringEncoder )
152
- s. cd. p != C_NULL || return s
145
+ s. cd == C_NULL && return s
153
146
154
147
# We need to retry several times in case output buffer is too small to convert
155
148
# all of the input. Even so, some incomplete sequences may remain in the input
@@ -164,10 +157,11 @@ function flush(s::StringEncoder)
164
157
end
165
158
166
159
function close (s:: StringEncoder )
167
- s. cd. p != C_NULL || return s
160
+ s. cd == C_NULL && return s
168
161
flush (s)
169
162
iconv_reset! (s)
170
- finalize (s. cd)
163
+ iconv_close (s. cd)
164
+ s. cd = C_NULL
171
165
# flush() wasn't able to empty input buffer, which cannot happen with correct data
172
166
s. inbytesleft[] == 0 || error (" iconv error: incomplete byte sequence at end of input" )
173
167
end
199
193
# Fill input buffer and convert it into output buffer
200
194
# Returns the number of bytes written to output buffer
201
195
function fill_buffer! (s:: StringDecoder )
202
- s. cd. p != C_NULL || return 0
196
+ s. cd == C_NULL && return 0
203
197
204
198
s. skip = 0
205
199
@@ -225,8 +219,9 @@ function eof(s::StringDecoder)
225
219
end
226
220
227
221
function close (s:: StringDecoder )
228
- s. cd. p != C_NULL || return s
229
- finalize (s. cd)
222
+ s. cd == C_NULL && return s
223
+ iconv_close (s. cd)
224
+ s. cd = C_NULL
230
225
# fill_buffer!() wasn't able to empty input buffer, which cannot happen with correct data
231
226
s. inbytesleft[] == 0 || error (" iconv error: incomplete byte sequence at end of input" )
232
227
end
0 commit comments