/*************************************************************************
 * <_xZkqqHx_iDhEiDeAiDeDiDiL_xHqqkZx_> UtilErr.cpp
 *
 * <_xZkqqHx_iLeAjEfM_xHqqkZx_> _xZkqqHx_iDeHiDiJiBfLiKnGjIeBiCmMiDgDiBfLiDiLiPfHiBeC_xHqqkZx_
 *
 * <_xZkqqHx_jHjKjHpA_xHqqkZx_> 99.02.22 Fukushiro M. _xZkqqHx_iNoMjAkM_xHqqkZx_
 *************************************************************************/

#include "stdafx.h"
#include "UtilErr.h"		// This header.

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

///////////////////////////////////////////////////////////////////////////////
// CFCMiscException _xZkqqHx_iDeOiDiJiDfIiDiBiDjDiDgPiKnGjAjEiBeC_xHqqkZx_

IMPLEMENT_DYNCREATE(CFCMiscException, CException)

/*************************************************************************
 * <_xZkqqHx_iKnGjAjE_xHqqkZx_>	CFCMiscException::CFCMiscException
 *
 * <_xZkqqHx_iLeAjEfM_xHqqkZx_>	_xZkqqHx_iDfCiDjDiDfIiDgHiDiJiDeOiDfOiBeC_xHqqkZx_
 *
 * <_xZkqqHx_iJpAjAoA_xHqqkZx_>	_xZkqqHx_iDgGiDhEiDeIiDiLiDgHiDfCiDjDiDfIiDgHiDiJiDeOiDfOiBeC_xHqqkZx_CException _xZkqqHx_iCpAiMhAiPlDiClHiCoJiClNiCnPiCmJjFeLjHhGiBeC_xHqqkZx_
 *
 * <_xZkqqHx_jHjKjHpA_xHqqkZx_>	03.03.29 Fukushiro M. _xZkqqHx_iNoMjAkM_xHqqkZx_
 *************************************************************************/
CFCMiscException::CFCMiscException ()
					:CException(TRUE)
{
	m_dwMessageId = DWORD(-1);
} // CFCMiscException::CFCMiscException.

/*************************************************************************
 * <_xZkqqHx_iKnGjAjE_xHqqkZx_>	CFCMiscException::GetErrorMessage
 *
 * <_xZkqqHx_iLeAjEfM_xHqqkZx_>	_xZkqqHx_jEkNjAlGiClFiClNiDeHiDiJiB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iClJiCpBiBeC_xHqqkZx_
 *
 * <_xZkqqHx_jFnEjCgM_xHqqkZx_>	_xZkqqHx_jAlDiPoNiPeJjHlJiClFiClNiPoKiNiHiCmN_xHqqkZx_ 0 _xZkqqHx_iImIiKePiCpAjFnEiClFiCnMiClHiBeC_xHqqkZx_
 *
 * <_xZkqqHx_jCiNiInD_xHqqkZx_>	GetErrorMessage _xZkqqHx_iCmNiDgPiDgCiDhEiDeAiCmJ_xHqqkZx_ nMaxError -1 _xZkqqHx_jFlGiOjKiCpAiJhKiCkGiCoJjFlGiOjKjHpBiCpA_xHqqkZx_
 *			_xZkqqHx_iDfCiDhDiBfLiClFiCnMiClJiCpBiBeCjFlGiOjKjHpBiCmMiNmFiMoDiCmJiCmNiPoNiCmJ_xHqqkZx_ NULL _xZkqqHx_jFlGiOjKiCpAjFhEiJmBiClFiCnMiClHiBeC_xHqqkZx_
 *			_xZkqqHx_iDgPiDgCiDhEiDeAiCkKiPkMiClDiClHiCkMiCoJiCmGiCkLiCmNiBeBjFlGiOjKjHpBiCmNjAnIiCoIiLgMiCnPiCoHiCoKiCnMiClHiBeC_xHqqkZx_
 *
 * <_xZkqqHx_jHjKjHpA_xHqqkZx_>	00.04.24 Fukushiro M. _xZkqqHx_iNoMjAkM_xHqqkZx_
 *************************************************************************/
BOOL CFCMiscException::GetErrorMessage (LPTSTR lpszError, UINT nMaxError, PUINT pnHelpContext)
{
	CString strMsg = WStrToTStr(m_strMsg);
	// _xZkqqHx_jFlGiOjKjHpBjClHiBgJiPeJjCfLiCpAiKnMiCnOiBgKiCpAjAnNjCoIiBeC_xHqqkZx_
	int nMaxLen = strMsg.GetLength() + 1;
	if (nMaxError < nMaxLen) nMaxLen = nMaxError;
	// _xZkqqHx_iPeJjCfLjFkKiCpAiMlIiCoHiClHiBeC_xHqqkZx_
	nMaxLen--;
	// _xZkqqHx_iDiBiDgCiDfKiBfLiDfHiCpAiDfCiDhDiBfLiBeC_xHqqkZx_
	_tcsncpy(lpszError, strMsg, nMaxLen);
	lpszError[nMaxLen] = _T('\0');
	// _xZkqqHx_iDhHiDiLiDhGiDfCiDjDiDgFiDeMiDfIiDgH_xHqqkZx_ID_xZkqqHx_iCmJ_xHqqkZx_0_xZkqqHx_iCpAjAnNjCoIiBeC_xHqqkZx_
	if (pnHelpContext != NULL) *pnHelpContext = 0;
	return TRUE;
} // CFCMiscException::GetErrorMessage.

/*************************************************************************
 * <_xZkqqHx_iKnGjAjE_xHqqkZx_>	CFCMiscException::GetMessageId
 *
 * <_xZkqqHx_iLeAjEfM_xHqqkZx_>	_xZkqqHx_iDeHiDiJiBfLiDiBiDgCiDfKiBfLiDfHiCmM_xHqqkZx_ID_xZkqqHx_iCpAjFnEiClHiBeC_xHqqkZx_
 *
 * <_xZkqqHx_jFnEjCgM_xHqqkZx_>	_xZkqqHx_iDeHiDiJiBfLiDiBiDgCiDfKiBfLiDfHiCmM_xHqqkZx_ID_xZkqqHx_iBeC_xHqqkZx_
 *
 * <_xZkqqHx_jHjKjHpA_xHqqkZx_>	03.06.27 Fukushiro M. _xZkqqHx_iNoMjAkM_xHqqkZx_
 *************************************************************************/
DWORD CFCMiscException::GetMessageId () const
{
	return m_dwMessageId;
} // CFCMiscException::GetMessageId.

//////////////////////////////////////////////////////////////////////////

/*************************************************************************
 * <_xZkqqHx_iKnGjAjE_xHqqkZx_>	FFThrowFileException
 *
 * <_xZkqqHx_iLeAjEfM_xHqqkZx_>	FileException_xZkqqHx_iCpAiDfIiDiNiBfLiClHiCoJiBeC_xHqqkZx_
 *
 * <_xZkqqHx_iIpIjAjE_xHqqkZx_>	AfxThrowFileException_xZkqqHx_iCmMiIpIjAjEiCmGjDkPiClGiBeC_xHqqkZx_UNICODE_xZkqqHx_jEmFiBeC_xHqqkZx_
 *
 * <_xZkqqHx_jHjKjHpA_xHqqkZx_>	02.02.10 Fukushiro M. _xZkqqHx_iNoMjAkM_xHqqkZx_
 *************************************************************************/
void FFThrowFileException (int cause, LONG lOsError, const wchar_t* lpszFileName)
{
	AfxThrowFileException(cause, lOsError, WStrToTStr(lpszFileName));
} // FFThrowFileException.

/*************************************************************************
 * <_xZkqqHx_iKnGjAjE_xHqqkZx_>	FFThrowMiscException
 *
 * <_xZkqqHx_iLeAjEfM_xHqqkZx_>	CFCMiscException_xZkqqHx_iCpAiDfIiDiNiBfLiClHiCoJiBeC_xHqqkZx_
 *
 * <_xZkqqHx_iIpIjAjE_xHqqkZx_>	dwMessageId	:_xZkqqHx_iDfIiDgHiDiKiDjDiDePiDiKiDfMiBfLiDfIiCmM_xHqqkZx_ID_xZkqqHx_iCpAiOhHjCoIiBeCiDhEiDeIiBfLiDhNiDgCiDgHiCpAiOhHjCoIiBeC_xHqqkZx_
 *			...			:dwMessageId_xZkqqHx_iCmFiOhHjCoIiClDiCoKiCoJiDhEiDeIiBfLiDhNiDgCiDgHiCmMiIpIjAjEiCpAiOhHjCoIiBeC_xHqqkZx_
 *
 * <_xZkqqHx_jHjKjHpA_xHqqkZx_>	03.07.03 Fukushiro M. _xZkqqHx_iNoMjAkM_xHqqkZx_
 *************************************************************************/
void FFThrowMiscException (DWORD dwMessageId, ...)
{
//	AfxDebugBreak();
	CFCMiscException* pMisc = new CFCMiscException();
	pMisc->m_dwMessageId = dwMessageId;
	//----- _xZkqqHx_iDeHiDiJiBfLiDiBiDgCiDfKiBfLiDfHiCmMjFlGiOjKjHpBiCpAjBgHiCnNjHkHiCmEiCoJ_xHqqkZx_ -----
	WString wstrFormat;
	(void)wstrFormat.LoadString(dwMessageId);
	va_list argList;
	va_start(argList, dwMessageId);
	pMisc->m_strMsg.FormatV(wstrFormat.c_str(), argList);
	va_end(argList);
	throw pMisc;
} // FFThrowMiscException.

/*************************************************************************
 * <_xZkqqHx_iKnGjAjE_xHqqkZx_>	FFThrowMiscLastError
 *
 * <_xZkqqHx_iLeAjEfM_xHqqkZx_>	GetLastError_xZkqqHx_iKnGjAjEiCkKjFnEiClHiDeHiDiJiBfLiCpAiBeB_xHqqkZx_CFCMiscException_xZkqqHx_jHoBiKePiCmGiClFiCmE_xHqqkZx_
 *			_xZkqqHx_iDfIiDiNiBfLiClHiCoJiBeC_xHqqkZx_
 *
 * <_xZkqqHx_jHjKjHpA_xHqqkZx_>	05.11.15 Fukushiro M. _xZkqqHx_iNoMjAkM_xHqqkZx_
 *************************************************************************/
void FFThrowMiscLastError ()
{
	AfxDebugBreak();
	// GetLastError _xZkqqHx_iCkKjFnEiClHiDfCiBfLiDgIiCpAjFlGiOjKjHpBiJlLiClHiCoJiBeC_xHqqkZx_
	LPTSTR lpMsgBuf;
	::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 
					FORMAT_MESSAGE_FROM_SYSTEM | 
					FORMAT_MESSAGE_IGNORE_INSERTS,
					NULL,
					GetLastError(),
					MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
					(LPTSTR)&lpMsgBuf,
					0,
					NULL);
	CFCMiscException* pMisc = new CFCMiscException();
	pMisc->m_strMsg = TStrToWStr(lpMsgBuf);
	// Free the buffer.
	LocalFree(lpMsgBuf);
	throw pMisc;
} // FFThrowMiscLastError.

void FFDebugTrace (const wchar_t* wcpFormat, ...)
{
	WString wstrMsg;
	va_list argList;
	va_start(argList, wcpFormat);
	wstrMsg.FormatV(wcpFormat, argList);
	va_end(argList);
	OutputDebugString(WStrToTStr(wstrMsg));
} // FFDebugTrace.
