Linux 文字コード変換のコード
iconv を使用した方法
std::string SJIS_to_UTF8 (const std::string& sj_str) { tstring u8_str ; iconv_t icd = ::iconv_open("UTF8","Shift_JIS") ; if (icd == (iconv_t)-1) { u8_str = ::iconv_SJIS_UTF8(sj_str) ; } else { { size_t sj_size = sj_str.length() ; size_t u8_size = sj_str.length()*3 + 1024 ; u8_str.resize(u8_size) ; char* sj_ptr = (char*)(&sj_str[0]) ; char* u8_ptr = (char*)(&u8_str[0]) ; ::iconv(icd,&sj_ptr,&sj_size,&u8_ptr,&u8_size) ; } ::iconv_close(icd) ; } return u8_str.c_str() ; }
iconv,uconv の部分
#define cmd_iconv _T("iconv") // linux #define cmd_uconv _T("uconv") // DS116 bool exec_x_conv (const tstring& s_j_name,const tstring& u_8_name) { tstring sj_name = ::QuotM_Add_Auto(s_j_name) ; tstring u8_name = ::QuotM_Add_Auto(u_8_name) ; tstring param = _T(" -f sjis -t utf8 ") + sj_name + _T(" -o ") + u8_name ; if (::which( cmd_iconv)) { tstring exe_iconv = cmd_iconv _T(" ") + param ; _tsystem(exe_iconv.c_str()) ; return true ; } if (::which( cmd_uconv)) { tstring exe_uconv = cmd_uconv _T(" ") + param ; _tsystem(exe_uconv.c_str()) ; return true ; } return false ; } std::string iconv_SJIS_UTF8(const std::string& sj_str) { tstring tmp_path = ::Get_i_Tools_tmp_date() ; tstring now_str = ::Now_Format(_T("%M%S")) ; tstring s_j_name = ::Path_AddLastSP(tmp_path) + _T("sj_") + now_str + _T(".txt") ; tstring u_8_name = ::Path_AddLastSP(tmp_path) + _T("u8_") + now_str + _T(".txt") ; s_j_name = ::CreateUniqueEmpty(s_j_name.c_str()) ; u_8_name = ::CreateUniqueEmpty(u_8_name.c_str()) ; std::string u8_str ; { v_char v_c_u8 ; v_char v_c_sj = ::To_v_char(sj_str.c_str()) ; ::v_c_SaveText(s_j_name.c_str(),v_c_sj) ; { ::exec_x_conv(s_j_name,u_8_name) ; } v_c_u8 = ::v_c_Load (u_8_name.c_str()) ; u8_str = ::To_tstring( v_c_u8 ).c_str() ; } return u8_str.c_str() ; }
うまく置き換わっていない文字があった.
‘~'(0x7e) が ‘‾'(0x203e) になってしまっていた.
他にも ‘\'(0x5c) が ‘¥'(0xa5) .
http://ossforum.jp/jossfiles/Linux_SJIS_Support.pdf
幾つか違う文字があるようで,iconv の -f sjis を SJIS-WIN でうまくいった.
tstring i_param = _T(" -f SJIS-WIN -t utf8 ") + sj_name + _T(" -o ") + u8_name ; tstring u_param = _T(" -f sjis -t utf8 ") + sj_name + _T(" -o ") + u8_name ;
2020/04/30 ASUSTOR NAS に対応
text_gnc.hxx exec_ic.hxx