/*<<<***************************************************************************
The MIT License (MIT)

Copyright(C) 2013 - 2015 Marc Mirti Mancinelli, Christophe Lardau and Philippe Pamart (xdcube.org)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
////////////////////////////////////////////////////////////////////////////////
#Title: xbODexceptionLow
#Project: xdcBase
////////////////////////////////////////////////////////////////////////////////
#Author: Marc MIRTI MANCINELLI
#History:
<04/09/2013: creation.
12/02/2014: this notice inserted.
***************************************************************************>>>*/
#include "stdafx.h"

#ifdef xbOS_MSWIN
//#include "xbODexceptionLow.h"
#include "../../xbExceptionLow.h"
#include "../../xbException.h"

xbHR	xbEbase::GetHR() const 
{
	if (SUCCEEDED(m_hr)) 
		return (m_hr == xbHR_OK?xbHR_FAIL:((m_hr&0x3FFFFFFF)|((unsigned long)(SEVERITY_ERROR)<<31)));
	return m_hr;
}

void xbEbase::freeHRmsg()
{
	if (m_pszHRmsg != NULL)
		::LocalFree( (HLOCAL)m_pszHRmsg );
	m_bHRmsgGet = false;
	m_pszHRmsg = NULL;
}

const char *xbStrFromHRlow(xbHR hr)
{
	LPSTR lpMsgBuf = NULL;
	if (::FormatMessage( 
		FORMAT_MESSAGE_ALLOCATE_BUFFER | 
		FORMAT_MESSAGE_FROM_SYSTEM | 
		FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL,
		hr,
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
		 (LPSTR)&lpMsgBuf,
		0,
		NULL ) == 0
		&& ::FormatMessage( 
		FORMAT_MESSAGE_ALLOCATE_BUFFER | 
		FORMAT_MESSAGE_FROM_SYSTEM | 
		FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL,
		ulong(hr) & 0x1fffffff,
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
		 (LPSTR)&lpMsgBuf,
		0,
		NULL ) == 0
		&& ::FormatMessage( 
		FORMAT_MESSAGE_ALLOCATE_BUFFER | 
		FORMAT_MESSAGE_FROM_SYSTEM | 
		FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL,
		HRESULT_CODE(hr),
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
		 (LPSTR)&lpMsgBuf,
		0,
		NULL ) == 0) 
		return NULL;
	if (!lpMsgBuf) 
		return NULL;
	char *ptr = lpMsgBuf + strlen(lpMsgBuf);
	while(ptr > lpMsgBuf)
	{
		ptr--;
		if (*ptr != ' ' && *ptr != '\n' && *ptr != '\r')
			break;
		*ptr = 0;
	}
	return lpMsgBuf;
}

const char *xbEbase::GetHRmsg() const
{
	if (!m_hr || m_hr == xbHR_FAIL)
		return NULL;
	if (xbIsHRmemory(m_hr))
		return "Out of memory";

	if (m_bHRmsgGet) 
		return m_pszHRmsg;
	m_bHRmsgGet = true;

	return m_pszHRmsg = xbStrFromHRlow(m_hr);
}

void xbEbase::TraceExcept(const char *pszType,HRESULT hr,const char *pszMsg,xbEctorMode ctorMode)
{
#if defined(_DEBUG) || defined(_RELDBG)
	if (xbIsHRmemory(hr) || !g_xbEngineCenter.m_heap.IsCreated())
		return;
	bool bCfgDebug = ctorMode != xbEctor_base && g_xbExceptionEngine.IsToDebug(pszType);
	bool bDebugBrk = xbIsDebuggerPresent() && g_xbExceptionEngine.IsToStop(pszType);
	if (ctorMode != xbEctor_base && (bCfgDebug || bDebugBrk))
		xbRelOutput("EXCEPTION '"+xbString(pszType)+"' THROWN ("+xbString(hr,xbStringNum_hexa)+") / "+xbString(pszMsg));
	if (bDebugBrk)
		::xbDebugBreak();
#endif
}

xbEXPORT xbString xbStrFromHR(xbHR hr)
{
	if (!hr || hr == xbHR_FAIL)
		return xbString();
	if (xbIsHRmemory(hr))
		return "Out of memory";

	xbString ret;
	const char *pMsg = xbStrFromHRlow(hr);
	if (!pMsg)
		return xbString();
	ret = pMsg;
	LocalFree( (HLOCAL)pMsg );
	return ret;
}

#endif
