エクスキューズする。C++ストリームは当然C++標準ライブラリの範疇で、まさかC言語標準ライブラリの関数によるC言語ロケール設定が必要とは想像もしなかった。以下が誤解に至った概略である。
この問題はC++標準入出力(wcin/wcout)(ただしC言語標準入出力との同期解除が前提)に限らないC++ファイルストリーム(wifstream/wofstream)一般で、wxWidgetsライブラリ利用のデスクトップアプリケーションがファイル入出力をwifstream/wofstreamで行う場合にも影響する。GNU gettextによる国際化機能が利用するwxLocaleクラスはInitメンバ関数からwxLanguageInfo::TrySetLocaleメンバ関数とwxSetlocale関数を経由してsetlocale関数をシステムロケール名でコールする。つまりwxWidgetsライブラリを利用する際、wxLocale国際化機能の有無で標準codecvtによるwifstream/wofstreamがASCII以外を変換できるかどうか、日本語環境であればユニコード文字列からシフトJISへ変換できるかどうかが変わる。
ソースコードでcodecvt<wchar_t,char,mbstate_t>のメンバ関数定義を確認する。do_outメンバ関数は内部ユニコード文字列を外部マルチバイト文字列に変換するためC言語標準ライブラリwcrtomb関数をコールする。do_inメンバ関数は外部マルチバイト文字列を内部ユニコード文字列に変換するためmbrtowc関数をコールする。
なお本記事の目的にはdo_out/do_inメンバ関数のみで十分であるが、自作codecvtとの比較に供するためフルソースコード(2022年1月3日コミット)を掲示する。
#include <locale>
#include <cstdlib>
#include <climits>
#include <cstring>
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
#ifdef _GLIBCXX_USE_WCHAR_T
codecvt_base::result
codecvt<wchar_t, char, mbstate_t>::
do_out(state_type& __state, const intern_type* __from,
const intern_type* __from_end, const intern_type*& __from_next,
extern_type* __to, extern_type* __to_end,
extern_type*& __to_next) const
{
result __ret = ok;
state_type __tmp_state(__state);
if (MB_CUR_MAX * (__from_end - __from) - (__to_end - __to) <= 0)
while (__from < __from_end)
{
const size_t __conv = wcrtomb(__to, *__from, &__tmp_state);
if (__conv == static_cast<size_t>(-1))
{
__ret = error;
break;
}
__state = __tmp_state;
__to += __conv;
__from++;
}
else
{
extern_type __buf[MB_LEN_MAX];
while (__from < __from_end && __to < __to_end)
{
const size_t __conv = wcrtomb(__buf, *__from, &__tmp_state);
if (__conv == static_cast<size_t>(-1))
{
__ret = error;
break;
}
else if (__conv > static_cast<size_t>(__to_end - __to))
{
__ret = partial;
break;
}
memcpy(__to, __buf, __conv);
__state = __tmp_state;
__to += __conv;
__from++;
}
}
if (__ret == ok && __from < __from_end)
__ret = partial;
__from_next = __from;
__to_next = __to;
return __ret;
}
codecvt_base::result
codecvt<wchar_t, char, mbstate_t>::
do_in(state_type& __state, const extern_type* __from,
const extern_type* __from_end, const extern_type*& __from_next,
intern_type* __to, intern_type* __to_end,
intern_type*& __to_next) const
{
result __ret = ok;
state_type __tmp_state(__state);
while (__from < __from_end && __to < __to_end)
{
size_t __conv = mbrtowc(__to, __from, __from_end - __from,
&__tmp_state);
if (__conv == static_cast<size_t>(-1))
{
__ret = error;
break;
}
else if (__conv == static_cast<size_t>(-2))
{
__ret = partial;
break;
}
else if (__conv == 0)
{
__conv = 1;
*__to = L'\0';
}
__state = __tmp_state;
__to++;
__from += __conv;
}
if (__ret == ok && __from < __from_end)
__ret = partial;
__from_next = __from;
__to_next = __to;
return __ret;
}
int
codecvt<wchar_t, char, mbstate_t>::
do_encoding() const throw()
{
int __ret = 0;
if (MB_CUR_MAX == 1)
__ret = 1;
return __ret;
}
int
codecvt<wchar_t, char, mbstate_t>::
do_max_length() const throw()
{
int __ret = MB_CUR_MAX;
return __ret;
}
int
codecvt<wchar_t, char, mbstate_t>::
do_length(state_type& __state, const extern_type* __from,
const extern_type* __end, size_t __max) const
{
int __ret = 0;
state_type __tmp_state(__state);
while (__from < __end && __max)
{
size_t __conv = mbrtowc(0, __from, __end - __from, &__tmp_state);
if (__conv == static_cast<size_t>(-1))
{
break;
}
else if (__conv == static_cast<size_t>(-2))
{
break;
}
else if (__conv == 0)
{
__conv = 1;
}
__state = __tmp_state;
__from += __conv;
__ret += __conv;
__max--;
}
return __ret;
}
#endif
_GLIBCXX_END_NAMESPACE_VERSION
}