我想codecvt
使用 ICU 实现一个方面,以便在内部从任何字符编码(ICU 支持)转换为 UTF-8。我知道它codecvt_byname
存在并且它可以用来做我想要的一部分,如本例所示。该示例的问题在于它(1)使用宽字符流(我想使用“常规”、面向字节的流)和(2)需要 2 个流来执行转换。相反,我想要一个像这样的流:
locale loc( locale(), new icu_codecvt( "ISO-8859-1" ) );
ifstream ifs;
ifs.imbue( loc );
ifs.open( "/path/to/some/file.txt" );
// data read from ifs here will have been converted from ISO-8859-1 to UTF-8
因此,我想做这样的实现,但使用 ICU 而不是iconv
. 鉴于此,我的实现do_in()
是:
icu_codecvt::result icu_codecvt::do_in( state_type &state,
extern_type const *from, extern_type const *from_end,
extern_type const *&from_next, intern_type *to,
intern_type *to_end, intern_type *&to_next ) const {
from_next = from;
to_next = to;
if ( always_noconv_ )
return noconv;
our_state *const s = state_store_.get( state );
UErrorCode err = U_ZERO_ERROR;
ucnv_convertEx(
s->utf8_conv_, s->extern_conv_, &to_next, to_end, &from_next, from_end,
nullptr, nullptr, nullptr, nullptr, false, false, &err
);
if ( err == U_TRUNCATED_CHAR_FOUND )
return partial;
return U_SUCCESS( err ) ? ok : error;
}
该our_state
对象维护两个UConverter*
指针,一个用于“外部”编码(在本例中为 ISO-8859-1),另一个用于 UTF-8 编码。
我的问题是:
- 我应该
nullptr
如上所述指定“枢轴”缓冲区,还是提供我自己的? - 我不确定何时(如果有的话)应该将
reset
参数(目前是false
上面的第一个)设置为true
. - 目前尚不清楚我如何知道何时将
flush
参数(当前是false
上面的第二个)设置为true
,即,我如何知道何时到达输入的末尾。
一点帮助?