Click here to Skip to main content
13,666,077 members
Click here to Skip to main content

Stats

31.2K views
469 downloads
28 bookmarked
Posted 12 Apr 2016
Licenced Ms-PL

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

, 12 Apr 2016
Linq-To-XML Node Creation for Native C++
BuildProcessTemplates
Elmax.vsmdi
ElmaxNet
Properties
Elmax
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
StringUtils
Local.testsettings
TestNativeElmax
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax
Properties
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
TryoutNet
Properties
Tryout
res
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
Elmax.vsmdi
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
Elmax.vsmdi
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
Elmax.vsmdi
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
Elmax.vsmdi
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
Elmax.vsmdi
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
Elmax-ver089
Elmax.vsmdi
Elmax
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
StringUtils
ElmaxNet
Properties
Local.testsettings
TestNativeElmax
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax
Properties
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout
res
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
TryoutNet
Properties
Elmax.vsmdi
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
#include "StdAfx.h"
#include "RegexConverter.h"
#include <climits>
#include <cfloat>

using namespace Elmax;

RegexConverter::RegexConverter(void)
: m_rxFloat(L"^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$")
, m_rxInt(L"^[-+]?\\d*$")
, m_rxUInt(L"^\\d+$")
{
}

RegexConverter::~RegexConverter(void)
{
}

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

	return true;
}

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

bool RegexConverter::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 RegexConverter::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 RegexConverter::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 RegexConverter::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 RegexConverter::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 RegexConverter::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 RegexConverter::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 RegexConverter::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 RegexConverter::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 RegexConverter::SetString(std::wstring& dest, const std::wstring& val)
{
	dest = val;
	return true;
}

bool RegexConverter::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 RegexConverter::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 RegexConverter::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 RegexConverter::GetShort(const std::wstring& src, short defaultVal, short& val)
{
	if(src.size()<=0)
	{
		val = defaultVal;
		return false;
	}

#ifdef ELMAX_USE_BOOST_REGEX
	using namespace boost;
#else
	using namespace std::tr1;
#endif

	bool b = regex_match(src.begin(), src.end(), m_rxInt);
	if(false==b)
	{
		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 RegexConverter::GetInt32(const std::wstring& src, int defaultVal, int& val)
{
	if(src.size()<=0)
	{
		val = defaultVal;
		return false;
	}

#ifdef ELMAX_USE_BOOST_REGEX
	using namespace boost;
#else
	using namespace std::tr1;
#endif

	bool b = regex_match(src.begin(), src.end(), m_rxInt);
	if(false==b)
	{
		val = defaultVal;
		return false;
	}

	val = _wtoi(src.c_str());

	return true;
}

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

#ifdef ELMAX_USE_BOOST_REGEX
	using namespace boost;
#else
	using namespace std::tr1;
#endif

	bool b = regex_match(src.begin(), src.end(), m_rxInt);
	if(false==b)
	{
		val = defaultVal;
		return false;
	}

	val = _wtoi64(src.c_str());

	return true;
}

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

#ifdef ELMAX_USE_BOOST_REGEX
	using namespace boost;
#else
	using namespace std::tr1;
#endif

	bool b = regex_match(src.begin(), src.end(), m_rxUInt);
	if(false==b)
	{
		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 RegexConverter::GetUShort(const std::wstring& src, unsigned short defaultVal, unsigned short& val)
{
	if(src.size()<=0)
	{
		val = defaultVal;
		return false;
	}

#ifdef ELMAX_USE_BOOST_REGEX
	using namespace boost;
#else
	using namespace std::tr1;
#endif

	bool b = regex_match(src.begin(), src.end(), m_rxUInt);
	if(false==b)
	{
		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 RegexConverter::GetUInt32(const std::wstring& src, unsigned int defaultVal, unsigned int& val)
{
	if(src.size()<=0)
	{
		val = defaultVal;
		return false;
	}

#ifdef ELMAX_USE_BOOST_REGEX
	using namespace boost;
#else
	using namespace std::tr1;
#endif

	bool b = regex_match(src.begin(), src.end(), m_rxUInt);
	if(false==b)
	{
		val = defaultVal;
		return false;
	}

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

	return true;
}

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

#ifdef ELMAX_USE_BOOST_REGEX
	using namespace boost;
#else
	using namespace std::tr1;
#endif

	bool b = regex_match(src.begin(), src.end(), m_rxUInt);
	if(false==b)
	{
		val = defaultVal;
		return false;
	}

	val = _wtoi64(src.c_str());

	return true;
}

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

#ifdef ELMAX_USE_BOOST_REGEX
	using namespace boost;
#else
	using namespace std::tr1;
#endif

	bool b = regex_match(src.begin(), src.end(), m_rxFloat);
	if(false==b)
	{
		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 RegexConverter::GetDouble(const std::wstring& src, double defaultVal, double& val)
{
	if(src.size()<=0)
	{
		val = defaultVal;
		return false;
	}

#ifdef ELMAX_USE_BOOST_REGEX
	using namespace boost;
#else
	using namespace std::tr1;
#endif

	bool b = regex_match(src.begin(), src.end(), m_rxFloat);
	if(false==b)
	{
		val = defaultVal;
		return false;
	}

	val = _wtof(src.c_str());

	return true;
}

bool RegexConverter::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 RegexConverter::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;
}

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

Cake Processor
Software Developer (Senior)
United States United States
C++ developer transitioning to Python

Right now, I am picking up DevOps skills at Pluralsight and pursuing CCNA certification. Stay tuned for my CCNA related article!

Coding Tidbit Blog

Latest blogpost: C++ – The Forgotten Trojan Horse by Eric Johnson

Yinyang Theory Blog

If you like unconventional wisdom based on Yinyang philosophy, hop over to read my Yinyang blog.

Latest blogpost: Fortune Telling: Good News or Bad News For You?

IT Certifications

  • IT Infrastructure Library Foundational (ITIL v3)
  • Scrum Alliance Certified Scrum Master (CSM)
  • Certified Secure Software Lifecycle Professional (CSSLP)

View my certificates here.

You may also be interested in...

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180810.1 | Last Updated 12 Apr 2016
Article Copyright 2016 by Cake Processor
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid