Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Linq-To-XML Style of Node Creation for C++

, 7 Jun 2012
Linq-To-XML Node Creation for Native C++
elmax-84498.zip
BuildProcessTemplates
ElmaxNet
Properties
Elmax
Elmax.vcxproj.user
StringUtils
TestNativeElmax
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax
Properties
TestNetElmax.csproj.user
TryoutNet
Properties
Settings.settings
Tryout
res
Tryout.ico
Tryout.vcxproj.user
elmax-ver083.zip
Elmax.vcxproj.user
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
Settings.settings
Tryout.ico
Tryout.vcxproj.user
elmax-ver084.zip
Elmax.vcxproj.user
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
Settings.settings
Tryout.ico
Tryout.vcxproj.user
elmax-ver085.zip
Elmax.vcxproj.user
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
Settings.settings
Tryout.ico
Tryout.vcxproj.user
elmax-ver086.zip
Elmax.vcxproj.user
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
Settings.settings
Tryout.ico
Tryout.vcxproj.user
elmax-ver087.zip
Elmax.vcxproj.user
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
Settings.settings
Tryout.ico
Tryout.vcxproj.user
Elmax-ver089.zip
Elmax-ver089
Elmax.vsmdi
Elmax
Elmax.vcxproj.user
StringUtils
ElmaxNet
Properties
Local.testsettings
TestNativeElmax
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax
Properties
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout
res
Tryout.ico
Tryout.vcxproj.user
TryoutNet
Properties
Settings.settings
ElmaxSrcVer082.zip
Elmax.vcxproj.user
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
Settings.settings
Tryout.ico
Tryout.vcxproj.user
#include "StdAfx.h"
#include "BaseConverter.h"
#include <climits>
#include <cfloat>
#include <sstream>
#include <iomanip>

using namespace Elmax;

BaseConverter::BaseConverter(void)
{
}

BaseConverter::~BaseConverter(void)
{
}

bool BaseConverter::SetBool(std::wstring& dest, bool val)
{
	if(val)
		dest = L"true";
	else
		dest = L"false";

	return true;
}

bool BaseConverter::SetChar(std::wstring& dest, char val)
{
	dest = val;
	return true;
}

bool BaseConverter::SetShort(std::wstring& dest, short val)
{
	const int size = 20;
	wchar_t buf[size];
	memset( buf, 0, sizeof(buf) );
	errno_t nRet = _itow_s( val, buf, size, 10 );

	if( 0 == nRet )
	{
		dest = buf;
		return true;
	}

	return false;
}

bool BaseConverter::SetInt32(std::wstring& dest, int val)
{
	const int size = 20;
	wchar_t buf[size];
	memset( buf, 0, sizeof(buf) );
	errno_t nRet = _itow_s( val, buf, size, 10 );

	if( 0 == nRet )
	{
		dest = buf;
		return true;
	}

	return false;
}

bool BaseConverter::SetInt64(std::wstring& dest, __int64 val)
{
	const int size = 30;
	wchar_t buf[size];
	memset( buf, 0, sizeof(buf) );
	errno_t nRet = _i64tow_s( val, buf, size, 10 );

	if( 0 == nRet )
	{
		dest = buf;
		return true;
	}

	return false;
}

bool BaseConverter::SetUChar(std::wstring& dest, unsigned char val)
{
	const int size = 20;
	wchar_t buf[size];
	memset( buf, 0, sizeof(buf) );
	errno_t nRet = _ultow_s( val, buf, size, 10 );

	if( 0 == nRet )
	{
		dest = buf;
		return true;
	}

	return false;
}

bool BaseConverter::SetUShort(std::wstring& dest, unsigned short val)
{
	const int size = 20;
	wchar_t buf[size];
	memset( buf, 0, sizeof(buf) );
	errno_t nRet = _ultow_s( val, buf, size, 10 );

	if( 0 == nRet )
	{
		dest = buf;
		return true;
	}

	return false;
}

bool BaseConverter::SetUInt32(std::wstring& dest, unsigned int val)
{
	const int size = 20;
	wchar_t buf[size];
	memset( buf, 0, sizeof(buf) );
	errno_t nRet = _ultow_s( val, buf, size, 10 );

	if( 0 == nRet )
	{
		dest = buf;
		return true;
	}

	return false;
}

bool BaseConverter::SetUInt64(std::wstring& dest, unsigned __int64 val)
{
	const int size = 30;
	wchar_t buf[size];
	memset( buf, 0, sizeof(buf) );
	errno_t nRet = _ui64tow_s( val, buf, size, 10 );

	if( 0 == nRet )
	{
		dest = buf;
		return true;
	}

	return false;
}

bool BaseConverter::SetFloat(std::wstring& dest, float val)
{
	const int size = 60;
	wchar_t buf[size];
	memset( buf, 0, sizeof(buf) );
	int nRet = swprintf_s( buf, size, L"%f", val );

	if( -1 != nRet )
	{
		dest = buf;
		return true;
	}

	return false;
}

bool BaseConverter::SetDouble(std::wstring& dest, double val)
{
	const int size = 60;
	wchar_t buf[size];
	memset( buf, 0, sizeof(buf) );
	int nRet = swprintf_s( buf, size, L"%lf", val );

	if( -1 != nRet )
	{
		dest = buf;
		return true;
	}

	return false;
}

bool BaseConverter::SetString(std::wstring& dest, const std::wstring& val)
{
	dest = val;
	return true;
}

bool BaseConverter::SetString(std::wstring& dest, const std::string& val)
{
	if(val.size()<=0)
	{
		dest = L"";
		return false;
	}

	const size_t size = val.size()+1;
	wchar_t* pbuf = new wchar_t[size];

	if( !pbuf )
	{
		dest = L"";
		return false;
	}

	memset( pbuf, 0, size*sizeof(wchar_t) );

	size_t nConverted=0;
	errno_t nRet = mbstowcs_s(
		&nConverted,
		pbuf,
		size,
		val.c_str(),
		_TRUNCATE );


	if( 0 == nRet )
		dest = pbuf;

	delete [] pbuf;

	return true;
}

bool BaseConverter::SetHex(std::wstring& dest, unsigned int val, bool bAddPrefix)
{
	const int size = 30;
	wchar_t buf[size];
	memset( buf, 0, sizeof(buf) );
	errno_t nRet = _ultow_s( val, buf, size, 16 );
	//int nRet = swprintf_s(buf, L"%X", val);

	std::wstring dest2 = buf;
	if(bAddPrefix)
	{
		dest2 = L"0x";
		dest2 += buf;
	}

	// Convert to upper case
	std::wstring dest3 = L"";
	for (size_t i = 0; i < dest2.size(); ++i)
	{
		if(dest2.at(i)==L'a')
			dest3 += L'A';
		else if(dest2.at(i)==L'b')
			dest3 += L'B';
		else if(dest2.at(i)==L'c')
			dest3 += L'C';
		else if(dest2.at(i)==L'd')
			dest3 += L'D';
		else if(dest2.at(i)==L'e')
			dest3 += L'E';
		else if(dest2.at(i)==L'f')
			dest3 += L'F';
		else
			dest3 += dest2.at(i);
	}

	if( 0 == nRet )
	{
		dest = dest3;
		return true;
	}

	return false;
}

bool BaseConverter::CheckHexString(const std::wstring& str)
{
	for(size_t i=0; i<str.size(); ++i)
	{
		wchar_t ch = str.at(i);
		if(ch==L'A'||ch==L'B'||ch==L'C'||ch==L'D'||ch==L'E'||ch==L'F')
			continue;
		if(ch==L'a'||ch==L'b'||ch==L'c'||ch==L'd'||ch==L'e'||ch==L'f')
			continue;
		if(ch==L'0'||ch==L'1'||ch==L'2'||ch==L'3'||ch==L'4'||ch==L'5'||ch==L'6'||ch==L'7'||ch==L'8'||ch==L'9')
			continue;
		if(i==1 && (ch==L'x' || ch==L'X') )
			continue;

		return false;
	}

	return true;
}

bool BaseConverter::GetBool(const std::wstring& src, bool defaultVal, bool& val)
{
	if(src.size()<=0)
	{
		val = defaultVal;
		return false;
	}

	std::wstring src2 = L"";
	for(size_t i=0; i<src.size(); ++i)
		src2 += towlower(src.at(i));

	if(src2==L"true"||src2==L"yes"||src2==L"1"||src2==L"ok")
		val = true;
	else if(src2==L"false"||src2==L"no"||src2==L"0"||src2==L"cancel")
		val = false;
	else
	{
		val = defaultVal;
		return false;
	}

	return true;
}

bool BaseConverter::GetChar(const std::wstring& src, char defaultVal, char& val)
{
	if(src.size()<=0||src.size()>1)
	{
		val = defaultVal;
		return false;
	}

	int sizeConverted = 0;
	errno_t err =wctomb_s(&sizeConverted, &val, 1, src.at(0));

	if(err==0&&sizeConverted==1)
		return true;

	return false;
}

bool BaseConverter::GetShort(const std::wstring& src, short defaultVal, short& val)
{
	if(src.size()<=0)
	{
		val = defaultVal;
		return false;
	}

	int n = _wtoi(src.c_str());

	if(n>SHRT_MAX||n<SHRT_MIN)
	{
		val = defaultVal;
		return false;
	}

	val = static_cast<short>(n);

	return true;
}

bool BaseConverter::GetInt32(const std::wstring& src, int defaultVal, int& val)
{
	if(src.size()<=0)
	{
		val = defaultVal;
		return false;
	}

	val = _wtoi(src.c_str());

	return true;
}

bool BaseConverter::GetInt64(const std::wstring& src, __int64 defaultVal, __int64& val)
{
	if(src.size()<=0)
	{
		val = defaultVal;
		return false;
	}

	val = _wtoi64(src.c_str());

	return true;
}

bool BaseConverter::GetUChar(const std::wstring& src, unsigned char defaultVal, unsigned char& val)
{
	if(src.size()<=0)
	{
		val = defaultVal;
		return false;
	}

	wchar_t* endpt = NULL;
	unsigned int n = wcstoul(src.c_str(), &endpt, 10);

	if(n>UCHAR_MAX)
	{
		val = defaultVal;
		return false;
	}

	val = static_cast<unsigned char>(n);

	return true;
}

bool BaseConverter::GetUShort(const std::wstring& src, unsigned short defaultVal, unsigned short& val)
{
	if(src.size()<=0)
	{
		val = defaultVal;
		return false;
	}

	wchar_t* endpt = NULL;
	unsigned int n = wcstoul(src.c_str(), &endpt, 10);

	if(n>USHRT_MAX)
	{
		val = defaultVal;
		return false;
	}

	val = static_cast<unsigned short>(n);

	return true;
}

bool BaseConverter::GetUInt32(const std::wstring& src, unsigned int defaultVal, unsigned int& val)
{
	if(src.size()<=0)
	{
		val = defaultVal;
		return false;
	}

	wchar_t* endpt = NULL;
	val = wcstoul(src.c_str(), &endpt, 10);

	return true;
}

bool BaseConverter::GetUInt64(const std::wstring& src, unsigned __int64 defaultVal, unsigned __int64& val)
{
	if(src.size()<=0)
	{
		val = defaultVal;
		return false;
	}

	val = _wtoi64(src.c_str());

	return true;
}

bool BaseConverter::GetFloat(const std::wstring& src, float defaultVal, float& val)
{
	if(src.size()<=0)
	{
		val = defaultVal;
		return false;
	}

	double d = _wtof(src.c_str());

	if(d>FLT_MAX||d<FLT_MIN)
	{
		val = defaultVal;
		return false;
	}

	val = static_cast<float>(d);

	return true;
}

bool BaseConverter::GetDouble(const std::wstring& src, double defaultVal, double& val)
{
	if(src.size()<=0)
	{
		val = defaultVal;
		return false;
	}

	val = _wtof(src.c_str());

	return true;
}

bool BaseConverter::GetString(const std::wstring& src, const std::wstring& defaultVal, std::wstring& val)
{
	if(src.size()<=0)
	{
		val = defaultVal;
		return false;
	}

	val = src;

	return true;
}

bool BaseConverter::GetString(const std::wstring& src, const std::string& defaultVal, std::string& val)
{
	if(src.size()<=0)
	{
		val = defaultVal;
		return false;
	}

	char* pBuf = new char[src.size()+1];
	if( NULL == pBuf )
		return false;
	memset( pBuf, 0, src.size()+1 );
	size_t RetSize=0;
	errno_t err = wcstombs_s( &RetSize, pBuf, src.size()+1, src.c_str(), src.size()+1 );

	if( 0 != err )
	{
		delete [] pBuf;
		return false;
	}

	val=pBuf;
	delete [] pBuf;

	return true;
}

bool BaseConverter::SetGUID(std::wstring& dest, GUID val, bool bRemoveBraces)
{
	BSTR bstrGuid;
	StringFromCLSID(val, &bstrGuid);

	dest = (LPCWSTR)(bstrGuid);

	if(bRemoveBraces)
	{
		dest = dest.substr(1, 36);
	}

	// ensure memory is freed
	::CoTaskMemFree(bstrGuid);

	return true;
}

bool BaseConverter::GetGUID(const std::wstring& src, const GUID& defaultVal, GUID& val)
{
	std::wstring temp = src;
	if(src.length()==36)
	{
		temp = L"{";
		temp += src;
		temp += L"}";
	}

	bstr_t bstrGuid = (LPCWSTR)temp.c_str();
	HRESULT hr = CLSIDFromString(bstrGuid, &val);

	if(FAILED(hr))
	{
		val = defaultVal;
		return false;
	}

	return true;
}

bool BaseConverter::ReadHex(const std::wstring& src, unsigned int defaultVal, unsigned int& val)
{
	if(src.size()<=0||CheckHexString(src)==false)
	{
		val = defaultVal;
		return false;
	}

	std::wstring src2 = src;
	if (src.size() > 1 && src.at(0) == L'0' && (src.at(1) == L'X' || src.at(1) == L'x'))
	{
		std::wstring str2 = L"";
		for (size_t i = 2; i < src.size(); ++i)
			str2 += src[i];

		src2 = str2;
	}

	// Convert to lower case
	std::wstring src3 = L"";
	for (size_t i = 0; i < src2.size(); ++i)
	{
		if(src2.at(i)==L'A')
			src3 += L'a';
		else if(src2.at(i)==L'B')
			src3 += L'b';
		else if(src2.at(i)==L'C')
			src3 += L'c';
		else if(src2.at(i)==L'D')
			src3 += L'd';
		else if(src2.at(i)==L'E')
			src3 += L'e';
		else if(src2.at(i)==L'F')
			src3 += L'f';
		else
			src3 += src2.at(i);
	}


	std::wstringstream ss;
	ss << std::setbase(16) << src3;
	ss >> val;

	return true;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

Shao Voon Wong
Software Developer
Singapore Singapore

Currently into areas like 3D graphics and application security. Hoping to revisit the cryptography and design pattern topics if time permits.


| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 7 Jun 2012
Article Copyright 2011 by Shao Voon Wong
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid