33 using result = codecvt_base::result;
34 using int_type = char;
35 using ext_type = char;
36 using state_type = __gnu_cxx::encoding_state;
37 using ccutf8_codecvt = codecvt<int_type, ext_type, state_type>;
40 const int_type* internal_ptr;
41 ext_type* external_ptr;
42 ext_type* buf =
new int_type[
in.size() * 3];
47 state_type state(
"ISO-8859-16",
"UTF-8", 0, 0);
49 ccutf8_codecvt* cvtf8 =
new ccutf8_codecvt;
50 locale loc(locale::classic(), cvtf8);
51 const ccutf8_codecvt& cvt = use_facet<ccutf8_codecvt>(loc);
52 result r = cvt.out(state,
in.c_str(),
in.c_str() +
in.size(), internal_ptr,
53 buf, buf +
in.size() * 3, external_ptr);
54 if (r == codecvt_base::ok)
55 out = string(buf, external_ptr);
58 if (r == codecvt_base::partial)
60 ext_type* n_ptr = external_ptr;
61 while (n_ptr > external_ptr)
64 r = cvt.in(state,
in.c_str(),
in.c_str() +
in.size(),
65 internal_ptr, buf, buf +
in.size() * 3, n_ptr);
68 if (r == codecvt_base::ok)
69 out = string(buf, n_ptr);
72 std::cerr <<
"convert_to_utf8: partial at "
73 <<
ulong(n_ptr -
in.c_str()) <<
" "
78 if (r == codecvt_base::error)
80 std::cerr <<
"convert_to_utf8: error " <<
in << std::endl;