diff --git a/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H b/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H index 990e0924e8..bf0150cb24 100644 --- a/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H +++ b/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H @@ -96,8 +96,9 @@ public: // Write functions - //- Write next token to stream - virtual Ostream& write(const token& tok) = 0; + //- Write token to stream or otherwise handle it. + // \return false if the token type was not handled by this method + virtual bool write(const token& tok) = 0; //- Write character virtual Ostream& write(const char c) = 0; diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.C b/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.C index d9ffbd0ed9..d8e10b619c 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.C +++ b/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.C @@ -185,25 +185,33 @@ Foam::UOPstream::~UOPstream() // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // -Foam::Ostream& Foam::UOPstream::write(const token& tok) +bool Foam::UOPstream::write(const token& tok) { - // Raw token output only supported for verbatim strings for now - if (tok.type() == token::tokenType::VERBATIMSTRING) + // Direct token handling only for some types + + switch (tok.type()) { - writeToBuffer(char(token::tokenType::VERBATIMSTRING)); - write(tok.stringToken()); + case token::tokenType::VERBATIMSTRING : + { + writeToBuffer(char(token::tokenType::VERBATIMSTRING)); + write(tok.stringToken()); + + return true; + } + + case token::tokenType::VARIABLE : + { + writeToBuffer(char(token::tokenType::VARIABLE)); + write(tok.stringToken()); + + return true; + } + + default: + break; } - else if (tok.type() == token::tokenType::VARIABLE) - { - writeToBuffer(char(token::tokenType::VARIABLE)); - write(tok.stringToken()); - } - else - { - NotImplemented; - setBad(); - } - return *this; + + return false; } diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.H b/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.H index c96b33ac8c..3287c62cd4 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.H +++ b/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.H @@ -127,7 +127,7 @@ public: // Inquiry //- Return flags of output stream - ios_base::fmtflags flags() const + virtual ios_base::fmtflags flags() const { return ios_base::fmtflags(0); } @@ -146,25 +146,26 @@ public: const label communicator = 0 ); - //- Write next token to stream - Ostream& write(const token& tok); + //- Write token to stream or otherwise handle it. + // \return false if the token type was not handled by this method + virtual bool write(const token& tok); //- Write single character. Whitespace is suppressed. - Ostream& write(const char c); + virtual Ostream& write(const char c); //- Write the word-characters of a character string. // Sends as a single char, or as word. - Ostream& write(const char* str); + virtual Ostream& write(const char* str); //- Write word - Ostream& write(const word& str); + virtual Ostream& write(const word& str); //- Write string - Ostream& write(const string& str); + virtual Ostream& write(const string& str); //- Write std::string surrounded by quotes. // Optional write without quotes. - Ostream& writeQuoted + virtual Ostream& writeQuoted ( const std::string& str, const bool quoted=true @@ -174,70 +175,74 @@ public: virtual Ostream& write(const int32_t val); //- Write int64_t as a label - Ostream& write(const int64_t val); + virtual Ostream& write(const int64_t val); //- Write floatScalar - Ostream& write(const floatScalar val); + virtual Ostream& write(const floatScalar val); //- Write doubleScalar - Ostream& write(const doubleScalar val); + virtual Ostream& write(const doubleScalar val); //- Write binary block with 8-byte alignment. - Ostream& write(const char* data, const std::streamsize count); + virtual Ostream& write + ( + const char* data, + const std::streamsize count + ); //- Begin marker for low-level raw binary output. // The count should indicate the number of bytes for subsequent // writeRaw calls. - Ostream& beginRaw(const std::streamsize count); + virtual Ostream& beginRaw(const std::streamsize count); //- Low-level raw binary output. - Ostream& writeRaw + virtual Ostream& writeRaw ( const char* data, const std::streamsize count ); //- End marker for low-level raw binary output. - Ostream& endRaw() + virtual Ostream& endRaw() { return *this; } //- Add indentation characters - void indent() + virtual void indent() {} // Stream state functions //- Flush stream - void flush() + virtual void flush() {} //- Add newline and flush stream - void endl() + virtual void endl() {} //- Get width of output field - int width() const + virtual int width() const { return 0; } //- Set width of output field (and return old width) - int width(const int) + virtual int width(const int) { return 0; } //- Get precision of output field - int precision() const + virtual int precision() const { return 0; } //- Set precision of output field (and return old precision) - int precision(const int) + virtual int precision(const int) { return 0; } diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.C b/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.C index 1ace27022c..f40f94218c 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.C +++ b/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.C @@ -30,21 +30,35 @@ License // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -Foam::Ostream& Foam::OSstream::write(const token& tok) +bool Foam::OSstream::write(const token& tok) { - if (tok.type() == token::tokenType::VERBATIMSTRING) + // Direct token handling only for some types + + switch (tok.type()) { - write(char(token::HASH)); - write(char(token::BEGIN_BLOCK)); - writeQuoted(tok.stringToken(), false); - write(char(token::HASH)); - write(char(token::END_BLOCK)); + case token::tokenType::VERBATIMSTRING : + { + write(char(token::HASH)); + write(char(token::BEGIN_BLOCK)); + writeQuoted(tok.stringToken(), false); + write(char(token::HASH)); + write(char(token::END_BLOCK)); + + return true; + } + + case token::tokenType::VARIABLE : + { + writeQuoted(tok.stringToken(), false); + + return true; + } + + default: + break; } - else if (tok.type() == token::tokenType::VARIABLE) - { - writeQuoted(tok.stringToken(), false); - } - return *this; + + return false; } diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.H b/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.H index 753bad7ca5..8218a5a3d6 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.H +++ b/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2014 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -104,8 +104,9 @@ public: // Write functions - //- Write next token to stream - virtual Ostream& write(const token& tok); + //- Write token to stream or otherwise handle it. + // \return false if the token type was not handled by this method + virtual bool write(const token& tok); //- Write character virtual Ostream& write(const char c); diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/prefixOSstream.C b/src/OpenFOAM/db/IOstreams/Sstreams/prefixOSstream.C index fcafc89f7f..eb96ed1414 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/prefixOSstream.C +++ b/src/OpenFOAM/db/IOstreams/Sstreams/prefixOSstream.C @@ -65,21 +65,35 @@ void Foam::prefixOSstream::print(Ostream& os) const } -Foam::Ostream& Foam::prefixOSstream::write(const token& tok) +bool Foam::prefixOSstream::write(const token& tok) { - if (tok.type() == token::tokenType::VERBATIMSTRING) + // Direct token handling only for some types + + switch (tok.type()) { - write(char(token::HASH)); - write(char(token::BEGIN_BLOCK)); - writeQuoted(tok.stringToken(), false); - write(char(token::HASH)); - write(char(token::END_BLOCK)); + case token::tokenType::VERBATIMSTRING : + { + write(char(token::HASH)); + write(char(token::BEGIN_BLOCK)); + writeQuoted(tok.stringToken(), false); + write(char(token::HASH)); + write(char(token::END_BLOCK)); + + return true; + } + + case token::tokenType::VARIABLE : + { + writeQuoted(tok.stringToken(), false); + + return true; + } + + default: + break; } - else if (tok.type() == token::tokenType::VARIABLE) - { - writeQuoted(tok.stringToken(), false); - } - return *this; + + return false; } diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/prefixOSstream.H b/src/OpenFOAM/db/IOstreams/Sstreams/prefixOSstream.H index 4c9eca7b44..d67c6d916e 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/prefixOSstream.H +++ b/src/OpenFOAM/db/IOstreams/Sstreams/prefixOSstream.H @@ -98,8 +98,9 @@ public: // Write functions - //- Write next token to stream - virtual Ostream& write(const token& tok); + //- Write token to stream or otherwise handle it. + // \return false if the token type was not handled by this method + virtual bool write(const token& tok); //- Write character virtual Ostream& write(const char c); diff --git a/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntryIO.C b/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntryIO.C index 1d91eccf6c..f9a6759030 100644 --- a/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntryIO.C +++ b/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntryIO.C @@ -200,25 +200,16 @@ void Foam::primitiveEntry::write(Ostream& os, const bool contentsOnly) const bool addSpace = false; // Separate from previous tokens with a space for (const token& tok : *this) { - if (tok.type() == token::tokenType::VERBATIMSTRING) + if (addSpace) os << token::SPACE; + + // Try to output token directly, with special handling in Ostreams. + + if (!os.write(tok)) { - // Bypass token output operator to avoid losing verbatimness. - // Handled in the Ostreams themselves - - if (addSpace) os << token::SPACE; - - os.write(tok); - - addSpace = true; // Separate from following tokens + os << tok; // Revert to normal '<<' output operator } - else - { - if (addSpace) os << token::SPACE; - os << tok; - - addSpace = true; // Separate from following tokens - } + addSpace = true; // Separate from following tokens } if (!contentsOnly)