diff --git a/applications/test/dictionary/testDict b/applications/test/dictionary/testDict index a933c543e3..5fd1c07e86 100644 --- a/applications/test/dictionary/testDict +++ b/applications/test/dictionary/testDict @@ -95,6 +95,14 @@ boundaryField #include "testDict2" +verbatim #{ + + This is a somewhat larger chunk of verbatim text that we would much + prefer to move as a token rather than copying its entire content each + time we do parsing or need to resize the token list. + +#}; + foo { $active diff --git a/src/OpenFOAM/Make/files b/src/OpenFOAM/Make/files index 782d12b42f..cfe98bfe64 100644 --- a/src/OpenFOAM/Make/files +++ b/src/OpenFOAM/Make/files @@ -217,7 +217,6 @@ $(dictionaryEntry)/dictionaryEntry.C $(dictionaryEntry)/dictionaryEntryIO.C dictionaryListEntry = $(dictionary)/dictionaryListEntry -$(dictionaryListEntry)/dictionaryListEntry.C $(dictionaryListEntry)/dictionaryListEntryIO.C functionEntries = $(dictionary)/functionEntries diff --git a/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H b/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H index c8eb66976f..990e0924e8 100644 --- a/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H +++ b/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H @@ -97,7 +97,7 @@ public: // Write functions //- Write next token to stream - virtual Ostream& write(const token& t) = 0; + virtual Ostream& 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 b94ede4056..d9ffbd0ed9 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.C +++ b/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.C @@ -185,18 +185,18 @@ Foam::UOPstream::~UOPstream() // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // -Foam::Ostream& Foam::UOPstream::write(const token& t) +Foam::Ostream& Foam::UOPstream::write(const token& tok) { // Raw token output only supported for verbatim strings for now - if (t.type() == token::tokenType::VERBATIMSTRING) + if (tok.type() == token::tokenType::VERBATIMSTRING) { writeToBuffer(char(token::tokenType::VERBATIMSTRING)); - write(t.stringToken()); + write(tok.stringToken()); } - else if (t.type() == token::tokenType::VARIABLE) + else if (tok.type() == token::tokenType::VARIABLE) { writeToBuffer(char(token::tokenType::VARIABLE)); - write(t.stringToken()); + write(tok.stringToken()); } else { diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.H b/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.H index 364b10ccdd..c96b33ac8c 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.H +++ b/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.H @@ -147,7 +147,7 @@ public: ); //- Write next token to stream - Ostream& write(const token& t); + Ostream& write(const token& tok); //- Write single character. Whitespace is suppressed. Ostream& write(const char c); diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C b/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C index cc9a0e5a65..804eb97858 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C +++ b/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C @@ -164,7 +164,7 @@ Foam::Istream& Foam::ISstream::read(token& t) // Analyse input starting with this character. switch (c) { - // Check for punctuation first + // Check for punctuation first - same as token::isSeparator case token::END_STATEMENT : case token::BEGIN_LIST : @@ -288,7 +288,7 @@ Foam::Istream& Foam::ISstream::read(token& t) case '0' : case '1' : case '2' : case '3' : case '4' : case '5' : case '6' : case '7' : case '8' : case '9' : { - bool asLabel = (c != '.'); + label labelVal = (c != '.'); // used as bool here unsigned nChar = 0; buf[nChar++] = c; @@ -308,9 +308,9 @@ Foam::Istream& Foam::ISstream::read(token& t) ) ) { - if (asLabel) + if (labelVal) { - asLabel = isdigit(c); + labelVal = isdigit(c); } buf[nChar++] = c; @@ -344,16 +344,15 @@ Foam::Istream& Foam::ISstream::read(token& t) // A single '-' is punctuation t = token::punctuationToken(token::SUBTRACT); } + else if (labelVal && Foam::read(buf, labelVal)) + { + t = labelVal; + } else { - label labelVal; scalar scalarVal; - if (asLabel && Foam::read(buf, labelVal)) - { - t = labelVal; - } - else if (readScalar(buf, scalarVal)) + if (readScalar(buf, scalarVal)) { // A scalar or too big to fit as a label t = scalarVal; diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/ISstreamI.H b/src/OpenFOAM/db/IOstreams/Sstreams/ISstreamI.H index ecad21b417..6ceeb982c0 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/ISstreamI.H +++ b/src/OpenFOAM/db/IOstreams/Sstreams/ISstreamI.H @@ -59,7 +59,7 @@ inline Foam::ISstream& Foam::ISstream::get(char& c) if (good() && c == '\n') { - lineNumber_++; + ++lineNumber_; } return *this; @@ -76,7 +76,7 @@ inline Foam::ISstream& Foam::ISstream::getLine(string& str) { std::getline(is_, str); setState(is_.rdstate()); - lineNumber_++; + ++lineNumber_; return *this; } @@ -86,7 +86,7 @@ inline Foam::ISstream& Foam::ISstream::putback(const char c) { if (c == '\n') { - lineNumber_--; + --lineNumber_; } if (!is_.putback(c)) diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.C b/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.C index 3cdcf3e6d0..1ace27022c 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.C +++ b/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -28,21 +28,21 @@ License #include "OSstream.H" #include "stringOps.H" -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -Foam::Ostream& Foam::OSstream::write(const token& t) +Foam::Ostream& Foam::OSstream::write(const token& tok) { - if (t.type() == token::tokenType::VERBATIMSTRING) + if (tok.type() == token::tokenType::VERBATIMSTRING) { write(char(token::HASH)); write(char(token::BEGIN_BLOCK)); - writeQuoted(t.stringToken(), false); + writeQuoted(tok.stringToken(), false); write(char(token::HASH)); write(char(token::END_BLOCK)); } - else if (t.type() == token::tokenType::VARIABLE) + else if (tok.type() == token::tokenType::VARIABLE) { - writeQuoted(t.stringToken(), false); + writeQuoted(tok.stringToken(), false); } return *this; } @@ -77,36 +77,51 @@ Foam::Ostream& Foam::OSstream::write(const word& str) } -Foam::Ostream& Foam::OSstream::write(const string& str) +Foam::Ostream& Foam::OSstream::writeQuoted +( + const std::string& str, + const bool quoted +) { + if (!quoted) + { + // Output unquoted, only advance line number on newline + lineNumber_ += stringOps::count(str, token::NL); + os_ << str; + + setState(os_.rdstate()); + return *this; + } + + + // Output with surrounding quotes and backslash escaping os_ << token::BEGIN_STRING; - int backslash = 0; + unsigned backslash = 0; for (auto iter = str.cbegin(); iter != str.cend(); ++iter) { const char c = *iter; if (c == '\\') { - backslash++; - // suppress output until we know if other characters follow - continue; + ++backslash; + continue; // only output after escaped character is known } else if (c == token::NL) { - lineNumber_++; - backslash++; // backslash escape for newline + ++lineNumber_; + ++backslash; // backslash escape for newline } else if (c == token::END_STRING) { - backslash++; // backslash escape for quote + ++backslash; // backslash escape for quote } - // output pending backslashes + // output all pending backslashes while (backslash) { os_ << '\\'; - backslash--; + --backslash; } os_ << c; @@ -114,7 +129,6 @@ Foam::Ostream& Foam::OSstream::write(const string& str) // silently drop any trailing backslashes // they would otherwise appear like an escaped end-quote - os_ << token::END_STRING; setState(os_.rdstate()); @@ -122,60 +136,9 @@ Foam::Ostream& Foam::OSstream::write(const string& str) } -Foam::Ostream& Foam::OSstream::writeQuoted -( - const std::string& str, - const bool quoted -) +Foam::Ostream& Foam::OSstream::write(const string& str) { - if (quoted) - { - os_ << token::BEGIN_STRING; - - int backslash = 0; - for (auto iter = str.cbegin(); iter != str.cend(); ++iter) - { - const char c = *iter; - - if (c == '\\') - { - backslash++; - // suppress output until we know if other characters follow - continue; - } - else if (c == token::NL) - { - lineNumber_++; - backslash++; // backslash escape for newline - } - else if (c == token::END_STRING) - { - backslash++; // backslash escape for quote - } - - // output pending backslashes - while (backslash) - { - os_ << '\\'; - backslash--; - } - - os_ << c; - } - - // silently drop any trailing backslashes - // they would otherwise appear like an escaped end-quote - os_ << token::END_STRING; - } - else - { - // output unquoted string, only advance line number on newline - lineNumber_ += stringOps::count(str, token::NL); - os_ << str; - } - - setState(os_.rdstate()); - return *this; + return writeQuoted(str, true); } diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.H b/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.H index 2dd13c50fb..753bad7ca5 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.H +++ b/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.H @@ -105,7 +105,7 @@ public: // Write functions //- Write next token to stream - virtual Ostream& write(const token& t); + virtual Ostream& write(const token& tok); //- Write character virtual Ostream& write(const char c); @@ -116,7 +116,7 @@ public: //- Write word virtual Ostream& write(const word& str); - //- Write string + //- Write string (quoted) // In the rare case that the string contains a final trailing // backslash, it will be dropped to the appearance of an escaped // double-quote. diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/prefixOSstream.C b/src/OpenFOAM/db/IOstreams/Sstreams/prefixOSstream.C index 42a2df76c9..fcafc89f7f 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/prefixOSstream.C +++ b/src/OpenFOAM/db/IOstreams/Sstreams/prefixOSstream.C @@ -65,19 +65,19 @@ void Foam::prefixOSstream::print(Ostream& os) const } -Foam::Ostream& Foam::prefixOSstream::write(const token& t) +Foam::Ostream& Foam::prefixOSstream::write(const token& tok) { - if (t.type() == token::tokenType::VERBATIMSTRING) + if (tok.type() == token::tokenType::VERBATIMSTRING) { write(char(token::HASH)); write(char(token::BEGIN_BLOCK)); - writeQuoted(t.stringToken(), false); + writeQuoted(tok.stringToken(), false); write(char(token::HASH)); write(char(token::END_BLOCK)); } - else if (t.type() == token::tokenType::VARIABLE) + else if (tok.type() == token::tokenType::VARIABLE) { - writeQuoted(t.stringToken(), false); + writeQuoted(tok.stringToken(), false); } return *this; } diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/prefixOSstream.H b/src/OpenFOAM/db/IOstreams/Sstreams/prefixOSstream.H index 8912793a55..4c9eca7b44 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/prefixOSstream.H +++ b/src/OpenFOAM/db/IOstreams/Sstreams/prefixOSstream.H @@ -99,7 +99,7 @@ public: // Write functions //- Write next token to stream - virtual Ostream& write(const token& t); + virtual Ostream& write(const token& tok); //- Write character virtual Ostream& write(const char c); diff --git a/src/OpenFOAM/db/IOstreams/StringStreams/StringStream.H b/src/OpenFOAM/db/IOstreams/StringStreams/StringStream.H index ab9f335612..83f75c642a 100644 --- a/src/OpenFOAM/db/IOstreams/StringStreams/StringStream.H +++ b/src/OpenFOAM/db/IOstreams/StringStreams/StringStream.H @@ -2,8 +2,8 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2017 OpenCFD Ltd. + \\/ M anipulation | ------------------------------------------------------------------------------- License This file is part of OpenFOAM. diff --git a/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.C b/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.C index ca31271fa3..24258a1fa3 100644 --- a/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.C +++ b/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.C @@ -134,19 +134,19 @@ void Foam::ITstream::print(Ostream& os) const } -Foam::Istream& Foam::ITstream::read(token& t) +Foam::Istream& Foam::ITstream::read(token& tok) { // Return the put back token if it exists - if (Istream::getBack(t)) + if (Istream::getBack(tok)) { - lineNumber_ = t.lineNumber(); + lineNumber_ = tok.lineNumber(); return *this; } if (tokenIndex_ < size()) { - t = operator[](tokenIndex_++); - lineNumber_ = t.lineNumber(); + tok = operator[](tokenIndex_++); + lineNumber_ = tok.lineNumber(); if (tokenIndex_ == size()) { @@ -170,15 +170,15 @@ Foam::Istream& Foam::ITstream::read(token& t) setEof(); } - t = token::undefinedToken; + tok = token::undefinedToken; if (size()) { - t.lineNumber() = tokenList::last().lineNumber(); + tok.lineNumber() = tokenList::last().lineNumber(); } else { - t.lineNumber() = lineNumber(); + tok.lineNumber() = lineNumber(); } } @@ -238,6 +238,7 @@ Foam::Istream& Foam::ITstream::read(char*, std::streamsize) void Foam::ITstream::rewind() { tokenIndex_ = 0; + lineNumber_ = 0; if (size()) { diff --git a/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.H b/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.H index af9fff4bef..f6c96f1272 100644 --- a/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.H +++ b/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.H @@ -208,7 +208,7 @@ public: // Read functions //- Return next token from stream - virtual Istream& read(token& t); + virtual Istream& read(token& tok); //- Read a character virtual Istream& read(char&); diff --git a/src/OpenFOAM/db/dictionary/dictionary.C b/src/OpenFOAM/db/dictionary/dictionary.C index f4fc9d6336..6c4697e7ed 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.C +++ b/src/OpenFOAM/db/dictionary/dictionary.C @@ -217,14 +217,16 @@ Foam::tokenList Foam::dictionary::tokens() const // Serialize dictionary into a string OStringStream os; write(os, false); + IStringStream is(os.str()); - // Parse string as tokens DynamicList tokens; - token t; - while (is.read(t)) + + // Parse string as tokens + token tok; + while (is.read(tok)) { - tokens.append(t); + tokens.append(std::move(tok)); } return tokenList(tokens.xfer()); diff --git a/src/OpenFOAM/db/dictionary/dictionaryEntry/dictionaryEntry.C b/src/OpenFOAM/db/dictionary/dictionaryEntry/dictionaryEntry.C index 6167956e70..262c6b4fb4 100644 --- a/src/OpenFOAM/db/dictionary/dictionaryEntry/dictionaryEntry.C +++ b/src/OpenFOAM/db/dictionary/dictionaryEntry/dictionaryEntry.C @@ -58,10 +58,8 @@ Foam::label Foam::dictionaryEntry::startLineNumber() const { return first()->startLineNumber(); } - else - { - return -1; - } + + return -1; } @@ -71,10 +69,8 @@ Foam::label Foam::dictionaryEntry::endLineNumber() const { return last()->endLineNumber(); } - else - { - return -1; - } + + return -1; } diff --git a/src/OpenFOAM/db/dictionary/dictionaryListEntry/dictionaryListEntry.C b/src/OpenFOAM/db/dictionary/dictionaryListEntry/dictionaryListEntry.C deleted file mode 100644 index 055a55b59d..0000000000 --- a/src/OpenFOAM/db/dictionary/dictionaryListEntry/dictionaryListEntry.C +++ /dev/null @@ -1,40 +0,0 @@ -/*---------------------------------------------------------------------------*\ - ========= | - \\ / F ield | OpenFOAM: The Open Source CFD Toolbox - \\ / O peration | - \\ / A nd | Copyright (C) 2016 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 "dictionaryListEntry.H" - -// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // - -Foam::dictionaryListEntry::dictionaryListEntry -( - const dictionary& parentDict, - const dictionaryListEntry& dictEnt -) -: - dictionaryEntry(parentDict, dictEnt) -{} - - -// ************************************************************************* // diff --git a/src/OpenFOAM/db/dictionary/dictionaryListEntry/dictionaryListEntryIO.C b/src/OpenFOAM/db/dictionary/dictionaryListEntry/dictionaryListEntryIO.C index 53399cc543..041c107258 100644 --- a/src/OpenFOAM/db/dictionary/dictionaryListEntry/dictionaryListEntryIO.C +++ b/src/OpenFOAM/db/dictionary/dictionaryListEntry/dictionaryListEntryIO.C @@ -44,6 +44,16 @@ static Foam::label realSize(const Foam::dictionary& dict) // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // +Foam::dictionaryListEntry::dictionaryListEntry +( + const dictionary& parentDict, + const dictionaryListEntry& dictEnt +) +: + dictionaryEntry(parentDict, dictEnt) +{} + + Foam::dictionaryListEntry::dictionaryListEntry ( const dictionary& parentDict, diff --git a/src/OpenFOAM/db/dictionary/entry/entryIO.C b/src/OpenFOAM/db/dictionary/entry/entryIO.C index 14bf0beb54..b1fb4b92ba 100644 --- a/src/OpenFOAM/db/dictionary/entry/entryIO.C +++ b/src/OpenFOAM/db/dictionary/entry/entryIO.C @@ -56,7 +56,8 @@ bool Foam::entry::getKeyword(keyType& keyword, token& keyToken, Istream& is) keyword = keyToken.wordToken(); return true; } - else if (keyToken.isString()) + + if (keyToken.isString()) { // Enable wildcards keyword = keyToken.stringToken(); @@ -136,7 +137,8 @@ bool Foam::entry::New { return false; } - else if + + if ( keyToken.isLabel() || (keyToken.isPunctuation() && keyToken.pToken() == token::BEGIN_LIST) @@ -162,9 +164,10 @@ bool Foam::entry::New return false; } + if (keyword[0] == '#') { - // Function entry + // Function entry - #function if (disableFunctionEntries) { @@ -183,9 +186,11 @@ bool Foam::entry::New const word functionName(keyword.substr(1), false); return functionEntry::execute(functionName, parentDict, is); } - else if (!disableFunctionEntries && keyword[0] == '$') + + + if (!disableFunctionEntries && keyword[0] == '$') { - // Substitution entry + // Substitution entry - $variable token nextToken(is); is.putBack(nextToken); @@ -245,10 +250,10 @@ bool Foam::entry::New return true; } - else - { - // Normal or scoped entry + + // Normal or scoped entry + { token nextToken(is); is.putBack(nextToken); @@ -399,11 +404,9 @@ bool Foam::entry::New ); } } - else - { - // Some error finding/creating intermediate dictionaries - return false; - } + + // Some error finding/creating intermediate dictionaries + return false; } else { diff --git a/src/OpenFOAM/db/dictionary/functionEntries/README b/src/OpenFOAM/db/dictionary/functionEntries/README new file mode 100644 index 0000000000..0dc051a5d7 --- /dev/null +++ b/src/OpenFOAM/db/dictionary/functionEntries/README @@ -0,0 +1,30 @@ +| directive | context | content | line oriented? +|-------------------|-------------------|-------------------|----------------- + #inputMode | dict | word + #default | dict | entry introducer + #merge | dict | entry introducer + #overwrite | dict | entry introducer + #warn | dict | entry introducer + #error | dict | entry introducer + | | + #remove | dict | readList + | | + #include | dict/primitive | string + #includeEtc | dict/primitive | string + #includeIfPresent | dict/primitive | string + #includeFunc | dict | word + | | + #calcEntry | dict/primitive | string + #codeStream | dict/primitive | dictionary + + +Pending future extensions + +| directive | context | content | line oriented? +|-------------------|-------------------|-------------------|----------------- + #define | dict | entry introducer + #local | dict | entry introducer + #undef | dict | readList + + +2017-11-05 diff --git a/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.C index a7e2e2c949..4fa4a4a271 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.C @@ -66,7 +66,7 @@ bool Foam::functionEntries::calcEntry::execute Istream& is ) { - Info<< "Using #calcEntry at line " << is.lineNumber() + Info<< "Using #calc at line " << is.lineNumber() << " in file " << parentDict.name() << endl; dynamicCode::checkSecurity @@ -110,7 +110,7 @@ bool Foam::functionEntries::calcEntry::execute Istream& is ) { - Info<< "Using #calcEntry at line " << is.lineNumber() + Info<< "Using #calc at line " << is.lineNumber() << " in file " << parentDict.name() << endl; dynamicCode::checkSecurity diff --git a/src/OpenFOAM/db/dictionary/functionEntries/functionEntry/functionEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/functionEntry/functionEntry.C index 2798b924e3..8eba79a926 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/functionEntry/functionEntry.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/functionEntry/functionEntry.C @@ -160,13 +160,16 @@ bool Foam::functionEntry::execute void Foam::functionEntry::write(Ostream& os) const { // Contents should be single string token - const token& t = operator[](0); - const string& s = t.stringToken(); + const token& tok = operator[](0); + const string& s = tok.stringToken(); - for (size_t i = 0; i < s.size(); i++) + // Write character-wise for literal output + for (size_t i = 0; i < s.size(); ++i) { os.write(s[i]); } + + os << nl; } diff --git a/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.C b/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.C index 88750273e7..76a0c482c4 100644 --- a/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.C +++ b/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.C @@ -30,11 +30,23 @@ License // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // -void Foam::primitiveEntry::append(const UList& varTokens) +void Foam::primitiveEntry::appendTokenList(const UList& varTokens) { for (const token& tok : varTokens) { - newElmt(tokenIndex()++) = tok; + newElmt(tokenIndex()++) = tok; // append copy + } +} + + +void Foam::primitiveEntry::appendTokensFromString(const string& input) +{ + IStringStream is(input); + + token tok; + while (!is.read(tok).bad() && tok.good()) + { + newElmt(tokenIndex()++) = std::move(tok); } } @@ -85,17 +97,18 @@ bool Foam::primitiveEntry::expandVariable return false; } - append(tokenList(IStringStream('(' + str + ')')())); + // Split input string into a stream of tokens and append to list + appendTokensFromString(str); } else if (eptr->isDict()) { // Found dictionary entry - append(eptr->dict().tokens()); + appendTokenList(eptr->dict().tokens()); } else { // Found primitive entry - append(eptr->stream()); + appendTokenList(eptr->stream()); } return true; @@ -113,10 +126,10 @@ Foam::primitiveEntry::primitiveEntry(const keyType& key, const ITstream& is) } -Foam::primitiveEntry::primitiveEntry(const keyType& key, const token& t) +Foam::primitiveEntry::primitiveEntry(const keyType& key, const token& tok) : entry(key), - ITstream(key, tokenList(1, t)) + ITstream(key, tokenList(1, tok)) {} @@ -148,14 +161,12 @@ Foam::label Foam::primitiveEntry::startLineNumber() const { const tokenList& tokens = *this; - if (tokens.empty()) + if (tokens.size()) { - return -1; - } - else - { - return tokens.first().lineNumber(); + tokens.first().lineNumber(); } + + return -1; } @@ -163,14 +174,12 @@ Foam::label Foam::primitiveEntry::endLineNumber() const { const tokenList& tokens = *this; - if (tokens.empty()) - { - return -1; - } - else + if (tokens.size()) { return tokens.last().lineNumber(); } + + return -1; } diff --git a/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.H b/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.H index 38e9412a8d..59c2a75734 100644 --- a/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.H +++ b/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.H @@ -64,17 +64,25 @@ class primitiveEntry { // Private Member Functions - //- Append the given tokens starting at the current tokenIndex - void append(const UList& varTokens); - - //- Append the given token to this entry - void append + //- Test if token is acceptable after filtering for function entries + //- and variable expansions. + bool acceptToken ( - const token& currToken, + const token& tok, const dictionary& dict, Istream& is ); + //- Append the given tokens at the current tokenIndex + // No filtering on the tokens. + void appendTokenList(const UList& varTokens); + + //- Split input string into a stream of tokens and append at the + //- current tokenIndex. + // No filtering on the tokens. + void appendTokensFromString(const string& input); + + //- Expand the given variable. // The keyword starts with '$', but has been removed by the caller // and thus passed as a varName. @@ -113,7 +121,7 @@ public: primitiveEntry(const keyType& key, const ITstream& is); //- Construct from keyword and a single token - primitiveEntry(const keyType& key, const token& t); + primitiveEntry(const keyType& key, const token& tok); //- Construct from keyword and a list of tokens primitiveEntry(const keyType& key, const UList& tokens); @@ -123,7 +131,7 @@ public: //- Construct from keyword and a T template - primitiveEntry(const keyType& key, const T& t); + primitiveEntry(const keyType& key, const T& val); autoPtr clone(const dictionary&) const { diff --git a/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntryIO.C b/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntryIO.C index 6a040941e7..1d91eccf6c 100644 --- a/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntryIO.C +++ b/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntryIO.C @@ -28,18 +28,20 @@ License // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // -void Foam::primitiveEntry::append +bool Foam::primitiveEntry::acceptToken ( - const token& currToken, + const token& tok, const dictionary& dict, Istream& is ) { - if (currToken.isWord()) - { - const word& key = currToken.wordToken(); + bool accept = tok.good(); - if + if (tok.isWord()) + { + const word& key = tok.wordToken(); + + accept = ( disableFunctionEntries || key.size() == 1 @@ -47,16 +49,13 @@ void Foam::primitiveEntry::append !(key[0] == '$' && expandVariable(key.substr(1), dict)) && !(key[0] == '#' && expandFunction(key.substr(1), dict, is)) ) - ) - { - newElmt(tokenIndex()++) = currToken; - } + ); } - else if (currToken.isVariable()) + else if (tok.isVariable()) { - const string& key = currToken.stringToken(); + const string& key = tok.stringToken(); - if + accept = ( disableFunctionEntries || key.size() <= 3 @@ -65,15 +64,10 @@ void Foam::primitiveEntry::append && key[1] == token::BEGIN_BLOCK && expandVariable(key.substr(1), dict) ) - ) - { - newElmt(tokenIndex()++) = currToken; - } - } - else - { - newElmt(tokenIndex()++) = currToken; + ); } + + return accept; } @@ -92,65 +86,41 @@ bool Foam::primitiveEntry::read(const dictionary& dict, Istream& is) { is.fatalCheck(FUNCTION_NAME); - label blockCount = 0; - token currToken; + label depth = 0; + token tok; - if + while ( - !is.read(currToken).bad() - && currToken.good() - && currToken != token::END_STATEMENT + !is.read(tok).bad() && tok.good() + && !(tok == token::END_STATEMENT && depth == 0) ) { - append(currToken, dict, is); - - if - ( - currToken == token::BEGIN_BLOCK - || currToken == token::BEGIN_LIST - ) + if (tok.isPunctuation()) { - blockCount++; + const char c = tok.pToken(); + if (c == token::BEGIN_BLOCK || c == token::BEGIN_LIST) + { + ++depth; + } + else if (c == token::END_BLOCK || c == token::END_LIST) + { + --depth; + } } - while - ( - !is.read(currToken).bad() - && currToken.good() - && !(currToken == token::END_STATEMENT && blockCount == 0) - ) + if (acceptToken(tok, dict, is)) { - if - ( - currToken == token::BEGIN_BLOCK - || currToken == token::BEGIN_LIST - ) - { - blockCount++; - } - else if - ( - currToken == token::END_BLOCK - || currToken == token::END_LIST - ) - { - blockCount--; - } - - append(currToken, dict, is); + newElmt(tokenIndex()++) = std::move(tok); } + + // With/without move: clear any old content and force to have a + // known good token so that we can rely on it for the return value. + + tok = token::punctuationToken::NULL_TOKEN; } is.fatalCheck(FUNCTION_NAME); - - if (currToken.good()) - { - return true; - } - else - { - return false; - } + return tok.good(); } @@ -227,24 +197,27 @@ void Foam::primitiveEntry::write(Ostream& os, const bool contentsOnly) const os.writeKeyword(keyword()); } - bool space = false; // Separate from previous tokens with a space - for (const token& t : *this) + bool addSpace = false; // Separate from previous tokens with a space + for (const token& tok : *this) { - if (space) - { - os << token::SPACE; - } - space = true; // Prefix any following tokens - - if (t.type() == token::tokenType::VERBATIMSTRING) + if (tok.type() == token::tokenType::VERBATIMSTRING) { // Bypass token output operator to avoid losing verbatimness. - // Handle in Ostreams themselves - os.write(t); + // Handled in the Ostreams themselves + + if (addSpace) os << token::SPACE; + + os.write(tok); + + addSpace = true; // Separate from following tokens } else { - os << t; + if (addSpace) os << token::SPACE; + + os << tok; + + addSpace = true; // Separate from following tokens } } diff --git a/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntryTemplates.C b/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntryTemplates.C index 29f11a5c80..ce36746fee 100644 --- a/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntryTemplates.C +++ b/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntryTemplates.C @@ -30,13 +30,13 @@ License // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template -Foam::primitiveEntry::primitiveEntry(const keyType& key, const T& t) +Foam::primitiveEntry::primitiveEntry(const keyType& key, const T& val) : entry(key), ITstream(key, tokenList(10)) { OStringStream os; - os << t << token::END_STATEMENT; + os << val << token::END_STATEMENT; readEntry(dictionary::null, IStringStream(os.str())()); } diff --git a/src/surfMesh/surfaceFormats/obj/OBJstream.C b/src/surfMesh/surfaceFormats/obj/OBJstream.C index 2fcf7f4305..2056c51d5a 100644 --- a/src/surfMesh/surfaceFormats/obj/OBJstream.C +++ b/src/surfMesh/surfaceFormats/obj/OBJstream.C @@ -48,9 +48,10 @@ void Foam::OBJstream::writeAndCheck(const char c) startOfLine_ = false; if (c == 'v') { - nVertices_++; + ++nVertices_; } } + OFstream::write(c); } @@ -88,9 +89,9 @@ Foam::Ostream& Foam::OBJstream::write(const char c) Foam::Ostream& Foam::OBJstream::write(const char* str) { - for (const char* p = str; *p != '\0'; ++p) + for (const char* iter = str; *iter; ++iter) { - writeAndCheck(*p); + writeAndCheck(*iter); } return *this; } @@ -114,54 +115,53 @@ Foam::Ostream& Foam::OBJstream::writeQuoted const bool quoted ) { - if (quoted) + if (!quoted) { - OFstream::write(token::BEGIN_STRING); - - int backslash = 0; - for (auto iter = str.cbegin(); iter != str.cend(); ++iter) - { - const char c = *iter; - - if (c == '\\') - { - backslash++; - // suppress output until we know if other characters follow - continue; - } - else if (c == token::NL) - { - lineNumber_++; - backslash++; // backslash escape for newline - } - else if (c == token::END_STRING) - { - backslash++; // backslash escape for quote - } - - // output pending backslashes - while (backslash) - { - OFstream::write('\\'); - backslash--; - } - - writeAndCheck(c); - } - - // silently drop any trailing backslashes - // they would otherwise appear like an escaped end-quote - OFstream::write(token::END_STRING); - } - else - { - // output unquoted string, only advance line number on newline + // Output unquoted, only advance line number on newline for (auto iter = str.cbegin(); iter != str.cend(); ++iter) { writeAndCheck(*iter); } + return *this; } + + OFstream::write(token::BEGIN_STRING); + + unsigned backslash = 0; + for (auto iter = str.cbegin(); iter != str.cend(); ++iter) + { + const char c = *iter; + + if (c == '\\') + { + ++backslash; + continue; // only output after escaped character is known + } + else if (c == token::NL) + { + ++lineNumber_; + ++backslash; // backslash escape for newline + } + else if (c == token::END_STRING) + { + ++backslash; // backslash escape for quote + } + + // output all pending backslashes + while (backslash) + { + OFstream::write('\\'); + --backslash; + } + + writeAndCheck(c); + } + + // silently drop any trailing backslashes + // they would otherwise appear like an escaped end-quote + OFstream::write(token::END_STRING); + return *this; }