Commit 81baa05d authored by Brad King's avatar Brad King

BUG: Using VTK_STREAM_EOF_SEVERITY to implement TellG and SeekG wrapper...

BUG: Using VTK_STREAM_EOF_SEVERITY to implement TellG and SeekG wrapper methods in a way that should work around all known stream bugs.
parent 6358ee40
......@@ -12,6 +12,21 @@
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
// Hack access to the fstream implementation if necessary. This is
// only needed on a few SGI MIPSpro compiler versions.
#if defined(__sgi) && !defined(__GNUC__) && defined(_COMPILER_VERSION)
# if _COMPILER_VERSION == 730
# include "vtkConfigure.h"
# if VTK_STREAM_EOF_SEVERITY == 3
# define protected public
# define private public
# include <fstream>
# undef private
# undef protected
# endif
# endif
#endif
#include "vtkXMLParser.h"
#include "vtkObjectFactory.h"
......@@ -19,7 +34,7 @@
#include <ctype.h>
#include <sys/stat.h>
vtkCxxRevisionMacro(vtkXMLParser, "1.20");
vtkCxxRevisionMacro(vtkXMLParser, "1.21");
vtkStandardNewMacro(vtkXMLParser);
//----------------------------------------------------------------------------
......@@ -56,17 +71,6 @@ void vtkXMLParser::PrintSelf(ostream& os, vtkIndent indent)
<< "\n";
}
//----------------------------------------------------------------------------
#if defined(VTK_USE_ANSI_STDLIB)
# if defined(__sgi) && !defined(__GNUC__)
# define VTK_XML_NEED_TELLG_WORKAROUND
# elif defined(__BORLANDC__) && (__BORLANDC__>=0x0560)
# define VTK_XML_NEED_TELLG_WORKAROUND
# elif defined(__APPLE_CC__)
# define VTK_XML_NEED_TELLG_WORKAROUND
# endif
#endif
//----------------------------------------------------------------------------
long vtkXMLParser::TellG()
{
......@@ -75,39 +79,89 @@ long vtkXMLParser::TellG()
{
return -1;
}
#if defined(VTK_XML_NEED_TELLG_WORKAROUND)
// This check is required for buggy streams libraries when VTK is
// built with VTK_USE_ANSI_STDLIB. It seems that after a read that
// just reaches EOF, tellg reports a -1.
#if VTK_STREAM_EOF_SEVERITY == 0
// No work-around required. Just return the position.
return this->Stream->tellg();
#else
long pos = this->Stream->tellg();
if(pos == -1)
{
// Save the eof flag.
// Clear the fail bit from failing tellg.
this->Stream->clear(this->Stream->rdstate() & ~ios::failbit);
// Save the eof bit.
int eof = this->Stream->eof()?1:0;
// Clear the eof and seek to the end. This works around the bug.
this->Stream->clear(this->Stream->rdstate() & ~ios::eofbit);
// Clear the eof bit.
if(eof)
{
this->Stream->clear(this->Stream->rdstate() & ~ios::eofbit);
}
# if VTK_STREAM_EOF_SEVERITY == 2
// Re-seek to the end to escape the buggy stream state.
this->Stream->seekg(0, ios::end);
# elif VTK_STREAM_EOF_SEVERITY == 3
// Call an internal filebuf method to escape the buggy stream
// state. This is a very ugly hack.
static_cast<ifstream*>(this->Stream)->rdbuf()->_M_seek_return(0,0);
# endif
// Call tellg to get the position.
pos = this->Stream->tellg();
// Restore the eof flag.
// Restore the eof bit.
if(eof)
{
this->Stream->clear(this->Stream->rdstate() | ios::eofbit);
}
}
return pos;
#else
return this->Stream->tellg();
#endif
}
//----------------------------------------------------------------------------
void vtkXMLParser::SeekG(long position)
{
// Standard seekg does nothing if fail() is true.
if(!this->Stream || this->Stream->fail())
{
return;
}
#if VTK_STREAM_EOF_SEVERITY == 0
// No work-around required. Just seek to the position.
this->Stream->seekg(position);
#else
// Save the eof bit.
int eof = this->Stream->eof()?1:0;
// Clear the eof bit.
if(eof)
{
this->Stream->clear(this->Stream->rdstate() & ~ios::eofbit);
}
# if VTK_STREAM_EOF_SEVERITY == 3
// Check if the stream is in the buggy state.
if(long(this->Stream->tellg()) == -1)
{
// Call an internal filebuf method to escape the buggy stream
// state. This is a very ugly hack.
this->Stream->clear(this->Stream->rdstate() & ~ios::failbit);
this->Stream->clear(this->Stream->rdstate() & ~ios::eofbit);
static_cast<ifstream*>(this->Stream)->rdbuf()->_M_seek_return(0,0);
}
# endif
// Seek to the given position.
this->Stream->seekg(position);
// Restore the eof bit.
if(eof)
{
this->Stream->clear(this->Stream->rdstate() | ios::eofbit);
}
#endif
}
//----------------------------------------------------------------------------
......
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