@@ -11,24 +11,17 @@ const E2BIG = 7
1111const EINVAL = 22
1212const EILSEQ = 84
1313
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
2419end
2520
2621function iconv_open (tocode, fromcode)
2722 p = ccall ((:iconv_open , :libc ), Ptr{Void}, (Cstring, Cstring), tocode, fromcode)
2823 if p != Ptr {Void} (- 1 )
29- obj = IConv (p)
30- finalizer (obj, iconv_close)
31- return obj
24+ return p
3225 elseif errno () == EINVAL
3326 error (" conversion from $fromcode to $tocode not supported by iconv implementation, check that specified encodings are correct" )
3427 else
@@ -43,7 +36,7 @@ const BUFSIZE = 100
4336
4437type StringEncoder{S<: IO } <: IO
4538 ostream:: S
46- cd:: IConv
39+ cd:: Ptr{Void}
4740 inbuf:: Vector{UInt8}
4841 outbuf:: Vector{UInt8}
4942 inbytesleft:: Ref{Csize_t}
5245
5346type StringDecoder{S<: IO } <: IO
5447 istream:: S
55- cd:: IConv
48+ cd:: Ptr{Void}
5649 inbuf:: Vector{UInt8}
5750 outbuf:: Vector{UInt8}
5851 inbytesleft:: Ref{Csize_t}
5952 outbytesleft:: Ref{Csize_t}
6053 skip:: Int
6154end
6255
63- function iconv! (cd:: IConv , inbuf:: Vector{UInt8} , outbuf:: Vector{UInt8} ,
56+ function iconv! (cd:: Ptr{Void} , inbuf:: Vector{UInt8} , outbuf:: Vector{UInt8} ,
6457 inbytesleft:: Ref{Csize_t} , outbytesleft:: Ref{Csize_t} )
6558 inbuf2_orig = pointer (inbuf, 1 )
6659 outbuf2_orig = pointer (outbuf, 1 )
@@ -73,7 +66,7 @@ function iconv!(cd::IConv, inbuf::Vector{UInt8}, outbuf::Vector{UInt8},
7366
7467 ret = ccall ((:iconv , :libc ), Csize_t,
7568 (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)
7770
7871 if ret == reinterpret (Csize_t, - 1 )
7972 err = errno ()
9992# Reset iconv to initial state
10093# Returns the number of bytes written into the output buffer, if any
10194function iconv_reset! (s:: Union{StringEncoder, StringDecoder} )
102- s. cd. p != C_NULL || return 0
95+ s. cd == C_NULL && return 0
10396
10497 if is (s, StringDecoder)
10598 s. skip = 0
@@ -109,7 +102,7 @@ function iconv_reset!(s::Union{StringEncoder, StringDecoder})
109102 s. outbytesleft[] = BUFSIZE
110103 ret = ccall ((:iconv , :libc ), Csize_t,
111104 (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)
113106
114107 if ret == reinterpret (Csize_t, - 1 )
115108 err = errno ()
149142# Flush input buffer and convert it into output buffer
150143# Returns the number of bytes written to output buffer
151144function flush (s:: StringEncoder )
152- s. cd. p != C_NULL || return s
145+ s. cd == C_NULL && return s
153146
154147 # We need to retry several times in case output buffer is too small to convert
155148 # all of the input. Even so, some incomplete sequences may remain in the input
@@ -164,10 +157,11 @@ function flush(s::StringEncoder)
164157end
165158
166159function close (s:: StringEncoder )
167- s. cd. p != C_NULL || return s
160+ s. cd == C_NULL && return s
168161 flush (s)
169162 iconv_reset! (s)
170- finalize (s. cd)
163+ iconv_close (s. cd)
164+ s. cd = C_NULL
171165 # flush() wasn't able to empty input buffer, which cannot happen with correct data
172166 s. inbytesleft[] == 0 || error (" iconv error: incomplete byte sequence at end of input" )
173167end
199193# Fill input buffer and convert it into output buffer
200194# Returns the number of bytes written to output buffer
201195function fill_buffer! (s:: StringDecoder )
202- s. cd. p != C_NULL || return 0
196+ s. cd == C_NULL && return 0
203197
204198 s. skip = 0
205199
@@ -225,8 +219,9 @@ function eof(s::StringDecoder)
225219end
226220
227221function 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
230225 # fill_buffer!() wasn't able to empty input buffer, which cannot happen with correct data
231226 s. inbytesleft[] == 0 || error (" iconv error: incomplete byte sequence at end of input" )
232227end
0 commit comments