| #include "igs_resource_msg_from_err.h" |
| #include <locale> |
| #include <stdexcept> |
| |
| |
| |
| |
| |
| void igs::resource::locale_to_jp(void) { |
| std::locale::global( |
| std::locale(std::locale(), "japanese", std::locale::ctype)); |
| } |
| |
| void igs::resource::mbs_to_wcs(const std::string &mbs, std::wstring &wcs, |
| const UINT code_page) { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| int length = ::MultiByteToWideChar(code_page, 0, mbs.c_str(), -1, 0, 0); |
| if (length <= 1) { |
| return; |
| } |
| |
| |
| |
| |
| |
| |
| wcs.resize(length); |
| length = ::MultiByteToWideChar(code_page, 0, mbs.c_str(), -1, |
| const_cast<LPWSTR>(wcs.data()), |
| static_cast<int>(wcs.size())); |
| if (0 == length) { |
| switch (::GetLastError()) { |
| case ERROR_INSUFFICIENT_BUFFER: |
| throw std::domain_error("MultiByteToWideChar():insufficient buffer"); |
| case ERROR_INVALID_FLAGS: |
| throw std::domain_error("MultiByteToWideChar():invalid flags"); |
| case ERROR_INVALID_PARAMETER: |
| throw std::domain_error("MultiByteToWideChar():invalid parameter"); |
| case ERROR_NO_UNICODE_TRANSLATION: |
| throw std::domain_error("MultiByteToWideChar():no unicode translation"); |
| } |
| } |
| |
| wcs.erase(wcs.end() - 1); |
| } |
| |
| void igs::resource::wcs_to_mbs(const std::wstring &wcs, std::string &mbs, |
| const UINT code_page) { |
| |
| int length = ::WideCharToMultiByte(code_page, 0, wcs.c_str(), -1, 0, 0, 0, 0); |
| if (length <= 1) { |
| return; |
| } |
| |
| |
| |
| |
| |
| |
| mbs.resize(length); |
| length = ::WideCharToMultiByte(code_page, 0, wcs.c_str(), -1, |
| const_cast<LPSTR>(mbs.data()), |
| static_cast<int>(mbs.size()), 0, NULL); |
| if (0 == length) { |
| switch (::GetLastError()) { |
| case ERROR_INSUFFICIENT_BUFFER: |
| throw std::domain_error("WideCharToMultiByte():insufficient buffer"); |
| case ERROR_INVALID_FLAGS: |
| throw std::domain_error("WideCharToMultiByte():invalid flags"); |
| case ERROR_INVALID_PARAMETER: |
| throw std::domain_error("WideCharToMultiByte():invalid parameter"); |
| } |
| } |
| |
| mbs.erase(mbs.end() - 1); |
| } |
| |
| const std::basic_string<TCHAR> igs::resource::ts_from_mbs( |
| const std::string &mbs, const UINT code_page) { |
| #if defined UNICODE |
| std::wstring wcs; |
| igs::resource::mbs_to_wcs(mbs, wcs, code_page); |
| return wcs; |
| #else |
| code_page; |
| |
| return mbs; |
| #endif |
| } |
| |
| const std::string igs::resource::mbs_from_ts(const std::basic_string<TCHAR> &ts, |
| const UINT code_page) { |
| #if defined UNICODE |
| std::string mbs; |
| igs::resource::wcs_to_mbs(ts, mbs, code_page); |
| return mbs; |
| #else |
| code_page; |
| |
| return ts; |
| #endif |
| } |
| |
| const std::string igs::resource::utf8_from_cp932_mb(const std::string &text) { |
| std::wstring wcs; |
| igs::resource::mbs_to_wcs(text, wcs); |
| std::string mbs; |
| igs::resource::wcs_to_mbs(wcs, mbs, CP_UTF8); |
| return mbs; |
| } |
| |
| const std::string igs::resource::cp932_from_utf8_mb(const std::string &text) { |
| std::wstring wcs; |
| igs::resource::mbs_to_wcs(text, wcs, CP_UTF8); |
| std::string mbs; |
| igs::resource::wcs_to_mbs(wcs, mbs, 932); |
| return mbs; |
| } |
| |
| #include <sstream> |
| const std::string igs::resource::msg_from_err_( |
| const std::basic_string<TCHAR> &tit, const DWORD error_message_id, |
| const std::basic_string<TCHAR> &file, const std::basic_string<TCHAR> &line, |
| const std::basic_string<TCHAR> &funcsig, |
| const std::basic_string<TCHAR> &comp_type, |
| const std::basic_string<TCHAR> &msc_full_ver, |
| const std::basic_string<TCHAR> &date, |
| const std::basic_string<TCHAR> &time) { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| std::basic_string<TCHAR> errmsg; |
| errmsg += TEXT('\"'); |
| |
| |
| |
| |
| std::basic_string<TCHAR>::size_type index = file.find_last_of(TEXT("/\\")); |
| if (std::basic_string<TCHAR>::npos != index) { |
| errmsg += file.substr(index + 1); |
| } else { |
| errmsg += file; |
| } |
| |
| errmsg += TEXT(':'); |
| errmsg += line; |
| errmsg += TEXT(':'); |
| errmsg += comp_type; |
| errmsg += TEXT(":"); |
| errmsg += msc_full_ver; |
| { |
| std::basic_istringstream<TCHAR> ist(date); |
| std::basic_string<TCHAR> month, day, year; |
| ist >> month; |
| ist >> day; |
| ist >> year; |
| errmsg += TEXT(':'); |
| errmsg += year; |
| errmsg += TEXT(':'); |
| errmsg += month; |
| errmsg += TEXT(':'); |
| errmsg += day; |
| } |
| errmsg += TEXT(':'); |
| errmsg += time; |
| errmsg += TEXT('\"'); |
| errmsg += TEXT(' '); |
| errmsg += TEXT('\"'); |
| errmsg += funcsig; |
| errmsg += TEXT('\"'); |
| errmsg += TEXT(' '); |
| errmsg += TEXT('\"'); |
| if (0 < tit.size()) { |
| errmsg += tit; |
| } |
| if (NO_ERROR != error_message_id) { |
| errmsg += TEXT(':'); |
| LPTSTR lpMsgBuf = 0; |
| if (0 < ::FormatMessage( |
| FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | |
| FORMAT_MESSAGE_IGNORE_INSERTS, |
| NULL, error_message_id, |
| MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT) |
| , |
| reinterpret_cast<LPTSTR>(&lpMsgBuf), 0, |
| NULL)) { |
| errmsg += lpMsgBuf; |
| ::LocalFree(lpMsgBuf); |
| std::string::size_type index = errmsg.find_first_of(TEXT("\r\n")); |
| if (std::string::npos != index) { |
| errmsg.erase(index); |
| } |
| } else { |
| errmsg += TEXT("FormatMessage() can not get (error)message"); |
| } |
| } |
| errmsg += TEXT('\"'); |
| |
| |
| return igs::resource::mbs_from_ts(errmsg); |
| } |