Commit e058cc5b authored by Kenneth Leiter's avatar Kenneth Leiter

ENH: Add ability to write light data to a stream rather than just to disk.

Also add ability in XdmfReader to parse light data from a string rather than
just from disk.
parent 10a0025d
......@@ -80,6 +80,21 @@ public:
mXPathMap.clear();
}
void
parse(const std::string & lightData)
{
mDocument = xmlParseDoc((const xmlChar*)lightData.c_str());
if(mDocument == NULL) {
XdmfError::message(XdmfError::FATAL,
"xmlReadFile could not parse passed light data string"
" in XdmfCoreReader::XdmfCoreReaderImpl::parse");
}
mXPathContext = xmlXPtrNewContext(mDocument, NULL, NULL);
mXPathMap.clear();
}
/**
* Constructs XdmfItems for all nodes in currNode's tree.
* XdmfItems are constructed by recursively calling this function for all
......@@ -207,6 +222,7 @@ public:
xmlDocPtr mDocument;
const XdmfCoreReader * const mCoreReader;
const shared_ptr<const XdmfCoreItemFactory> mItemFactory;
const std::string mRootItemTag;
std::string mXMLDir;
xmlXPathContextPtr mXPathContext;
std::map<xmlNodePtr, shared_ptr<XdmfItem> > mXPathMap;
......@@ -222,6 +238,17 @@ XdmfCoreReader::~XdmfCoreReader()
delete mImpl;
}
shared_ptr<XdmfItem >
XdmfCoreReader::parse(const std::string & lightData) const
{
mImpl->parse(lightData);
const xmlNodePtr currNode = xmlDocGetRootElement(mImpl->mDocument);
const std::vector<shared_ptr<XdmfItem> > toReturn =
mImpl->read(currNode->children);
mImpl->closeFile();
return(toReturn[0]);
}
std::vector<shared_ptr<XdmfItem> >
XdmfCoreReader::readItems(const std::string & filePath) const
{
......
......@@ -50,6 +50,17 @@ public:
virtual ~XdmfCoreReader() = 0;
/**
* Parse a string containing light data into an Xdmf structure in
* memory.
*
* @param lightData a string containing light data description of an
* Xdmf file.
*
* @return an XdmfItem at the root of the Xdmf tree.
*/
virtual shared_ptr<XdmfItem> parse(const std::string & lightData) const;
/**
* Read an Xdmf file from disk into memory.
*
......@@ -105,6 +116,7 @@ private:
void operator=(const XdmfCoreReader &); // Not implemented.
XdmfCoreReaderImpl * const mImpl;
};
#ifdef _WIN32
......
......@@ -21,6 +21,7 @@
/* */
/*****************************************************************************/
#include <fstream>
#include <libxml/tree.h>
#include <sstream>
#include "XdmfArray.hpp"
......@@ -40,12 +41,14 @@ class XdmfWriter::XdmfWriterImpl {
public:
XdmfWriterImpl(const std::string & xmlFilePath,
const shared_ptr<XdmfHeavyDataWriter> heavyDataWriter) :
const shared_ptr<XdmfHeavyDataWriter> heavyDataWriter,
std::ostream * stream) :
mDepth(0),
mDocumentTitle("Xdmf"),
mHeavyDataWriter(heavyDataWriter),
mLastXPathed(false),
mLightDataLimit(100),
mStream(stream),
mMode(Default),
mWriteXPaths(true),
mXMLCurrentNode(NULL),
......@@ -65,7 +68,28 @@ public:
closeFile()
{
mXPath.clear();
xmlSaveFormatFile(mXMLFilePath.c_str(), mXMLDocument, 1);
std::ofstream fileStream;
if(!mStream) {
fileStream.open(mXMLFilePath.c_str());
mStream = &fileStream;
}
xmlBufferPtr buffer = xmlBufferCreate();
xmlOutputBuffer * outputBuffer = xmlOutputBufferCreateBuffer(buffer,
NULL);
xmlSaveFormatFileTo(outputBuffer,
mXMLDocument,
"utf-8",
1);
*mStream << buffer->content;
xmlBufferFree(buffer);
if(fileStream.is_open()) {
fileStream.close();
mStream = NULL;
}
xmlFreeDoc(mXMLDocument);
xmlCleanupParser();
};
......@@ -90,6 +114,7 @@ public:
bool mLastXPathed;
unsigned int mLightDataLimit;
Mode mMode;
std::ostream * mStream;
bool mWriteXPaths;
xmlNodePtr mXMLCurrentNode;
xmlDocPtr mXMLDocument;
......@@ -112,7 +137,7 @@ XdmfWriter::New(const std::string & xmlFilePath)
else {
heavyFileName << xmlFilePath << ".h5";
}
shared_ptr<XdmfHDF5Writer> hdf5Writer =
shared_ptr<XdmfHDF5Writer> hdf5Writer =
XdmfHDF5Writer::New(heavyFileName.str());
shared_ptr<XdmfWriter> p(new XdmfWriter(xmlFilePath, hdf5Writer));
return p;
......@@ -127,9 +152,22 @@ XdmfWriter::New(const std::string & xmlFilePath,
return p;
}
shared_ptr<XdmfWriter>
XdmfWriter::New(std::ostream & stream,
const shared_ptr<XdmfHeavyDataWriter> heavyDataWriter)
{
shared_ptr<XdmfWriter> p(new XdmfWriter("",
heavyDataWriter,
&stream));
return p;
}
XdmfWriter::XdmfWriter(const std::string & xmlFilePath,
shared_ptr<XdmfHeavyDataWriter> heavyDataWriter) :
mImpl(new XdmfWriterImpl(xmlFilePath, heavyDataWriter))
shared_ptr<XdmfHeavyDataWriter> heavyDataWriter,
std::ostream * stream) :
mImpl(new XdmfWriterImpl(xmlFilePath,
heavyDataWriter,
stream))
{
}
......@@ -181,12 +219,6 @@ XdmfWriter::setDocumentTitle(std::string title)
mImpl->mDocumentTitle = title;
}
void
XdmfWriter::setVersionString(std::string version)
{
mImpl->mVersionString = version;
}
void
XdmfWriter::setLightDataLimit(const unsigned int numValues)
{
......@@ -199,6 +231,12 @@ XdmfWriter::setMode(const Mode mode)
mImpl->mMode = mode;
}
void
XdmfWriter::setVersionString(std::string version)
{
mImpl->mVersionString = version;
}
void
XdmfWriter::setWriteXPaths(const bool writeXPaths)
{
......
......@@ -87,6 +87,18 @@ public:
static shared_ptr<XdmfWriter> New(const std::string & xmlFilePath,
const shared_ptr<XdmfHeavyDataWriter> heavyDataWriter);
/**
* Create a new XdmfWriter to write Xdmf data to disk. This will
* write heavy data to disk using the passed heavy data writer and
* will add xml output to the stream.
*
* @param stream the output stream to write light data to.
*
* @return the new XdmfWriter;
*/
static shared_ptr<XdmfWriter> New(std::ostream & stream,
const shared_ptr<XdmfHeavyDataWriter> heavyDataWriter);
virtual ~XdmfWriter();
/**
......@@ -180,7 +192,8 @@ public:
protected:
XdmfWriter(const std::string & xmlFilePath,
shared_ptr<XdmfHeavyDataWriter> heavyDataWriter);
shared_ptr<XdmfHeavyDataWriter> heavyDataWriter,
std::ostream * stream = NULL);
void setDocumentTitle(std::string title);
void setVersionString(std::string version);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment