Commit 10955ce9 authored by Brad King's avatar Brad King

ENH: Added support for reading/writing XML data with a string stream. Writing...

ENH: Added support for reading/writing XML data with a string stream.  Writing currently works only for inline ascii and inline binary without compression because these modes do not require random access seeking.
parent dc778b95
......@@ -31,7 +31,7 @@
#include "vtkXMLStructuredGridWriter.h"
#include "vtkXMLUnstructuredGridWriter.h"
vtkCxxRevisionMacro(vtkXMLDataSetWriter, "1.3");
vtkCxxRevisionMacro(vtkXMLDataSetWriter, "1.4");
vtkStandardNewMacro(vtkXMLDataSetWriter);
//----------------------------------------------------------------------------
......@@ -125,6 +125,7 @@ int vtkXMLDataSetWriter::WriteInternal()
// Copy the settings to the writer.
writer->SetDebug(this->GetDebug());
writer->SetFileName(this->GetFileName());
writer->SetStream(this->GetStream());
writer->SetByteOrder(this->GetByteOrder());
writer->SetCompressor(this->GetCompressor());
writer->SetBlockSize(this->GetBlockSize());
......
......@@ -31,7 +31,7 @@
#include "vtkXMLPStructuredGridWriter.h"
#include "vtkXMLPUnstructuredGridWriter.h"
vtkCxxRevisionMacro(vtkXMLPDataSetWriter, "1.3");
vtkCxxRevisionMacro(vtkXMLPDataSetWriter, "1.4");
vtkStandardNewMacro(vtkXMLPDataSetWriter);
//----------------------------------------------------------------------------
......@@ -120,6 +120,7 @@ int vtkXMLPDataSetWriter::WriteInternal()
// Copy the settings to the writer.
writer->SetDebug(this->GetDebug());
writer->SetFileName(this->GetFileName());
writer->SetStream(this->GetStream());
writer->SetByteOrder(this->GetByteOrder());
writer->SetCompressor(this->GetCompressor());
writer->SetBlockSize(this->GetBlockSize());
......
......@@ -32,12 +32,13 @@
#include <sys/stat.h>
vtkCxxRevisionMacro(vtkXMLReader, "1.14");
vtkCxxRevisionMacro(vtkXMLReader, "1.15");
//----------------------------------------------------------------------------
vtkXMLReader::vtkXMLReader()
{
this->FileName = 0;
this->Stream = 0;
this->FileStream = 0;
this->XMLParser = 0;
this->PointDataArraySelection = vtkDataArraySelection::New();
......@@ -113,15 +114,21 @@ int vtkXMLReader::OpenVTKFile()
vtkErrorMacro("File already open.");
return 1;
}
if (!this->FileName)
if(!this->Stream && !this->FileName)
{
vtkErrorMacro("File name not specified");
return 0;
}
// First make sure the file exists. This prevents an empty file
// from being created on older compilers.
if(this->Stream)
{
// Use user-provided stream.
return 1;
}
// Need to open a file. First make sure it exists. This prevents
// an empty file from being created on older compilers.
struct stat fs;
if(stat(this->FileName, &fs) != 0)
{
......@@ -129,35 +136,45 @@ int vtkXMLReader::OpenVTKFile()
return 0;
}
this->FileStream = new ifstream;
#ifdef _WIN32
this->FileStream->open(this->FileName, ios::binary | ios::in);
this->FileStream = new ifstream(this->FileName, ios::binary | ios::in);
#else
this->FileStream->open(this->FileName, ios::in);
this->FileStream = new ifstream(this->FileName, ios::in);
#endif
if(!(*this->FileStream))
if(!this->FileStream || !(*this->FileStream))
{
vtkErrorMacro("Error opening file " << this->FileName);
delete this->FileStream;
this->FileStream = 0;
if(this->FileStream)
{
delete this->FileStream;
this->FileStream = 0;
}
return 0;
}
// Use the file stream.
this->Stream = this->FileStream;
return 1;
}
//----------------------------------------------------------------------------
void vtkXMLReader::CloseVTKFile()
{
if(!this->FileStream)
if(!this->Stream)
{
vtkErrorMacro("File not open.");
return;
}
this->FileStream->close();
delete this->FileStream;
this->FileStream = 0;
if(this->Stream == this->FileStream)
{
// We opened the file. Close it.
this->FileStream->close();
delete this->FileStream;
this->FileStream = 0;
this->Stream = 0;
}
}
//----------------------------------------------------------------------------
......@@ -232,7 +249,7 @@ void vtkXMLReader::ExecuteInformation()
this->CreateXMLParser();
// Configure the parser for this file.
this->XMLParser->SetStream(this->FileStream);
this->XMLParser->SetStream(this->Stream);
// Parse the input file.
if(this->XMLParser->Parse())
......@@ -287,7 +304,7 @@ void vtkXMLReader::ExecuteData(vtkDataObject* output)
// Give the vtkXMLParser instance its file back so that data section
// reads will work.
this->XMLParser->SetStream(this->FileStream);
this->XMLParser->SetStream(this->Stream);
// We are just starting to read. Do not call UpdateProgressDiscrete
// because we want a 0 progress callback the first time.
......
......@@ -80,6 +80,14 @@ public:
void SetPointArrayStatus(const char* name, int status);
void SetCellArrayStatus(const char* name, int status);
//BTX
// Description:
// Get/Set the input stream from which to read the data. This
// overrides use of FileName.
vtkGetMacro(Stream, istream*);
vtkSetMacro(Stream, istream*);
//ETX
protected:
vtkXMLReader();
~vtkXMLReader();
......@@ -160,8 +168,8 @@ protected:
// The input file's name.
char* FileName;
// The file stream used to read the input file.
ifstream* FileStream;
// The stream used to read the input.
istream* Stream;
// The array selections.
vtkDataArraySelection* PointDataArraySelection;
......@@ -188,6 +196,10 @@ protected:
virtual void SetProgressRange(float* range, int curStep, float* fractions);
virtual void UpdateProgressDiscrete(float progress);
float ProgressRange[2];
private:
// The stream used to read the input if it is in a file.
ifstream* FileStream;
private:
vtkXMLReader(const vtkXMLReader&); // Not implemented.
......
......@@ -28,7 +28,7 @@
#include "vtkUnsignedCharArray.h"
#include "vtkZLibDataCompressor.h"
vtkCxxRevisionMacro(vtkXMLWriter, "1.20");
vtkCxxRevisionMacro(vtkXMLWriter, "1.21");
vtkCxxSetObjectMacro(vtkXMLWriter, Compressor, vtkDataCompressor);
//----------------------------------------------------------------------------
......@@ -226,7 +226,7 @@ int vtkXMLWriter::Write()
}
// Make sure we have a file to write.
if(!this->FileName)
if(!this->Stream && !this->FileName)
{
vtkErrorMacro("Write() called with no FileName set.");
return 0;
......@@ -252,21 +252,30 @@ int vtkXMLWriter::Write()
//----------------------------------------------------------------------------
int vtkXMLWriter::WriteInternal()
{
// Try to open the output file for writing.
{
ofstream* outFile = 0;
if(this->Stream)
{
// Rewind stream to the beginning.
this->Stream->seekp(0);
}
else
{
// Try to open the output file for writing.
#ifdef _WIN32
ofstream outFile(this->FileName, ios::out | ios::binary);
outFile = new ofstream(this->FileName, ios::out | ios::binary);
#else
ofstream outFile(this->FileName, ios::out);
outFile = new ofstream(this->FileName, ios::out);
#endif
if(!outFile)
{
vtkErrorMacro("Error opening output file \"" << this->FileName << "\"");
return 0;
if(!outFile || !*outFile)
{
vtkErrorMacro("Error opening output file \"" << this->FileName << "\"");
return 0;
}
this->Stream = outFile;
}
// Setup the output streams.
this->Stream = &outFile;
this->DataStream->SetStream(this->Stream);
// Tell the subclass to write the data.
......@@ -274,7 +283,13 @@ int vtkXMLWriter::WriteInternal()
// Cleanup the output streams.
this->DataStream->SetStream(0);
this->Stream = 0;
if(outFile)
{
// We opened a file. Close it.
delete outFile;
this->Stream = 0;
}
return result;
}
......
......@@ -128,6 +128,14 @@ public:
// Invoke the writer. Returns 1 for success, 0 for failure.
int Write();
//BTX
// Description:
// Get/Set the output stream to which to write the data. This
// overrides use of FileName.
vtkGetMacro(Stream, ostream*);
vtkSetMacro(Stream, ostream*);
//ETX
protected:
vtkXMLWriter();
~vtkXMLWriter();
......
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