Encoder: support iconv implementation (not optimized)
MODIFY
src/encoder.cpp
src/encoder.cpp
@@ -85,6 +85,9 @@ void Encoder::Init() {
_conv_runtime = conv_runtime;
_conv_storage = conv_storage;
+#else
+ _conv_runtime = const_cast<char*>("UTF-8");
+ _conv_storage = const_cast<char*>(_encoding.c_str());
#endif
}
@@ -124,6 +127,33 @@ void Encoder::Convert(std::string& str, void* conv_dst_void, void* conv_src_void
str.assign(_buffer.data(), dst_p);
return;
+#else
+ auto* conv_dst = reinterpret_cast<const char*>(conv_dst_void);
+ auto* conv_src = reinterpret_cast<const char*>(conv_src_void);
+ iconv_t cd = iconv_open(conv_dst, conv_src);
+ if (cd == (iconv_t)-1)
+ return;
+ char *src = &str.front();
+ size_t src_left = str.size();
+ size_t dst_size = str.size() * 5 + 10;
+ _buffer.resize(dst_size);
+ char *dst = _buffer.data();
+ size_t dst_left = dst_size;
+# ifdef ICONV_CONST
+ char ICONV_CONST *p = src;
+# else
+ char *p = src;
+# endif
+ char *q = dst;
+ size_t status = iconv(cd, &p, &src_left, &q, &dst_left);
+ iconv_close(cd);
+ if (status == (size_t) -1 || src_left > 0) {
+ str.clear();
+ return;
+ }
+ *q++ = '\0';
+ str.assign(dst, dst_size - dst_left);
+ return;
#endif
}

MODIFY
src/reader_lcf.cpp
src/reader_lcf.cpp
@@ -271,11 +271,7 @@ const std::string& LcfReader::GetError() {
}
void LcfReader::Encode(std::string& str) {
-#ifdef LCF_SUPPORT_ICU
encoder.Encode(str);
-#else
- str = ReaderUtil::Recode(str, encoder.GetEncoding(), "UTF-8");
-#endif
}
int LcfReader::IntSize(unsigned int x) {

MODIFY
src/writer_lcf.cpp
src/writer_lcf.cpp
@@ -127,13 +127,9 @@ bool LcfWriter::IsOk() const {
}
std::string LcfWriter::Decode(const std::string& str) {
-#ifdef LCF_SUPPORT_ICU
auto copy = str;
encoder.Decode(copy);
return copy;
-#else
- return ReaderUtil::Recode(str, "UTF-8", encoder.GetEncoding());
-#endif
}
#ifdef WORDS_BIGENDIAN

Encoder: support iconv implementation (not optimized)