/*<<<***************************************************************************
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: xbString
#Project: xdcBase
////////////////////////////////////////////////////////////////////////////////
#Author: Marc MIRTI MANCINELLI
#History:
<17/08/2013: creation.
12/02/2014: this notice inserted.
***************************************************************************>>>*/
#include "stdafx.h"

#define _xbSTRING_INTERNAL
#include "xbString.h"
#include "xbXmlItf.h"

#define xbSTRING_DEFW
#include "xbString.cpp.inl"
#undef xbSTRING_DEFW

#define xbSTRING_DEFA
#include "xbString.cpp.inl"
#undef xbSTRING_DEFA

BSTR xbStringA::AllocSysString(ulong cp) const
{
	int nLen = ::MultiByteToWideChar(cp, MB_ERR_INVALID_CHARS, *this,
		GetLength(), NULL, NULL);
	if (nLen <= 0)
	{
		if (IsValid() || nLen < 0)
			xbThrow xbEsys(xbHR_LAST, "string conversion api failure - AllocSysString()");
	}

	BSTR bstr = ::SysAllocStringLen(NULL, nLen);
	if (bstr == NULL)
		xbThrowEmem("xbStringA::AllocSysString failure");
	
	if (nLen > 0 && MultiByteToWideChar(cp, 0, *this, GetLength(),bstr, nLen) <= 0)
		xbThrow xbEsys(xbHR_LAST, "string conversion api failure - AllocSysString(a)");

	return bstr;
}

BSTR xbStringA::SetSysString(BSTR* pbstr,ulong cp) const
{
	int nLen = MultiByteToWideChar(cp, MB_ERR_INVALID_CHARS, *this,
		GetLength(), NULL, NULL);
	if (nLen <= 0)
	{
		if (IsValid() || nLen < 0)
			xbThrow xbEsys(xbHR_LAST, "string conversion api failure - SetSysString()");
	}
	if (!::SysReAllocStringLen(pbstr, NULL, nLen))
		xbThrowEmem("xbStringA::SetSysString failure");

	if (nLen > 0 && MultiByteToWideChar(cp, 0, *this, GetLength(),
		*pbstr, nLen)<=0)
		xbThrow xbEsys(xbHR_LAST, "string conversion api failure - SetSysString(a)");

	xbAssert(*pbstr != NULL);
	return *pbstr;
}

BSTR xbStringW::AllocSysString() const
{
	BSTR bstr = ::SysAllocStringLen(*this, GetLength());
	if (bstr == NULL)
		xbThrowEmem("xbStringA::AllocSysString failure");
	
	return bstr;
}

BSTR xbStringW::SetSysString(BSTR* pbstr) const
{
	if (!::SysReAllocStringLen(pbstr, *this, GetLength()))
		xbThrowEmem("xbStringA::SetSysString failure");

	xbAssert(*pbstr != NULL);
	return *pbstr;
}

xbStringA::xbStringA(ulong cp, LPCWSTR lpsz, int nLength)
{
	m_pData = NULL;
	::xbCPconvert(lpsz,nLength,cp,this);
}

xbStringA::xbStringA(ulong cp, LPCWSTR lpsz)
{
	m_pData = NULL;
	::xbCPconvert(lpsz,(int)xbStrlenW(lpsz),cp,this);
}

xbStringA::xbStringA(ulong cp, const xbStringW &str)
{
	m_pData = NULL;
	::xbCPconvert(str,str.Length,cp,this);
}


xbStringA::xbStringA(const xbStringW &str)
{
	m_pData = NULL;
	::xbCPconvert(str,str.Length,CP_ACP,this);
}

xbStringA::xbStringA(LPCWSTR lpsz, int nLength)
{
	m_pData = NULL;
	::xbCPconvert(lpsz,nLength,CP_ACP,this);
}

xbStringA::xbStringA(LPCWSTR lpsz)
{
	m_pData = NULL;
	::xbCPconvert(lpsz,(int)xbStrlenW(lpsz),CP_ACP,this);
}

xbStringW::xbStringW(const xbStringA &str)
{
	m_pData = NULL;
	::xbCPconvert(CP_ACP,str,this);
}

xbStringW::xbStringW(ulong cp,const xbStringA &str)
{
	m_pData = NULL;
	::xbCPconvert(cp,str,this);
}

xbStringW::xbStringW(LPCSTR lpsz)
{
	m_pData = NULL;
	::xbCPconvert(CP_ACP,lpsz,(int)xbStrlen(lpsz),this);
}

xbStringW::xbStringW(LPCSTR lpsz, int nLength)
{
	m_pData = NULL;
	::xbCPconvert(CP_ACP,lpsz,nLength,this);
}

xbStringW::xbStringW(ulong cp,LPCSTR lpsz)
{
	m_pData = NULL;
	::xbCPconvert(cp,lpsz,(int)xbStrlen(lpsz),this);
}

xbStringW::xbStringW(ulong cp,LPCSTR lpsz, int nLength)
{
	m_pData = NULL;
	::xbCPconvert(cp,lpsz,nLength,this);
}
