From 8f444b71646f39edfef6c51e791b07bf9e34845c Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Mon, 13 Nov 2017 09:21:11 +0100 Subject: [PATCH] ENH: support change of Pstream buffer format via flag modifier - allows changing the format of the sending OPstream at an arbitrary point in the transmission. The information is passed through the buffer and the receiving IPstream changes its format accordingly. This allows a temporary toggling of ASCII/BINARY mid-stream. --- .../db/IOstreams/Pstreams/UIPstream.C | 59 +++++++++++++++++-- .../db/IOstreams/Pstreams/UOPstream.C | 3 +- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.C b/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.C index 1ed965b758..76f9706093 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.C +++ b/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.C @@ -29,6 +29,24 @@ License #include "token.H" #include +// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * // + +namespace Foam +{ +// Adjust stream format based on the flagMask +inline static void processFlags(Istream& is, int flagMask) +{ + if ((flagMask & token::ASCII)) + { + is.format(IOstream::ASCII); + } + else if ((flagMask & token::BINARY)) + { + is.format(IOstream::BINARY); + } +} +} + // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // @@ -121,19 +139,48 @@ Foam::UIPstream::~UIPstream() Foam::Istream& Foam::UIPstream::read(token& t) { // Return the put back token if it exists + // - with additional handling for special stream flags if (Istream::getBack(t)) { - return *this; + if (t.isFlag()) + { + processFlags(*this, t.flagToken()); + } + else + { + return *this; + } } + // Read character, return on error + // - with additional handling for special stream flags + char c; - - // Return on error - if (!read(c)) + do { - t.setBad(); - return *this; + if (!read(c)) + { + t.setBad(); // Error + return *this; + } + + if (c == token::FLAG) + { + char flagVal; + + if (read(flagVal)) + { + processFlags(*this, flagVal); + } + else + { + t.setBad(); // Error + return *this; + } + } } + while (c == token::FLAG); + // Set the line number of this token to the current stream line number t.lineNumber() = lineNumber(); diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.C b/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.C index b5c63d0c57..6b5a46ed6f 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.C +++ b/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.C @@ -193,7 +193,8 @@ bool Foam::UOPstream::write(const token& tok) { case token::tokenType::FLAG : { - // silently consume the flag + writeToBuffer(char(token::tokenType::FLAG)); + writeToBuffer(char(tok.flagToken())); return true; }