Commit e99683a0 authored by Zack Galbreath's avatar Zack Galbreath
Browse files

vtkXMLReader can now read from an input string

Extend this class so that it can read from either a file or a
string.

Change-Id: Ida093f0c4c44896ed59da96b8950e7ffa2b6351b
parent 2f042ba4
......@@ -84,6 +84,9 @@ vtkXMLReader::vtkXMLReader()
this->FileName = 0;
this->Stream = 0;
this->FileStream = 0;
this->StringStream = 0;
this->ReadFromInputString = 0;
this->InputString = "";
this->XMLParser = 0;
this->FieldDataElement = 0;
this->PointDataArraySelection = vtkDataArraySelection::New();
......@@ -191,6 +194,19 @@ int vtkXMLReader::CanReadFileVersion(int major, int vtkNotUsed(minor))
return 1;
}
//----------------------------------------------------------------------------
int vtkXMLReader::OpenStream()
{
if (this->ReadFromInputString)
{
return this->OpenVTKString();
}
else
{
return this->OpenVTKFile();
}
}
//----------------------------------------------------------------------------
int vtkXMLReader::OpenVTKFile()
{
......@@ -244,6 +260,60 @@ int vtkXMLReader::OpenVTKFile()
return 1;
}
//----------------------------------------------------------------------------
int vtkXMLReader::OpenVTKString()
{
if(this->StringStream)
{
vtkErrorMacro("string already open.");
return 1;
}
if(!this->Stream && this->InputString.compare("") == 0)
{
vtkErrorMacro("Input string not specified");
return 0;
}
if(this->Stream)
{
// Use user-provided stream.
return 1;
}
// Open the string stream
this->StringStream = new std::istringstream(this->InputString);
if(!this->StringStream || !(*this->StringStream))
{
vtkErrorMacro("Error opening string stream");
if(this->StringStream)
{
delete this->StringStream;
this->StringStream = 0;
}
return 0;
}
// Use the string stream.
this->Stream = this->StringStream;
return 1;
}
//----------------------------------------------------------------------------
void vtkXMLReader::CloseStream()
{
if (this->ReadFromInputString)
{
this->CloseVTKString();
}
else
{
this->CloseVTKFile();
}
this->Stream = 0;
}
//----------------------------------------------------------------------------
void vtkXMLReader::CloseVTKFile()
{
......@@ -258,7 +328,22 @@ void vtkXMLReader::CloseVTKFile()
this->FileStream->close();
delete this->FileStream;
this->FileStream = 0;
this->Stream = 0;
}
}
//----------------------------------------------------------------------------
void vtkXMLReader::CloseVTKString()
{
if(!this->Stream)
{
vtkErrorMacro("String not open.");
return;
}
if(this->Stream == this->StringStream)
{
// We opened the string. Close it.
delete this->StringStream;
this->StringStream = 0;
}
}
......@@ -332,8 +417,8 @@ int vtkXMLReader::ReadXMLInformation()
}
// Open the input file. If it fails, the error was already
// reported by OpenVTKFile.
if(!this->OpenVTKFile())
// reported by OpenStream.
if(!this->OpenStream())
{
return 0;
}
......@@ -366,8 +451,8 @@ int vtkXMLReader::ReadXMLInformation()
this->ReadError = 1;
}
// Close the file to prevent resource leaks.
this->CloseVTKFile();
// Close the input stream to prevent resource leaks.
this->CloseStream();
this->ReadMTime.Modified();
}
......@@ -472,8 +557,8 @@ int vtkXMLReader::RequestData(vtkInformation *vtkNotUsed(request),
}
// Re-open the input file. If it fails, the error was already
// reported by OpenVTKFile.
if(!this->OpenVTKFile())
// reported by OpenStream.
if(!this->OpenStream())
{
this->SetupEmptyOutput();
this->CurrentOutput = 0;
......@@ -522,8 +607,8 @@ int vtkXMLReader::RequestData(vtkInformation *vtkNotUsed(request),
// We have finished reading.
this->UpdateProgressDiscrete(1);
// Close the file to prevent resource leaks.
this->CloseVTKFile();
// Close the input stream to prevent resource leaks.
this->CloseStream();
if( this->TimeSteps )
{
// The SetupOutput should not reallocate this should be done only in a TimeStep case
......
......@@ -22,6 +22,7 @@
#define __vtkXMLReader_h
#include "vtkIOXMLModule.h" // For export macro
#include <sstream> // For istringstream ivar.
#include "vtkAlgorithm.h"
class vtkAbstractArray;
......@@ -45,6 +46,13 @@ public:
vtkSetStringMacro(FileName);
vtkGetStringMacro(FileName);
// Description:
// Enable writing to an InputString instead of the default, a file.
vtkSetMacro(ReadFromInputString,int);
vtkGetMacro(ReadFromInputString,int);
vtkBooleanMacro(ReadFromInputString,int);
void SetInputString(std::string s) { this->InputString = s; }
// Description:
// Test whether the file with the given name can be read by this
// reader.
......@@ -150,8 +158,12 @@ protected:
int CreateInformationKey(vtkXMLDataElement *eInfoKey, vtkInformation *info);
// Internal utility methods.
virtual int OpenStream();
virtual void CloseStream();
virtual int OpenVTKFile();
virtual void CloseVTKFile();
virtual int OpenVTKString();
virtual void CloseVTKString();
virtual void CreateXMLParser();
virtual void DestroyXMLParser();
void SetupCompressor(const char* type);
......@@ -213,6 +225,13 @@ protected:
// The stream used to read the input.
istream* Stream;
// Whether this object is reading from a string or a file.
// Default is 0: read from file.
int ReadFromInputString;
// The input string.
std::string InputString;
// The array selections.
vtkDataArraySelection* PointDataArraySelection;
vtkDataArraySelection* CellDataArraySelection;
......@@ -279,6 +298,8 @@ protected:
private:
// The stream used to read the input if it is in a file.
ifstream* FileStream;
// The stream used to read the input if it is in a string.
std::istringstream* StringStream;
int TimeStepWasReadOnce;
int FileMajorVersion;
......
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