Click here to Skip to main content
15,861,125 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"
#include "Date.h"

using namespace Elmax;

Date::Date(void)
: m_nYear(0)
, m_nMonth(1)
, m_nMonthDay(1)
{
}

Date::Date(short year, short month, short day)
{
	try
	{
		SetDate(year, month, day);
	}
	catch(...)
	{
		m_nYear = 0;
		m_nMonth = 1;
		m_nMonthDay = 1;
	}
}


Date::~Date(void)
{
}

bool Date::SetDate(short year, short month, short day)
{
	if(month<1||month>12)
		throw std::exception("Invalid month");

	switch(month)
	{
	case 1:
		if(day<1||day>31)
			throw std::exception("Invalid day in January");
		break;
	case 2:
		if(day<1||day>29)
			throw std::exception("Invalid day in February");
		break;
	case 3:
		if(day<1||day>31)
			throw std::exception("Invalid day in March");
		break;
	case 4:
		if(day<1||day>30)
			throw std::exception("Invalid day in April");
		break;
	case 5:
		if(day<1||day>31)
			throw std::exception("Invalid day in May");
		break;
	case 6:
		if(day<1||day>30)
			throw std::exception("Invalid day in June");
		break;
	case 7:
		if(day<1||day>31)
			throw std::exception("Invalid day in July");
		break;
	case 8:
		if(day<1||day>31)
			throw std::exception("Invalid day in August");
		break;
	case 9:
		if(day<1||day>30)
			throw std::exception("Invalid day in September");
		break;
	case 10:
		if(day<1||day>31)
			throw std::exception("Invalid day in October");
		break;
	case 11:
		if(day<1||day>30)
			throw std::exception("Invalid day in November");
		break;
	case 12:
		if(day<1||day>31)
			throw std::exception("Invalid day in December");
		break;
	}

	m_nYear = year;
	m_nMonth = month;
	m_nMonthDay = day;

	return true;
}

void Date::SetString(const std::wstring& strDate)
{
	if(strDate.length()<10)
		throw std::exception("Invalid date string");

	std::wstring year = L"";
	for(size_t i=0; i<4; ++i)
	{
		if(strDate.at(i)>=L'0'&&strDate.at(i)<=L'9')
			year += strDate.at(i);
		else
			throw std::exception("Invalid character in year, instead of numbers");
	}

	short nYear = (short) (_wtoi(year.c_str()));

	std::wstring month = L"";
	for(size_t i=5; i<7; ++i)
	{
		if(i==5&&strDate.at(i)==L'0')
			continue;
		else if(strDate.at(i)>=L'0'&&strDate.at(i)<=L'9')
			month += strDate.at(i);
		else
			throw std::exception("Invalid character in month, instead of numbers");
	}

	short nMonth = (short) (_wtoi(month.c_str()));

	std::wstring monthday = L"";
	for(size_t i=8; i<10; ++i)
	{
		if(i==8&&strDate.at(i)==L'0')
			continue;
		else if(strDate.at(i)>=L'0'&&strDate.at(i)<=L'9')
			monthday += strDate.at(i);
		else
			throw std::exception("Invalid character in month, instead of numbers");
	}

	short nMonthDay = (short) (_wtoi(monthday.c_str()));

	SetDate(nYear, nMonth, nMonthDay);
}

std::wstring Date::GetString() const
{
	wchar_t buf[20];
	memset(buf, 0, sizeof(buf));
	StringCchPrintfW(buf, sizeof(buf)/sizeof(wchar_t), L"%04d-%02d-%02d", m_nYear, m_nMonth, m_nMonthDay);

	std::wstring strDate = buf;

	return strDate;
}

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