From 91e00b40b428eb1d8011bc8f5ed2487c0e8f5ffb Mon Sep 17 00:00:00 2001 From: Henry Weller Date: Fri, 16 Aug 2019 11:35:46 +0100 Subject: [PATCH] verbatimString: New string type to handle the code blocks in codeStream without the need to handle the VERBATIMSTRING token type explicitly everywhere in the IO sub-system. Having a specific type is more consistent with the design and operation of token and much easier to maintain and extend. --- src/OpenFOAM/Make/files | 2 + src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H | 7 +- .../db/IOstreams/Pstreams/UIPstream.C | 28 ++-- .../db/IOstreams/Pstreams/UOPstream.C | 25 ++-- .../db/IOstreams/Pstreams/UOPstream.H | 6 +- src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C | 12 +- src/OpenFOAM/db/IOstreams/Sstreams/ISstream.H | 2 +- src/OpenFOAM/db/IOstreams/Sstreams/OSstream.C | 24 ++-- src/OpenFOAM/db/IOstreams/Sstreams/OSstream.H | 6 +- .../db/IOstreams/Sstreams/prefixOSstream.C | 14 +- .../db/IOstreams/Sstreams/prefixOSstream.H | 6 +- src/OpenFOAM/db/IOstreams/token/token.H | 13 ++ src/OpenFOAM/db/IOstreams/token/tokenI.H | 80 +++++++++-- src/OpenFOAM/db/IOstreams/token/tokenIO.C | 21 +-- .../functionEntry/functionEntry.C | 12 +- .../functionEntries/ifeqEntry/ifeqEntry.C | 16 +-- .../primitiveEntry/primitiveEntryIO.C | 12 +- .../dynamicCode/dynamicCodeContext.C | 14 +- .../codedFixedValuePointPatchField.C | 35 ++--- .../strings/verbatimString/verbatimString.C | 44 ++++++ .../strings/verbatimString/verbatimString.H | 128 ++++++++++++++++++ .../strings/verbatimString/verbatimStringI.H | 84 ++++++++++++ .../strings/verbatimString/verbatimStringIO.C | 83 ++++++++++++ .../codedFixedValueFvPatchField.C | 38 ++---- .../codedMixed/codedMixedFvPatchField.C | 38 ++---- 25 files changed, 547 insertions(+), 203 deletions(-) create mode 100644 src/OpenFOAM/primitives/strings/verbatimString/verbatimString.C create mode 100644 src/OpenFOAM/primitives/strings/verbatimString/verbatimString.H create mode 100644 src/OpenFOAM/primitives/strings/verbatimString/verbatimStringI.H create mode 100644 src/OpenFOAM/primitives/strings/verbatimString/verbatimStringIO.C diff --git a/src/OpenFOAM/Make/files b/src/OpenFOAM/Make/files index bc2d0afd0b..81448ff128 100644 --- a/src/OpenFOAM/Make/files +++ b/src/OpenFOAM/Make/files @@ -110,6 +110,8 @@ $(strings)/fileName/fileName.C $(strings)/fileName/fileNameIO.C $(strings)/variable/variable.C $(strings)/variable/variableIO.C +$(strings)/verbatimString/verbatimString.C +$(strings)/verbatimString/verbatimStringIO.C $(strings)/keyType/keyType.C $(strings)/wordRe/wordRe.C $(strings)/lists/hashedWordList.C diff --git a/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H b/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H index fafbaa07a0..7749ef28b7 100644 --- a/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H +++ b/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H @@ -38,6 +38,7 @@ SourceFiles #include "IOstream.H" #include "keyType.H" +#include "verbatimString.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -96,9 +97,6 @@ public: // Write functions - //- Write next token to stream - virtual Ostream& writeVerbatim(const token&) = 0; - //- Write character virtual Ostream& write(const char) = 0; @@ -116,6 +114,9 @@ public: //- Write string virtual Ostream& write(const string&) = 0; + //- Write verbatimString + virtual Ostream& write(const verbatimString&) = 0; + //- Write std::string surrounded by quotes. // Optional write without quotes. virtual Ostream& writeQuoted diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.C b/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.C index 9e8c466d9d..a5e23f62b1 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.C +++ b/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.C @@ -165,8 +165,17 @@ Foam::Istream& Foam::UIPstream::read(token& t) return *this; } - // Verbatim string - case token::VERBATIMSTRING : + // Variable + case token::VARIABLE : + { + FatalErrorInFunction + << "Binary IO of variables not supported" + << Foam::abort(FatalError); + return *this; + } + + // String + case token::STRING : { string* pval = new string; if (read(*pval)) @@ -181,19 +190,10 @@ Foam::Istream& Foam::UIPstream::read(token& t) return *this; } - // Variable - case token::VARIABLE : + // Verbatim string + case token::VERBATIMSTRING : { - FatalErrorInFunction - << "Binary IO of variables not supported" - << Foam::abort(FatalError); - return *this; - } - - // String - case token::STRING : - { - string* pval = new string; + verbatimString* pval = new verbatimString; if (read(*pval)) { t = pval; diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.C b/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.C index 6b14b15cae..c931858a2f 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.C +++ b/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.C @@ -150,19 +150,6 @@ Foam::UOPstream::~UOPstream() // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // -Foam::Ostream& Foam::UOPstream::writeVerbatim(const token& t) -{ - writeToBuffer(char(token::VERBATIMSTRING)); - - const string& str = t.stringToken(); - size_t len = str.size(); - writeToBuffer(len); - writeToBuffer(str.c_str(), len + 1, 1); - - return *this; -} - - Foam::Ostream& Foam::UOPstream::write(const char c) { if (!isspace(c)) @@ -217,6 +204,18 @@ Foam::Ostream& Foam::UOPstream::write(const string& str) } +Foam::Ostream& Foam::UOPstream::write(const verbatimString& vs) +{ + writeToBuffer(char(token::VERBATIMSTRING)); + + size_t len = vs.size(); + writeToBuffer(len); + writeToBuffer(vs.c_str(), len + 1, 1); + + return *this; +} + + Foam::Ostream& Foam::UOPstream::writeQuoted ( const std::string& str, diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.H b/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.H index dc34398714..5fc0528f56 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.H +++ b/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.H @@ -133,9 +133,6 @@ public: const label communicator = 0 ); - //- Write next token to stream - Ostream& writeVerbatim(const token&); - //- Write character Ostream& write(const char); @@ -148,6 +145,9 @@ public: //- Write string Ostream& write(const string&); + //- Write verbatimString + Ostream& write(const verbatimString&); + //- Write std::string surrounded by quotes. // Optional write without quotes. Ostream& writeQuoted diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C b/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C index 82ac08516b..e62c6c1a48 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C +++ b/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C @@ -230,19 +230,17 @@ Foam::Istream& Foam::ISstream::read(token& t) else if (nextC == token::BEGIN_BLOCK) { // Verbatim string - string* sPtr = new string; + verbatimString* vsPtr = new verbatimString; - if (readVerbatim(*sPtr).bad()) + if (readVerbatim(*vsPtr).bad()) { - delete sPtr; + delete vsPtr; t.setBad(); } else { - t = sPtr; - t.type() = token::VERBATIMSTRING; + t = vsPtr; } - return *this; } else @@ -715,7 +713,7 @@ Foam::Istream& Foam::ISstream::readVariable(string& str) } -Foam::Istream& Foam::ISstream::readVerbatim(string& str) +Foam::Istream& Foam::ISstream::readVerbatim(verbatimString& str) { static const int maxLen = 8000; static const int errLen = 80; // truncate error message for readability diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.H b/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.H index f21011e0e8..29cb9193f9 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.H +++ b/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.H @@ -64,7 +64,7 @@ class ISstream char nextValid(); //- Read a verbatim string (excluding block delimiters). - Istream& readVerbatim(string&); + Istream& readVerbatim(verbatimString&); //- Read a variable name Istream& readVariable(string&); diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.C b/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.C index bbcb501999..3abe0dde08 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.C +++ b/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.C @@ -29,20 +29,6 @@ License // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -Foam::Ostream& Foam::OSstream::writeVerbatim(const token& t) -{ - if (t.type() == token::VERBATIMSTRING) - { - write(char(token::HASH)); - write(char(token::BEGIN_BLOCK)); - writeQuoted(t.stringToken(), false); - write(char(token::HASH)); - write(char(token::END_BLOCK)); - } - return *this; -} - - Foam::Ostream& Foam::OSstream::write(const char c) { os_ << c; @@ -78,6 +64,16 @@ Foam::Ostream& Foam::OSstream::write(const string& str) } +Foam::Ostream& Foam::OSstream::write(const verbatimString& vs) +{ + os_ << token::HASH << token::BEGIN_BLOCK; + writeQuoted(vs, false); + os_ << token::HASH << token::END_BLOCK; + setState(os_.rdstate()); + return *this; +} + + Foam::Ostream& Foam::OSstream::writeQuoted ( const std::string& str, diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.H b/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.H index b8d0f69bba..8d54a2d755 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.H +++ b/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.H @@ -98,9 +98,6 @@ public: // Write functions - //- Write next token to stream - virtual Ostream& writeVerbatim(const token&); - //- Write character virtual Ostream& write(const char); @@ -113,6 +110,9 @@ public: //- Write string with double quotes virtual Ostream& write(const string&); + //- Write verbatimString with #{ }# + virtual Ostream& write(const verbatimString&); + //- Write std::string with optional double quotes. virtual Ostream& writeQuoted ( diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/prefixOSstream.C b/src/OpenFOAM/db/IOstreams/Sstreams/prefixOSstream.C index 76987e3141..5fdb7ffa60 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/prefixOSstream.C +++ b/src/OpenFOAM/db/IOstreams/Sstreams/prefixOSstream.C @@ -65,13 +65,6 @@ void Foam::prefixOSstream::print(Ostream& os) const } -Foam::Ostream& Foam::prefixOSstream::writeVerbatim(const token& t) -{ - checkWritePrefix(); - return OSstream::writeVerbatim(t); -} - - Foam::Ostream& Foam::prefixOSstream::write(const char c) { checkWritePrefix(); @@ -115,6 +108,13 @@ Foam::Ostream& Foam::prefixOSstream::write(const string& val) } +Foam::Ostream& Foam::prefixOSstream::write(const verbatimString& vs) +{ + checkWritePrefix(); + return OSstream::write(vs); +} + + Foam::Ostream& Foam::prefixOSstream::writeQuoted ( const std::string& val, diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/prefixOSstream.H b/src/OpenFOAM/db/IOstreams/Sstreams/prefixOSstream.H index 4f311b56c2..80df1ad442 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/prefixOSstream.H +++ b/src/OpenFOAM/db/IOstreams/Sstreams/prefixOSstream.H @@ -98,9 +98,6 @@ public: // Write functions - //- Write next token to stream - virtual Ostream& writeVerbatim(const token&); - //- Write character virtual Ostream& write(const char); @@ -113,6 +110,9 @@ public: //- Write string virtual Ostream& write(const string&); + //- Write verbatimString + virtual Ostream& write(const verbatimString&); + //- Write std::string surrounded by quotes. // Optional write without quotes. virtual Ostream& writeQuoted diff --git a/src/OpenFOAM/db/IOstreams/token/token.H b/src/OpenFOAM/db/IOstreams/token/token.H index 6dbb84c31c..ac74f4b0fe 100644 --- a/src/OpenFOAM/db/IOstreams/token/token.H +++ b/src/OpenFOAM/db/IOstreams/token/token.H @@ -42,6 +42,7 @@ SourceFiles #include "scalar.H" #include "word.H" #include "variable.H" +#include "verbatimString.H" #include "InfoProxy.H" #include "refCount.H" #include "typeInfo.H" @@ -249,6 +250,7 @@ private: word* wordTokenPtr_; variable* variableTokenPtr_; string* stringTokenPtr_; + verbatimString* verbatimStringTokenPtr_; label labelToken_; floatScalar floatScalarToken_; doubleScalar doubleScalarToken_; @@ -293,6 +295,9 @@ public: //- Construct string token inline token(const string&, label lineNumber=0); + //- Construct verbatimString token + inline token(const verbatimString&, label lineNumber=0); + //- Construct label token inline token(const label, label lineNumber=0); @@ -336,6 +341,9 @@ public: inline bool isString() const; inline const string& stringToken() const; + inline bool isVerbatimString() const; + inline const verbatimString& verbatimStringToken() const; + inline bool isLabel() const; inline label labelToken() const; @@ -395,6 +403,9 @@ public: inline void operator=(string*); inline void operator=(const string&); + inline void operator=(verbatimString*); + inline void operator=(const verbatimString&); + inline void operator=(const label); inline void operator=(const floatScalar); inline void operator=(const doubleScalar); @@ -410,6 +421,7 @@ public: inline bool operator==(const word&) const; inline bool operator==(const variable&) const; inline bool operator==(const string&) const; + inline bool operator==(const verbatimString&) const; inline bool operator==(const label) const; inline bool operator==(const floatScalar) const; inline bool operator==(const doubleScalar) const; @@ -423,6 +435,7 @@ public: inline bool operator!=(const word&) const; inline bool operator!=(const variable&) const; inline bool operator!=(const string&) const; + inline bool operator!=(const verbatimString&) const; inline bool operator!=(const label) const; inline bool operator!=(const floatScalar) const; inline bool operator!=(const doubleScalar) const; diff --git a/src/OpenFOAM/db/IOstreams/token/tokenI.H b/src/OpenFOAM/db/IOstreams/token/tokenI.H index a909d6bfd1..724158410b 100644 --- a/src/OpenFOAM/db/IOstreams/token/tokenI.H +++ b/src/OpenFOAM/db/IOstreams/token/tokenI.H @@ -37,10 +37,14 @@ inline void Foam::token::clear() { delete variableTokenPtr_; } - else if (type_ == STRING || type_ == VERBATIMSTRING) + else if (type_ == STRING) { delete stringTokenPtr_; } + else if (type_ == VERBATIMSTRING) + { + delete verbatimStringTokenPtr_; + } else if (type_ == LONG_DOUBLE_SCALAR) { delete longDoubleScalarTokenPtr_; @@ -93,10 +97,14 @@ inline Foam::token::token(const token& t) break; case STRING: - case VERBATIMSTRING: stringTokenPtr_ = new string(*t.stringTokenPtr_); break; + case VERBATIMSTRING: + verbatimStringTokenPtr_ = + new verbatimString(*t.verbatimStringTokenPtr_); + break; + case LABEL: labelToken_ = t.labelToken_; break; @@ -149,6 +157,14 @@ inline Foam::token::token(const string& s, label lineNumber) {} +inline Foam::token::token(const verbatimString& vs, label lineNumber) +: + type_(VERBATIMSTRING), + verbatimStringTokenPtr_(new verbatimString(vs)), + lineNumber_(lineNumber) +{} + + inline Foam::token::token(const label l, label lineNumber) : type_(LABEL), @@ -272,12 +288,12 @@ inline const Foam::variable& Foam::token::variableToken() const inline bool Foam::token::isString() const { - return (type_ == STRING || type_ == VERBATIMSTRING); + return (type_ == STRING); } inline const Foam::string& Foam::token::stringToken() const { - if (type_ == STRING || type_ == VERBATIMSTRING) + if (type_ == STRING) { return *stringTokenPtr_; } @@ -288,6 +304,24 @@ inline const Foam::string& Foam::token::stringToken() const } } +inline bool Foam::token::isVerbatimString() const +{ + return (type_ == VERBATIMSTRING); +} + +inline const Foam::verbatimString& Foam::token::verbatimStringToken() const +{ + if (type_ == VERBATIMSTRING) + { + return *verbatimStringTokenPtr_; + } + else + { + parseError(verbatimString::typeName); + return verbatimString::null; + } +} + inline bool Foam::token::isLabel() const { return (type_ == LABEL); @@ -478,10 +512,14 @@ inline void Foam::token::operator=(const token& t) break; case STRING: - case VERBATIMSTRING: stringTokenPtr_ = new string(*t.stringTokenPtr_); break; + case VERBATIMSTRING: + verbatimStringTokenPtr_ = + new verbatimString(*t.verbatimStringTokenPtr_); + break; + case LABEL: labelToken_ = t.labelToken_; break; @@ -554,6 +592,18 @@ inline void Foam::token::operator=(const string& s) operator=(new string(s)); } +inline void Foam::token::operator=(verbatimString* vsPtr) +{ + clear(); + type_ = VERBATIMSTRING; + verbatimStringTokenPtr_ = vsPtr; +} + +inline void Foam::token::operator=(const verbatimString& vs) +{ + operator=(new verbatimString(vs)); +} + inline void Foam::token::operator=(const label l) { clear(); @@ -612,9 +662,11 @@ inline bool Foam::token::operator==(const token& t) const return *variableTokenPtr_ == *t.variableTokenPtr_; case STRING: - case VERBATIMSTRING: return *stringTokenPtr_ == *t.stringTokenPtr_; + case VERBATIMSTRING: + return *verbatimStringTokenPtr_ == *t.verbatimStringTokenPtr_; + case LABEL: return labelToken_ == t.labelToken_; @@ -658,11 +710,12 @@ inline bool Foam::token::operator==(const variable& v) const inline bool Foam::token::operator==(const string& s) const { - return - ( - (type_ == STRING || type_ == VERBATIMSTRING) - && stringToken() == s - ); + return (type_ == STRING && stringToken() == s); +} + +inline bool Foam::token::operator==(const verbatimString& vs) const +{ + return (type_ == VERBATIMSTRING && verbatimStringToken() == vs); } inline bool Foam::token::operator==(const label l) const @@ -708,6 +761,11 @@ inline bool Foam::token::operator!=(const string& s) const return !operator==(s); } +inline bool Foam::token::operator!=(const verbatimString& vs) const +{ + return !operator==(vs); +} + inline bool Foam::token::operator!=(const floatScalar s) const { return !operator==(s); diff --git a/src/OpenFOAM/db/IOstreams/token/tokenIO.C b/src/OpenFOAM/db/IOstreams/token/tokenIO.C index c2d2f68135..602409a214 100644 --- a/src/OpenFOAM/db/IOstreams/token/tokenIO.C +++ b/src/OpenFOAM/db/IOstreams/token/tokenIO.C @@ -71,10 +71,13 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const token& t) break; case token::STRING: - case token::VERBATIMSTRING: os << *t.stringTokenPtr_; break; + case token::VERBATIMSTRING: + os << *t.verbatimStringTokenPtr_; + break; + case token::LABEL: os << t.labelToken_; break; @@ -162,12 +165,12 @@ ostream& Foam::operator<<(ostream& os, const InfoProxy& ip) os << " the string " << t.stringToken(); break; - case token::VARIABLE: - os << " the variable " << t.variableToken(); + case token::VERBATIMSTRING: + os << " the verbatim string " << t.verbatimStringToken(); break; - case token::VERBATIMSTRING: - os << " the verbatim string " << t.stringToken(); + case token::VARIABLE: + os << " the variable " << t.variableToken(); break; case token::LABEL: @@ -238,12 +241,12 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const InfoProxy& ip) os << " the string " << t.stringToken(); break; - case token::VARIABLE: - os << " the variable " << t.variableToken(); + case token::VERBATIMSTRING: + os << " the verbatim string " << t.verbatimStringToken(); break; - case token::VERBATIMSTRING: - os << " the verbatim string " << t.stringToken(); + case token::VARIABLE: + os << " the variable " << t.variableToken(); break; case token::LABEL: diff --git a/src/OpenFOAM/db/dictionary/functionEntries/functionEntry/functionEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/functionEntry/functionEntry.C index 873dd1b9d5..447f6a0ba7 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/functionEntry/functionEntry.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/functionEntry/functionEntry.C @@ -168,17 +168,7 @@ void Foam::functionEntry::write(Ostream& os) const for (label i=0; i(t1) + << exit(FatalIOError); return eqType; } return false; diff --git a/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntryIO.C b/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntryIO.C index d8c52a1913..99b73704b1 100644 --- a/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntryIO.C +++ b/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntryIO.C @@ -232,17 +232,7 @@ void Foam::primitiveEntry::write(Ostream& os, const bool contentsOnly) const for (label i=0; istream()), + stringOps::trim(verbatimString(codePtrs[i]->stream())), dict ) ); @@ -84,7 +84,11 @@ Foam::dynamicCodeContext::dynamicCodeContext if (optionsPtr) { options_ = - stringOps::expand(stringOps::trim(optionsPtr->stream()), dict); + stringOps::expand + ( + stringOps::trim(verbatimString(optionsPtr->stream())), + dict + ); } // Libs @@ -92,7 +96,11 @@ Foam::dynamicCodeContext::dynamicCodeContext if (libsPtr) { libs_ = - stringOps::expand(stringOps::trim(libsPtr->stream()), dict); + stringOps::expand + ( + stringOps::trim(verbatimString(libsPtr->stream())), + dict + ); } // Calculate SHA1 digest from all entries diff --git a/src/OpenFOAM/fields/pointPatchFields/derived/codedFixedValue/codedFixedValuePointPatchField.C b/src/OpenFOAM/fields/pointPatchFields/derived/codedFixedValue/codedFixedValuePointPatchField.C index 7871015fee..b66960f14c 100644 --- a/src/OpenFOAM/fields/pointPatchFields/derived/codedFixedValue/codedFixedValuePointPatchField.C +++ b/src/OpenFOAM/fields/pointPatchFields/derived/codedFixedValue/codedFixedValuePointPatchField.C @@ -350,51 +350,36 @@ void Foam::codedFixedValuePointPatchField::write(Ostream& os) const if (dict_.found("codeInclude")) { - os.writeKeyword("codeInclude") - << token::HASH << token::BEGIN_BLOCK; - - os.writeQuoted(string(dict_["codeInclude"]), false) - << token::HASH << token::END_BLOCK + os.writeKeyword("codeInclude"); + os.write(verbatimString(dict_["codeInclude"])) << token::END_STATEMENT << nl; } if (dict_.found("localCode")) { - os.writeKeyword("localCode") - << token::HASH << token::BEGIN_BLOCK; - - os.writeQuoted(string(dict_["localCode"]), false) - << token::HASH << token::END_BLOCK + os.writeKeyword("localCode"); + os.write(verbatimString(dict_["localCode"])) << token::END_STATEMENT << nl; } if (dict_.found("code")) { - os.writeKeyword("code") - << token::HASH << token::BEGIN_BLOCK; - - os.writeQuoted(string(dict_["code"]), false) - << token::HASH << token::END_BLOCK + os.writeKeyword("code"); + os.write(verbatimString(dict_["code"])) << token::END_STATEMENT << nl; } if (dict_.found("codeOptions")) { - os.writeKeyword("codeOptions") - << token::HASH << token::BEGIN_BLOCK; - - os.writeQuoted(string(dict_["codeOptions"]), false) - << token::HASH << token::END_BLOCK + os.writeKeyword("codeOptions"); + os.write(verbatimString(dict_["codeOptions"])) << token::END_STATEMENT << nl; } if (dict_.found("codeLibs")) { - os.writeKeyword("codeLibs") - << token::HASH << token::BEGIN_BLOCK; - - os.writeQuoted(string(dict_["codeLibs"]), false) - << token::HASH << token::END_BLOCK + os.writeKeyword("codeLibs"); + os.write(verbatimString(dict_["codeLibs"])) << token::END_STATEMENT << nl; } } diff --git a/src/OpenFOAM/primitives/strings/verbatimString/verbatimString.C b/src/OpenFOAM/primitives/strings/verbatimString/verbatimString.C new file mode 100644 index 0000000000..e015a1bf44 --- /dev/null +++ b/src/OpenFOAM/primitives/strings/verbatimString/verbatimString.C @@ -0,0 +1,44 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Copyright (C) 2019 OpenFOAM Foundation + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "verbatimString.H" +#include "debug.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +const char* const Foam::verbatimString::typeName = "verbatimString"; +int Foam::verbatimString::debug(Foam::debug::debugSwitch(verbatimString::typeName, 0)); +const Foam::verbatimString Foam::verbatimString::null; + + +// * * * * * * * * * * * * * * * IOstream Functions * * * * * * * * * * * * // + +void Foam::writeEntry(Ostream& os, const verbatimString& value) +{ + os << value; +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/strings/verbatimString/verbatimString.H b/src/OpenFOAM/primitives/strings/verbatimString/verbatimString.H new file mode 100644 index 0000000000..178a2237f3 --- /dev/null +++ b/src/OpenFOAM/primitives/strings/verbatimString/verbatimString.H @@ -0,0 +1,128 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Copyright (C) 2019 OpenFOAM Foundation + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Class + Foam::verbatimString + +Description + A class for handling verbatimStrings, derived from string. + + A verbatimString is a verbatim string of characters. + +SourceFiles + verbatimString.C + verbatimStringIO.C + +\*---------------------------------------------------------------------------*/ + +#ifndef verbatimString_H +#define verbatimString_H + +#include "string.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +// Forward declaration of friend functions and operators +class verbatimString; +inline verbatimString operator&(const verbatimString&, const verbatimString&); +Istream& operator>>(Istream&, verbatimString&); +Ostream& operator<<(Ostream&, const verbatimString&); + + +/*---------------------------------------------------------------------------*\ + Class verbatimString Declaration +\*---------------------------------------------------------------------------*/ + +class verbatimString +: + public string +{ + +public: + + // Static Data Members + + static const char* const typeName; + static int debug; + + //- An empty verbatimString + static const verbatimString null; + + + // Constructors + + //- Construct null + inline verbatimString(); + + //- Copy constructor + inline verbatimString(const verbatimString&); + + //- Copy constructor of character array + inline verbatimString(const char*); + + //- Copy constructor of string + inline verbatimString(const string&); + + //- Copy constructor of std::string + inline verbatimString(const std::string&); + + //- Construct from Istream + verbatimString(Istream&); + + + // Member Operators + + // Assignment + + inline void operator=(const verbatimString&); + inline void operator=(const string&); + inline void operator=(const std::string&); + inline void operator=(const char*); + + + // IOstream Operators + + friend Istream& operator>>(Istream&, verbatimString&); + friend Ostream& operator<<(Ostream&, const verbatimString&); +}; + + +void writeEntry(Ostream& os, const verbatimString& value); + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#include "verbatimStringI.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/strings/verbatimString/verbatimStringI.H b/src/OpenFOAM/primitives/strings/verbatimString/verbatimStringI.H new file mode 100644 index 0000000000..0ee9313266 --- /dev/null +++ b/src/OpenFOAM/primitives/strings/verbatimString/verbatimStringI.H @@ -0,0 +1,84 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Copyright (C) 2019 OpenFOAM Foundation + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +inline Foam::verbatimString::verbatimString(const verbatimString& w) +: + string(w) +{} + + +inline Foam::verbatimString::verbatimString() +: + string() +{} + + +inline Foam::verbatimString::verbatimString(const string& s) +: + string(s) +{} + + +inline Foam::verbatimString::verbatimString(const std::string& s) +: + string(s) +{} + + +inline Foam::verbatimString::verbatimString(const char* s) +: + string(s) +{} + + +// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // + +inline void Foam::verbatimString::operator=(const verbatimString& q) +{ + string::operator=(q); +} + + +inline void Foam::verbatimString::operator=(const string& q) +{ + string::operator=(q); +} + + +inline void Foam::verbatimString::operator=(const std::string& q) +{ + string::operator=(q); +} + + +inline void Foam::verbatimString::operator=(const char* q) +{ + string::operator=(q); +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/strings/verbatimString/verbatimStringIO.C b/src/OpenFOAM/primitives/strings/verbatimString/verbatimStringIO.C new file mode 100644 index 0000000000..b98263894a --- /dev/null +++ b/src/OpenFOAM/primitives/strings/verbatimString/verbatimStringIO.C @@ -0,0 +1,83 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Copyright (C) 2019 OpenFOAM Foundation + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "verbatimString.H" +#include "IOstreams.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +Foam::verbatimString::verbatimString(Istream& is) +: + string() +{ + is >> *this; +} + + +Foam::Istream& Foam::operator>>(Istream& is, verbatimString& vs) +{ + token t(is); + + if (!t.good()) + { + is.setBad(); + return is; + } + + if (t.isVerbatimString()) + { + vs = t.verbatimStringToken(); + } + else if (t.isString()) + { + vs = t.stringToken(); + } + else + { + is.setBad(); + FatalIOErrorInFunction(is) + << "wrong token type - expected verbatimString, found " + << t.info() + << exit(FatalIOError); + + return is; + } + + // Check state of IOstream + is.check("Istream& operator>>(Istream&, verbatimString&)"); + + return is; +} + + +Foam::Ostream& Foam::operator<<(Ostream& os, const verbatimString& w) +{ + os.write(w); + os.check("Ostream& operator<<(Ostream&, const verbatimString&)"); + return os; +} + + +// ************************************************************************* // diff --git a/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchField.C b/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchField.C index 0b2ec87491..ad9aa7e480 100644 --- a/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchField.C +++ b/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchField.C @@ -344,56 +344,40 @@ template void Foam::codedFixedValueFvPatchField::write(Ostream& os) const { fixedValueFvPatchField::write(os); - os.writeKeyword("name") << name_ - << token::END_STATEMENT << nl; + writeEntry(os, "name", name_); if (dict_.found("codeInclude")) { - os.writeKeyword("codeInclude") - << token::HASH << token::BEGIN_BLOCK; - - os.writeQuoted(string(dict_["codeInclude"]), false) - << token::HASH << token::END_BLOCK + os.writeKeyword("codeInclude"); + os.write(verbatimString(dict_["codeInclude"])) << token::END_STATEMENT << nl; } if (dict_.found("localCode")) { - os.writeKeyword("localCode") - << token::HASH << token::BEGIN_BLOCK; - - os.writeQuoted(string(dict_["localCode"]), false) - << token::HASH << token::END_BLOCK + os.writeKeyword("localCode"); + os.write(verbatimString(dict_["localCode"])) << token::END_STATEMENT << nl; } if (dict_.found("code")) { - os.writeKeyword("code") - << token::HASH << token::BEGIN_BLOCK; - - os.writeQuoted(string(dict_["code"]), false) - << token::HASH << token::END_BLOCK + os.writeKeyword("code"); + os.write(verbatimString(dict_["code"])) << token::END_STATEMENT << nl; } if (dict_.found("codeOptions")) { - os.writeKeyword("codeOptions") - << token::HASH << token::BEGIN_BLOCK; - - os.writeQuoted(string(dict_["codeOptions"]), false) - << token::HASH << token::END_BLOCK + os.writeKeyword("codeOptions"); + os.write(verbatimString(dict_["codeOptions"])) << token::END_STATEMENT << nl; } if (dict_.found("codeLibs")) { - os.writeKeyword("codeLibs") - << token::HASH << token::BEGIN_BLOCK; - - os.writeQuoted(string(dict_["codeLibs"]), false) - << token::HASH << token::END_BLOCK + os.writeKeyword("codeLibs"); + os.write(verbatimString(dict_["codeLibs"])) << token::END_STATEMENT << nl; } } diff --git a/src/finiteVolume/fields/fvPatchFields/derived/codedMixed/codedMixedFvPatchField.C b/src/finiteVolume/fields/fvPatchFields/derived/codedMixed/codedMixedFvPatchField.C index b7c0defe39..2f2d9249a2 100644 --- a/src/finiteVolume/fields/fvPatchFields/derived/codedMixed/codedMixedFvPatchField.C +++ b/src/finiteVolume/fields/fvPatchFields/derived/codedMixed/codedMixedFvPatchField.C @@ -357,56 +357,40 @@ template void Foam::codedMixedFvPatchField::write(Ostream& os) const { mixedFvPatchField::write(os); - os.writeKeyword("name") << name_ - << token::END_STATEMENT << nl; + writeEntry(os, "name", name_); if (dict_.found("codeInclude")) { - os.writeKeyword("codeInclude") - << token::HASH << token::BEGIN_BLOCK; - - os.writeQuoted(string(dict_["codeInclude"]), false) - << token::HASH << token::END_BLOCK + os.writeKeyword("codeInclude"); + os.write(verbatimString(dict_["codeInclude"])) << token::END_STATEMENT << nl; } if (dict_.found("localCode")) { - os.writeKeyword("localCode") - << token::HASH << token::BEGIN_BLOCK; - - os.writeQuoted(string(dict_["localCode"]), false) - << token::HASH << token::END_BLOCK + os.writeKeyword("localCode"); + os.write(verbatimString(dict_["localCode"])) << token::END_STATEMENT << nl; } if (dict_.found("code")) { - os.writeKeyword("code") - << token::HASH << token::BEGIN_BLOCK; - - os.writeQuoted(string(dict_["code"]), false) - << token::HASH << token::END_BLOCK + os.writeKeyword("code"); + os.write(verbatimString(dict_["code"])) << token::END_STATEMENT << nl; } if (dict_.found("codeOptions")) { - os.writeKeyword("codeOptions") - << token::HASH << token::BEGIN_BLOCK; - - os.writeQuoted(string(dict_["codeOptions"]), false) - << token::HASH << token::END_BLOCK + os.writeKeyword("codeOptions"); + os.write(verbatimString(dict_["codeOptions"])) << token::END_STATEMENT << nl; } if (dict_.found("codeLibs")) { - os.writeKeyword("codeLibs") - << token::HASH << token::BEGIN_BLOCK; - - os.writeQuoted(string(dict_["codeLibs"]), false) - << token::HASH << token::END_BLOCK + os.writeKeyword("codeLibs"); + os.write(verbatimString(dict_["codeLibs"])) << token::END_STATEMENT << nl; } }