2020-05-23 02:17:41 +12:00
|
|
|
#define _XOPEN_SOURCE 700 /* isascii() */
|
|
|
|
#undef _GNU_SOURCE /* int strerror_r() on glibc */
|
2016-03-19 06:57:51 +13:00
|
|
|
#include <cerrno>
|
2016-06-15 18:43:10 +12:00
|
|
|
#include <cstring> /* memset */
|
2016-03-19 06:57:51 +13:00
|
|
|
#include <vector>
|
2016-06-15 18:43:10 +12:00
|
|
|
#include <stdexcept> // std::domain_error(-)
|
2016-03-19 06:57:51 +13:00
|
|
|
#include <locale>
|
|
|
|
#include "igs_resource_msg_from_err.h"
|
|
|
|
|
|
|
|
|
2020-07-06 17:32:52 +12:00
|
|
|
#if defined UNICODE
|
2016-03-19 06:57:51 +13:00
|
|
|
/*------ ワイド文字文字列 --> マルチバイト文字列 ------*/
|
2020-07-06 17:32:52 +12:00
|
|
|
static void wcs_to_mbs(const std::wstring &wcs, std::string &mbs) {
|
2016-06-15 18:43:10 +12:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
// std::vector<char> dst(length);
|
|
|
|
mbs.resize(length);
|
|
|
|
{
|
|
|
|
const wchar_t *src_ptr = wcs.c_str();
|
|
|
|
mbstate_t ss;
|
|
|
|
::memset(&ss, 0, sizeof(ss));
|
|
|
|
// length = ::wcsrtombs(&dst.at(0) ,&src_ptr ,length ,&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 = std::string(dst.begin() ,dst.end()-1);/* 終端以外を */
|
|
|
|
mbs.erase(mbs.end() - 1); /* 終端文字を消す */
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/*------ UNICODE宣言ならワイド文字文字列をマルチバイト文字列に変換 ------*/
|
2020-07-06 17:32:52 +12:00
|
|
|
const static std::string mbs_from_ts(
|
2016-06-15 18:43:10 +12:00
|
|
|
const std::basic_string<TCHAR> &ts) {
|
2016-03-19 06:57:51 +13:00
|
|
|
#if defined UNICODE
|
2016-06-15 18:43:10 +12:00
|
|
|
std::string mbs;
|
2020-07-06 17:32:52 +12:00
|
|
|
wcs_to_mbs(ts, mbs);
|
2016-06-15 18:43:10 +12:00
|
|
|
return mbs;
|
2016-03-19 06:57:51 +13:00
|
|
|
#else
|
2016-06-15 18:43:10 +12:00
|
|
|
/* MBCSの場合のsize()は文字数ではなくchar(byte)数,2bytes文字は2 */
|
|
|
|
return ts;
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*------ エラーメッセージ表示の元関数、直接呼び出すことはしない ------*/
|
2016-06-15 18:43:10 +12:00
|
|
|
#include <cerrno> // errno
|
|
|
|
#include <cstring> // strerror_r()
|
|
|
|
#include <sstream> // std::istringstream
|
2016-03-19 06:57:51 +13:00
|
|
|
#include "igs_resource_msg_from_err.h"
|
|
|
|
|
|
|
|
const std::string igs::resource::msg_from_err_(
|
2016-06-15 18:43:10 +12:00
|
|
|
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 += '\"';
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
/* フルパスで入ってきた場合ファイル名だけにする */
|
|
|
|
std::string::size_type index = file.find_last_of("/\\");
|
|
|
|
if (std::basic_string<TCHAR>::npos != index) {
|
|
|
|
errmsg += file.substr(index + 1);
|
|
|
|
} else {
|
|
|
|
errmsg += file;
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
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()) {
|
2020-07-06 17:32:52 +12:00
|
|
|
errmsg += mbs_from_ts(tit);
|
2016-06-15 18:43:10 +12:00
|
|
|
}
|
|
|
|
if (0 != erno) {
|
|
|
|
errmsg += ':';
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
#if defined __HP_aCC
|
2016-06-15 18:43:10 +12:00
|
|
|
/*
|
|
|
|
HP-UX(v11.23)では、strerror_r()をサポートしない。
|
|
|
|
注意::strerror()はThread SafeではなくMulti Threadでは正常動作しない
|
|
|
|
*/
|
|
|
|
errmsg += ::strerror(erno);
|
2020-09-25 19:07:44 +12:00
|
|
|
#elif !defined(__APPLE__)
|
2016-06-15 18:43:10 +12:00
|
|
|
/*
|
|
|
|
http://japanese-linux-man-pages.coding-school.com/man/X_strerror_r-3
|
|
|
|
より、POSIX.1.2002で規定されたXSI準拠のバージョンのstrerror_r()
|
|
|
|
*/
|
|
|
|
char buff[4096];
|
|
|
|
const int ret = ::strerror_r(erno, buff, sizeof(buff));
|
|
|
|
if (0 == ret) {
|
|
|
|
errmsg += buff;
|
|
|
|
} else if (-1 == ret) {
|
2020-07-06 17:32:52 +12:00
|
|
|
switch(errno) {
|
2016-06-15 18:43:10 +12:00
|
|
|
case EINVAL:
|
|
|
|
errmsg +=
|
|
|
|
"strerror_r() gets Error : The value of errnum is not a "
|
|
|
|
"valid error number.";
|
|
|
|
/* errnum の値が有効なエラー番号ではない */
|
|
|
|
break;
|
|
|
|
case ERANGE:
|
|
|
|
errmsg +=
|
|
|
|
"strerror_r() gets Error : Insufficient storage was "
|
|
|
|
"supplied via strerrbuf and buflen to contain the "
|
|
|
|
"generated message string.";
|
|
|
|
/* エラーコードを説明する文字列のために、
|
|
|
|
充分な領域が確保できな かった */
|
|
|
|
break;
|
2020-07-06 17:32:52 +12:00
|
|
|
default:
|
2016-06-15 18:43:10 +12:00
|
|
|
errmsg += "strerror_r() gets Error and Returns bad errno";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errmsg += "strerror_r() returns bad value";
|
|
|
|
}
|
2020-09-25 19:07:44 +12:00
|
|
|
#else
|
2016-06-15 18:43:10 +12:00
|
|
|
char buff[4096];
|
|
|
|
int ret = ::strerror_r(erno, buff, sizeof(buff));
|
|
|
|
if (!ret) {
|
|
|
|
errmsg += buff;
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
2016-06-15 18:43:10 +12:00
|
|
|
}
|
|
|
|
errmsg += '\"';
|
|
|
|
return errmsg;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|