| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| void igs::resource::locale_to_jp(void) { setlocale(LC_CTYPE, "ja_JP.utf8"); } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| void igs::resource::locale_to_jp(void) { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if ( ::setlocale( LC_ALL ,"" ) == NULL ) { |
| throw std::domain_error( "Can not set locale." ); |
| } |
| |
| |
| if ( !::XSupportsLocale() ) { |
| std::string msg("X is not support locale "); |
| msg += setlocale( LC_ALL ,NULL ); |
| msg += ".\n"; |
| throw std::domain_error( msg.c_str() ); |
| } |
| |
| |
| |
| if ( ::XSetLocaleModifiers("") == NULL ) { |
| throw std::domain_error( "Can not set locale modifiers." ); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| void igs::resource::mbs_to_wcs(const std::string &mbs, std::wstring &wcs) { |
| size_t length = 0; |
| { |
| const char *src_ptr = mbs.c_str(); |
| mbstate_t ss; |
| ::memset(&ss, 0, sizeof(ss)); |
| length = ::mbsrtowcs(NULL, &src_ptr, 0, &ss); |
| if (length == (size_t)(-1)) { |
| throw std::domain_error( |
| "mbstowcs(-) got bad multi byte character,when size"); |
| } |
| if (length <= 0) { |
| return; |
| } |
| ++length; |
| } |
| |
| wcs.resize(length); |
| { |
| const char *src_ptr = mbs.c_str(); |
| mbstate_t ss; |
| ::memset(&ss, 0, sizeof(ss)); |
| |
| length = |
| ::mbsrtowcs(const_cast<wchar_t *>(wcs.c_str()), &src_ptr, length, &ss); |
| if (length == (size_t)(-1)) { |
| throw std::domain_error( |
| "mbstowcs(-) got bad multi byte character,when conv"); |
| } |
| if (length <= 0) { |
| throw std::domain_error("mbstowcs(-) got zero or under equal -2 "); |
| } |
| } |
| |
| wcs.erase(wcs.end() - 1); |
| } |
| |
| void igs::resource::wcs_to_mbs(const std::wstring &wcs, std::string &mbs) { |
| size_t length = 0; |
| { |
| const wchar_t *src_ptr = wcs.c_str(); |
| mbstate_t ss; |
| ::memset(&ss, 0, sizeof(ss)); |
| length = ::wcsrtombs(NULL, &src_ptr, 0, &ss); |
| if (length <= 0) { |
| return; |
| } |
| ++length; |
| } |
| |
| mbs.resize(length); |
| { |
| const wchar_t *src_ptr = wcs.c_str(); |
| mbstate_t ss; |
| ::memset(&ss, 0, sizeof(ss)); |
| |
| length = |
| ::wcsrtombs(const_cast<char *>(mbs.c_str()), &src_ptr, length, &ss); |
| if (length <= 0) { |
| throw std::domain_error("wcstombs(-) got bad wide character"); |
| } |
| } |
| |
| mbs.erase(mbs.end() - 1); |
| } |
| |
| const std::basic_string<TCHAR> igs::resource::ts_from_mbs( |
| const std::string &mbs) { |
| |
| std::wstring wcs; |
| igs::resource::mbs_to_wcs(mbs, wcs); |
| return wcs; |
| |
| |
| return mbs; |
| |
| } |
| |
| const std::string igs::resource::mbs_from_ts( |
| const std::basic_string<TCHAR> &ts) { |
| |
| std::string mbs; |
| igs::resource::wcs_to_mbs(ts, mbs); |
| return mbs; |
| |
| |
| return ts; |
| |
| } |
| |
| namespace { |
| const std::string iconv_to_from_(const std::string &text, const char *tocode, |
| const char *fromcode) { |
| iconv_t icd = ::iconv_open(tocode, fromcode); |
| if (reinterpret_cast<iconv_t>(-1) == icd) { |
| throw std::domain_error( |
| igs_resource_msg_from_err(TEXT("iconv_open(-)"), errno)); |
| } |
| |
| std::vector<char> dst(text.size() * 4); |
| char *inbuf = const_cast<char *>(text.c_str()); |
| char *outbuf = &dst.at(0); |
| size_t inbytesleft = text.size(); |
| size_t outbytesleft = dst.size(); |
| size_t ret = ::iconv(icd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); |
| *outbuf = '\0'; |
| |
| |
| |
| |
| |
| ret = dst.size() - outbytesleft; |
| if (ret <= 0) { |
| |
| ::iconv_close(icd); |
| |
| throw std::domain_error(igs_resource_msg_from_err(TEXT("iconv(-)"), errno)); |
| } |
| |
| if (-1 == ::iconv_close(icd)) { |
| throw std::domain_error( |
| igs_resource_msg_from_err(TEXT("iconv_close(-)"), errno)); |
| } |
| |
| std::string mbs(std::string(dst.begin(), dst.begin() + ret)); |
| return mbs; |
| } |
| } |
| const std::string igs::resource::utf8_from_cp932_mb(const std::string &text) { |
| return iconv_to_from_(text, "UTF-8", "CP932"); |
| } |
| const std::string igs::resource::cp932_from_utf8_mb(const std::string &text) { |
| return iconv_to_from_(text, "CP932", "UTF-8"); |
| } |
| |
| |
| |
| |
| |
| |
| const std::string igs::resource::msg_from_err_( |
| const std::basic_string<TCHAR> &tit, const int erno, |
| const std::string &file, const std::string &line, |
| const std::string &pretty_function, const std::string &comp_type, |
| const std::string &gnuc, const std::string &gnuc_minor, |
| const std::string &gnuc_patchlevel, const std::string &gnuc_rh_release, |
| const std::string &date, const std::string &time) { |
| std::string errmsg; |
| errmsg += '\"'; |
| |
| |
| std::string::size_type index = file.find_last_of("/\\"); |
| if (std::basic_string<TCHAR>::npos != index) { |
| errmsg += file.substr(index + 1); |
| } else { |
| errmsg += file; |
| } |
| |
| errmsg += ':'; |
| errmsg += line; |
| errmsg += ':'; |
| errmsg += comp_type; |
| errmsg += ':'; |
| errmsg += gnuc; |
| errmsg += '.'; |
| errmsg += gnuc_minor; |
| errmsg += '.'; |
| errmsg += gnuc_patchlevel; |
| errmsg += '-'; |
| errmsg += gnuc_rh_release; |
| { |
| std::istringstream ist(date); |
| std::string month, day, year; |
| ist >> month; |
| ist >> day; |
| ist >> year; |
| errmsg += ':'; |
| errmsg += year; |
| errmsg += ':'; |
| errmsg += month; |
| errmsg += ':'; |
| errmsg += day; |
| } |
| errmsg += ':'; |
| errmsg += time; |
| errmsg += '\"'; |
| errmsg += ' '; |
| errmsg += '\"'; |
| errmsg += pretty_function; |
| errmsg += '\"'; |
| errmsg += ' '; |
| errmsg += '\"'; |
| if (0 < tit.size()) { |
| errmsg += igs::resource::mbs_from_ts(tit); |
| } |
| if (0 != erno) { |
| errmsg += ':'; |
| |
| |
| |
| |
| |
| |
| errmsg += ::strerror(erno); |
| |
| |
| |
| |
| |
| char buff[4096]; |
| const int ret = ::strerror_r(erno, buff, sizeof(buff)); |
| if (0 == ret) { |
| errmsg += buff; |
| } else if (-1 == ret) { |
| swtich(errno) { |
| case EINVAL: |
| errmsg += |
| "strerror_r() gets Error : The value of errnum is not a " |
| "valid error number."; |
| |
| break; |
| case ERANGE: |
| errmsg += |
| "strerror_r() gets Error : Insufficient storage was " |
| "supplied via strerrbuf and buflen to contain the " |
| "generated message string."; |
| |
| |
| break; |
| deatult: |
| errmsg += "strerror_r() gets Error and Returns bad errno"; |
| break; |
| } |
| } else { |
| errmsg += "strerror_r() returns bad value"; |
| } |
| |
| char buff[4096]; |
| int ret = ::strerror_r(erno, buff, sizeof(buff)); |
| if (!ret) { |
| errmsg += buff; |
| } |
| |
| |
| |
| |
| |
| |
| char buff[4096]; |
| const char *ret = ::strerror_r(erno, buff, sizeof(buff)); |
| errmsg += ret; |
| |
| } |
| errmsg += '\"'; |
| return errmsg; |
| } |