XdmfWriter.cpp 3.13 KB
Newer Older
1 2 3
// Kenneth Leiter
// Xdmf Smart Pointer Test

4
#include <libxml/tree.h>
5
#include <sstream>
6
#include "XdmfArray.hpp"
7
#include "XdmfItem.hpp"
8
#include "XdmfHDF5Writer.hpp"
9 10
#include "XdmfWriter.hpp"

11 12 13 14 15 16 17
/**
 * PIMPL
 */
class XdmfWriter::XdmfWriterImpl {

public:

18 19
	XdmfWriterImpl(const std::string & xmlFilePath, boost::shared_ptr<XdmfHDF5Writer> hdf5Writer) :
		mHDF5Writer(hdf5Writer),
20 21
		mLightDataLimit(100),
		mXMLDocument(xmlNewDoc((xmlChar*)"1.0")),
22 23
		mXMLCurrentNode(xmlNewNode(NULL, (xmlChar*)"Xdmf")),
		mXMLFilePath(xmlFilePath)
24 25 26 27 28 29 30 31 32 33 34 35 36
	{
		xmlDocSetRootElement(mXMLDocument, mXMLCurrentNode);
	};
	~XdmfWriterImpl()
	{
		xmlSaveFormatFile("output.xmf", mXMLDocument, 1);
		xmlFreeDoc(mXMLDocument);
		xmlCleanupParser();
	};
	boost::shared_ptr<XdmfHDF5Writer> mHDF5Writer;
	unsigned int mLightDataLimit;
	xmlDocPtr mXMLDocument;
	xmlNodePtr mXMLCurrentNode;
37
	std::string mXMLFilePath;
38 39
};

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
XdmfWriter::XdmfWriter(const std::string & xmlFilePath)
{
	std::stringstream heavyFileName;
	size_t extension = xmlFilePath.rfind(".");
	if(extension != std::string::npos)
	{
		heavyFileName << xmlFilePath.substr(0, extension) << ".h5";
	}
	else
	{
		heavyFileName << xmlFilePath << ".h5";
	}
	boost::shared_ptr<XdmfHDF5Writer> hdf5Writer = XdmfHDF5Writer::New(heavyFileName.str());
	mImpl = new XdmfWriterImpl(xmlFilePath, hdf5Writer);
	std::cout << "Created XdmfWriter " << this << std::endl;
}

XdmfWriter::XdmfWriter(const std::string & xmlFilePath, boost::shared_ptr<XdmfHDF5Writer> hdf5Writer) :
	mImpl(new XdmfWriterImpl(xmlFilePath, hdf5Writer))
59
{
60
	std::cout << "Created XdmfWriter " << this << std::endl;
61 62 63 64
}

XdmfWriter::~XdmfWriter()
{
65
	delete mImpl;
66
	std::cout << "Deleted XdmfWriter " << this << std::endl;
67
}
68

69 70
unsigned int XdmfWriter::getLightDataLimit() const
{
71
	return mImpl->mLightDataLimit;
72 73 74 75
}

void XdmfWriter::setLightDataLimit(unsigned int numValues)
{
76
	mImpl->mLightDataLimit = numValues;
77 78 79 80 81 82
}

void XdmfWriter::visit(XdmfArray & array, boost::shared_ptr<Loki::BaseVisitor> visitor)
{
	this->visit(dynamic_cast<XdmfItem &>(array), visitor);

83 84
	xmlNodePtr parentNode = mImpl->mXMLCurrentNode;
	mImpl->mXMLCurrentNode = mImpl->mXMLCurrentNode->children;
85 86

	std::stringstream xmlTextValues;
87
	if(array.getHDF5Controller() || array.getSize() > mImpl->mLightDataLimit)
88
	{
89 90
		mImpl->mHDF5Writer->visit(array, mImpl->mHDF5Writer);
		xmlTextValues << mImpl->mHDF5Writer->getLastWrittenDataSet();
91 92 93 94 95 96
	}
	else
	{
		xmlTextValues << array.getValuesString();
	}

97 98
	xmlAddChild(mImpl->mXMLCurrentNode, xmlNewText((xmlChar*)xmlTextValues.str().c_str()));
	mImpl->mXMLCurrentNode = parentNode;
99 100
}

101
void XdmfWriter::visit(XdmfItem & item, boost::shared_ptr<Loki::BaseVisitor> visitor)
102
{
103 104
	xmlNodePtr parentNode = mImpl->mXMLCurrentNode;
	mImpl->mXMLCurrentNode = xmlNewChild(mImpl->mXMLCurrentNode, NULL, (xmlChar*)item.getItemTag().c_str(), NULL);
105 106
	const std::map<std::string, std::string> itemProperties = item.getItemProperties();
	for(std::map<std::string, std::string>::const_iterator iter = itemProperties.begin(); iter != itemProperties.end(); ++iter)
107
	{
108
		xmlNewProp(mImpl->mXMLCurrentNode, (xmlChar*)iter->first.c_str(), (xmlChar*)iter->second.c_str());
109
	}
110
	item.traverse(visitor);
111
	mImpl->mXMLCurrentNode = parentNode;
112
}