Click here to Skip to main content
15,860,859 members
Articles / Programming Languages / Visual C++ 10.0

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

Rate me:
Please Sign up or sign in to vote.
4.78/5 (14 votes)
12 Apr 2016Ms-PL11 min read 43.3K   500   29  
Linq-To-XML Node Creation for Native C++
#include "StdAfx.h"
#define _VS2005_
#if _MSC_VER < 1400
#undef _VS2005_
#endif
#include "DataTypeRef.h"
#include "..\\BaseConverter.h"
using namespace Elmax;

DataTypeRef::~DataTypeRef(void)
{
}

bool DataTypeRef::ConvStrToType( const std::wstring& str )
{
	BaseConverter conv;

	switch( m_type )
	{
	case DTR_INT:
		conv.GetInt32(str, -1, *(m_ptr.pi));
		break;
	case DTR_UINT:
		conv.GetUInt32(str, 0, *(m_ptr.pui));
		break;
	case DTR_SHORT:
		conv.GetInt16(str, -1, *(m_ptr.psi));
		break;
	case DTR_USHORT:
		conv.GetUInt16(str, 0, *(m_ptr.pusi));
		break;
	case DTR_INT64:
		conv.GetInt64(str, -1, *(m_ptr.pi64));
		break;
	case DTR_UINT64:
		conv.GetUInt64(str, 0, *(m_ptr.pui64));
		break;
	case DTR_FLOAT:
		conv.GetFloat(str, 0.0f, *(m_ptr.pf));
		break;
	case DTR_DOUBLE:
		conv.GetDouble(str, 0.0, *(m_ptr.pd));
		break;
	case DTR_STR:
		{
#ifdef _VS2005_
				char* pBuf = new char[str.size()+1];
				if( NULL == pBuf )
					return false;
				memset( pBuf, 0, str.size()+1 );
				size_t RetSize=0;
				errno_t err = wcstombs_s( &RetSize, pBuf, str.size()+1, str.c_str(), str.size()+1 );

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

				*(m_ptr.ps)=pBuf;
				delete [] pBuf;
#else // _VS2005_
				char* pBuf = new char[StrUtil.size()+1];
				if( NULL == pBuf )
					return false;
				memset( pBuf, 0, StrUtil.size()+1 );
				wcstombs( pBuf, StrUtil.c_str(), StrUtil.size()+1 );

				*(m_ptr.ps)=pBuf;
				delete [] pBuf;
#endif // _VS2005_
		}

		break;
	case DTR_WSTR:
		*(m_ptr.pws) = str;
		break;
	case DTR_CHAR:
		conv.GetInt8(str, '-', *(m_ptr.pc));
		break;
	case DTR_WCHAR:
		if( str.size() > 0 )
			*(m_ptr.pwc) = str.at(0);
		break;
	default:
		return false;
	}

	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)


Written By
Software Developer (Senior)
Singapore Singapore
Shao Voon is from Singapore. His interest lies primarily in computer graphics, software optimization, concurrency, security, and Agile methodologies.

In recent years, he shifted focus to software safety research. His hobby is writing a free C++ DirectX photo slideshow application which can be viewed here.

Comments and Discussions