1313#include " lcf/scope_guard.h"
1414#include < cstdio>
1515#include < cstdlib>
16- #include < exception>
1716
1817#if LCF_SUPPORT_ICU
1918# include < unicode/ucsdet.h>
2726#ifdef _WIN32
2827# include < windows.h>
2928#else
30- # if !LCF_SUPPORT_ICU
31- # include < iconv.h>
32- # endif
3329# include < locale>
3430#endif
3531
@@ -77,12 +73,12 @@ void Encoder::Decode(std::string& str) {
7773 Convert (str, _conv_storage, _conv_runtime);
7874}
7975
76+ #if LCF_SUPPORT_ICU
8077void Encoder::Init () {
8178 if (_encoding.empty ()) {
8279 return ;
8380 }
8481
85- #if LCF_SUPPORT_ICU
8682 auto code_page = atoi (_encoding.c_str ());
8783 const auto & storage_encoding = code_page > 0
8884 ? ReaderUtil::CodepageToEncoding (code_page)
@@ -110,27 +106,22 @@ void Encoder::Init() {
110106
111107 _conv_runtime = conv_runtime;
112108 _conv_storage = conv_storage;
113- #else
114- _conv_runtime = const_cast <char *>(" UTF-8" );
115- _conv_storage = const_cast <char *>(_encoding.c_str ());
116- #endif
117109}
118110
119111void Encoder::Reset () {
120- #if LCF_SUPPORT_ICU
121- auto * conv = reinterpret_cast <UConverter*>(_conv_runtime);
122- if (conv) ucnv_close (conv);
123- conv = reinterpret_cast <UConverter*>(_conv_storage);
124- if (conv) ucnv_close (conv);
125- #endif
126- }
112+ if (_conv_runtime) {
113+ ucnv_close (_conv_runtime);
114+ _conv_runtime = nullptr ;
115+ }
127116
117+ if (_conv_storage) {
118+ ucnv_close (_conv_storage);
119+ _conv_storage = nullptr ;
120+ }
121+ }
128122
129- void Encoder::Convert (std::string& str, void * conv_dst_void, void * conv_src_void) {
130- #if LCF_SUPPORT_ICU
123+ void Encoder::Convert (std::string& str, UConverter* conv_dst, UConverter* conv_src) {
131124 const auto & src = str;
132- auto * conv_dst = reinterpret_cast <UConverter*>(conv_dst_void);
133- auto * conv_src = reinterpret_cast <UConverter*>(conv_src_void);
134125
135126 auto status = U_ZERO_ERROR ;
136127 _buffer.resize (src.size () * 4 );
@@ -151,36 +142,8 @@ void Encoder::Convert(std::string& str, void* conv_dst_void, void* conv_src_void
151142 }
152143
153144 str.assign (_buffer.data (), dst_p);
154- return ;
155- #else
156- auto * conv_dst = reinterpret_cast <const char *>(conv_dst_void);
157- auto * conv_src = reinterpret_cast <const char *>(conv_src_void);
158- iconv_t cd = iconv_open (conv_dst, conv_src);
159- if (cd == (iconv_t )-1 )
160- return ;
161- char *src = &str.front ();
162- size_t src_left = str.size ();
163- size_t dst_size = str.size () * 5 + 10 ;
164- _buffer.resize (dst_size);
165- char *dst = _buffer.data ();
166- size_t dst_left = dst_size;
167- # ifdef ICONV_CONST
168- char ICONV_CONST *p = src;
169- # else
170- char *p = src;
171- # endif
172- char *q = dst;
173- size_t status = iconv (cd, &p, &src_left, &q, &dst_left);
174- iconv_close (cd);
175- if (status == (size_t ) -1 || src_left > 0 ) {
176- str.clear ();
177- return ;
178- }
179- *q++ = ' \0 ' ;
180- str.assign (dst, dst_size - dst_left);
181- return ;
182- #endif
183145}
146+ #endif
184147
185148} // namespace lcf
186149
0 commit comments