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

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

, 7 Jun 2012
Linq-To-XML Node Creation for Native C++
elmax-84498.zip
BuildProcessTemplates
Elmax.vsmdi
ElmaxNet
Properties
Elmax
Elmax.vcxproj.user
StringUtils
Local.testsettings
TestNativeElmax
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax
Properties
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
TryoutNet
Properties
Settings.settings
Tryout
res
Tryout.ico
Tryout.vcxproj.user
elmax-ver083.zip
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Settings.settings
Tryout.ico
Tryout.vcxproj.user
elmax-ver084.zip
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Settings.settings
Tryout.ico
Tryout.vcxproj.user
elmax-ver085.zip
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Settings.settings
Tryout.ico
Tryout.vcxproj.user
elmax-ver086.zip
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Settings.settings
Tryout.ico
Tryout.vcxproj.user
elmax-ver087.zip
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
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.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Settings.settings
Tryout.ico
Tryout.vcxproj.user
// RootElement.cpp
//
// written by: PJ Arends
// License Ms-PL

#include "stdafx.h"
#include "RootElement.h"
using namespace Elmax;

#include <boost/Filesystem.hpp>
using namespace boost::filesystem;

bool RootElement::LoadFile(const std::wstring &FilePath)
{
	bool retval(false);

	UnloadFile();

	HRESULT hr = m_ptrDoc.CreateInstance(__uuidof(MSXML2::DOMDocument30));

#ifdef _DEBUG
	if (hr == CO_E_NOTINITIALIZED)
	{
		throw std::exception("CoInitialize has not been called for this thread");
	}
#endif

	if (SUCCEEDED(hr))
	{
		retval = true;

		m_ptrDoc->async = VARIANT_FALSE;
		m_ptrDoc->validateOnParse = VARIANT_FALSE;
		m_ptrDoc->resolveExternals = VARIANT_FALSE;

		path Path(FilePath);
		if (exists(Path) && is_regular_file(Path))
		{   // Open existing file
			variant_t varFile(FilePath.c_str());

			// load will fail if the file has malformed xml
			if (m_ptrDoc->load(varFile) != VARIANT_TRUE)
			{
				retval = false;
			}
		}

		if (retval)
		{
			SetConverter(REGEX_CONV);
			SetDomDoc(m_ptrDoc);
			m_FilePath = FilePath;
		}
	}

	return retval;
}
bool RootElement::CanWriteFile(const std::wstring& FilePath, bool OverWrite, bool CreateFolder)
{
	path Path(FilePath);
	if (exists(Path))
	{
		if (is_regular_file(Path))
		{
			return OverWrite;
		}
		return false;
	}

	path Folder = Path.parent_path();
	if (exists(Folder))
	{
		return true;
	}

	if (CreateFolder)
	{
		return create_directories(Folder);
	}

	return false;
}

bool RootElement::SaveFile(const std::wstring& FilePath, bool OverWrite, bool CreateFolder)
{
	if (NULL == m_ptrDoc)
	{
		return false;
	}

	if (!FilePath.empty())
	{
		m_FilePath = FilePath;
	}

	if (CanWriteFile(m_FilePath, OverWrite, CreateFolder))
	{
		variant_t varFile(m_FilePath.c_str());
		Elmax::Document doc(m_ptrDoc);
		return doc.PrettySave(m_FilePath, L"1.0", true);
	}

	return false;
}

bool RootElement::UnloadFile()
{
	m_ptrDoc = NULL;  // calls Release() for us
	m_FilePath.clear();
	return true;
}

RootElement::~RootElement()
{
	UnloadFile();
}

RootElement& RootElement::operator = (const RootElement &other)
{
	UnloadFile();
	Element::operator=(other);
	m_FilePath = other.m_FilePath;

	return *this;
}

bool RootElement::SetFilePath(std::wstring FilePath)
{
	if (NULL == m_ptrDoc)
	{
		return false;
	}

	m_FilePath = FilePath;
	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
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.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