Add peekBack() method to Istream.

This commit is contained in:
Mark Olesen
2009-12-12 15:35:10 +01:00
parent 497ec32ed8
commit 7c8316df70
2 changed files with 44 additions and 29 deletions

View File

@ -25,35 +25,9 @@ License
\*---------------------------------------------------------------------------*/
#include "Istream.H"
#include "bool.H"
#include "token.H"
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
// Set t to the put back token if there is one and return true,
// otherwise return false
bool Foam::Istream::getBack(token& t)
{
if (bad())
{
FatalIOErrorIn("void Istream::getBack(token&)", *this)
<< "Attempt to get back from bad stream"
<< exit(FatalIOError);
return false;
}
else if (putBack_)
{
t = putBackToken_;
putBack_ = false;
return true;
}
return false;
}
// Keep the put back token
void Foam::Istream::putBack(const token& t)
{
if (bad())
@ -76,6 +50,40 @@ void Foam::Istream::putBack(const token& t)
}
bool Foam::Istream::getBack(token& t)
{
if (bad())
{
FatalIOErrorIn("void Istream::getBack(token&)", *this)
<< "Attempt to get back from bad stream"
<< exit(FatalIOError);
}
else if (putBack_)
{
t = putBackToken_;
putBack_ = false;
return true;
}
return false;
}
bool Foam::Istream::peekBack(token& t)
{
if (putBack_)
{
t = putBackToken_;
}
else
{
t = token::undefinedToken;
}
return putBack_;
}
// Functions for reading object delimiters ( ... )
Foam::Istream& Foam::Istream::readBegin(const char* funcName)

View File

@ -62,7 +62,7 @@ class Istream
{
// Private data
//- Has a token been put back on the stream
//- Has a token been put back on the stream?
bool putBack_;
//- The last token put back on the stream
@ -97,12 +97,19 @@ public:
// Read functions
//- Put back token
// Only a single put back is permitted
void putBack(const token&);
//- Get the put back token
//- Get the put back token if there is one and return true.
// Return false if no put back token is available.
bool getBack(token&);
//- Return next token from stream
//- Peek at the put back token without removing it.
// Returns false if no put back token is available and set the
// token to undefined.
bool peekBack(token&);
//- Return next token from stream
virtual Istream& read(token&) = 0;
//- Read a character