From 2787a8664d7eb2b4522efc9676251c7d4af0fec3 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Sun, 26 Nov 2017 12:45:49 +0100 Subject: [PATCH] STYLE: relegate special purpose readList function to points of use - the readList(Istream&) function was introduced to handle command -options with either a single or a list value, but was also used for the #remove dictionary directive. However, the parsing was fragile if the list did not start with a '('. Now handle command-line arg/option list directly (via ITstream) and #remove with special-purpose reading of a string or word list. This removes ambiguity and reduces potential future problems. STYLE: use ITstream instead of IStringStream for command-line lookups - parses directly to a tokenList without a string copy. --- src/OpenFOAM/containers/Lists/List/List.H | 11 ---- src/OpenFOAM/containers/Lists/List/ListIO.C | 35 ----------- .../db/dictionary/functionEntries/README | 6 +- .../functionEntry/functionEntry.C | 4 +- .../functionEntry/functionEntry.H | 19 +++++- .../functionEntry/functionEntryTemplates.C | 61 +++++++++++++++++++ .../functionEntries/removeEntry/removeEntry.C | 2 +- src/OpenFOAM/global/argList/argList.C | 15 ++++- src/OpenFOAM/global/argList/argList.H | 6 +- src/OpenFOAM/global/argList/argListI.H | 27 ++++---- 10 files changed, 115 insertions(+), 71 deletions(-) create mode 100644 src/OpenFOAM/db/dictionary/functionEntries/functionEntry/functionEntryTemplates.C diff --git a/src/OpenFOAM/containers/Lists/List/List.H b/src/OpenFOAM/containers/Lists/List/List.H index 7fd057a02f..ec6ea7c515 100644 --- a/src/OpenFOAM/containers/Lists/List/List.H +++ b/src/OpenFOAM/containers/Lists/List/List.H @@ -308,17 +308,6 @@ public: }; -//- Read a bracket-delimited list, or handle a single value as list of size 1 -// For example, -// \code -// wList = readList(IStringStream("(patch1 patch2 patch3)")()); -// wList = readList(IStringStream("patch0")()); -// \endcode -// Mostly useful for handling command-line arguments -template -List readList(Istream& is); - - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam diff --git a/src/OpenFOAM/containers/Lists/List/ListIO.C b/src/OpenFOAM/containers/Lists/List/ListIO.C index aa8cd447f1..86850f91e9 100644 --- a/src/OpenFOAM/containers/Lists/List/ListIO.C +++ b/src/OpenFOAM/containers/Lists/List/ListIO.C @@ -159,39 +159,4 @@ Foam::Istream& Foam::operator>>(Istream& is, List& L) } -template -Foam::List Foam::readList(Istream& is) -{ - List L; - token firstToken(is); - is.putBack(firstToken); - - if (firstToken.isPunctuation()) - { - if (firstToken.pToken() != token::BEGIN_LIST) - { - FatalIOErrorInFunction(is) - << "incorrect first token, expected '(', found " - << firstToken.info() - << exit(FatalIOError); - } - - // Read as singly-linked list - SLList sll(is); - - // Convert the singly-linked list to this list - L = sll; - } - else - { - // Create list with a single item - L.setSize(1); - - is >> L[0]; - } - - return L; -} - - // ************************************************************************* // diff --git a/src/OpenFOAM/db/dictionary/functionEntries/README b/src/OpenFOAM/db/dictionary/functionEntries/README index c75c21043a..b16070ab98 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/README +++ b/src/OpenFOAM/db/dictionary/functionEntries/README @@ -7,7 +7,7 @@ #warn | dict | entry introducer #error | dict | entry introducer | | - #remove | dict | readList + #remove | dict | keyType or List | | #include | dict/primitive | string #includeEtc | dict/primitive | string @@ -24,7 +24,7 @@ Pending future extensions |-------------------|-------------------|-------------------|----------------- #define | dict | entry introducer #local | dict | entry introducer - #undef | dict | readList + #undef | dict | keyType or List -2017-11-05 +2017-11-26 diff --git a/src/OpenFOAM/db/dictionary/functionEntries/functionEntry/functionEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/functionEntry/functionEntry.C index 8eba79a926..974a91d8d1 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/functionEntry/functionEntry.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/functionEntry/functionEntry.C @@ -27,7 +27,7 @@ License #include "IOstreams.H" #include "ISstream.H" -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // namespace Foam { @@ -47,7 +47,7 @@ namespace Foam } -// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // +// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // Foam::token Foam::functionEntry::readLine(const word& key, Istream& is) { diff --git a/src/OpenFOAM/db/dictionary/functionEntries/functionEntry/functionEntry.H b/src/OpenFOAM/db/dictionary/functionEntries/functionEntry/functionEntry.H index 4d118c79ff..32dd478596 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/functionEntry/functionEntry.H +++ b/src/OpenFOAM/db/dictionary/functionEntries/functionEntry/functionEntry.H @@ -67,9 +67,6 @@ class functionEntry { // Private Member Functions - //- Read line as string token - static token readLine(const word& key, Istream& is); - //- Disallow default bitwise copy construct functionEntry(const functionEntry&) = delete; @@ -77,6 +74,16 @@ class functionEntry void operator=(const functionEntry&) = delete; +protected: + + //- Read line and return as a string token + static token readLine(const word& key, Istream& is); + + //- Read a List of strings values, treating a single entry like a + //- list of size 1. + template + static List readStringList(Istream& is); + public: // Constructors @@ -142,6 +149,12 @@ public: // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +#ifdef NoRepository + #include "functionEntryTemplates.C" +#endif + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + #endif // ************************************************************************* // diff --git a/src/OpenFOAM/db/dictionary/functionEntries/functionEntry/functionEntryTemplates.C b/src/OpenFOAM/db/dictionary/functionEntries/functionEntry/functionEntryTemplates.C new file mode 100644 index 0000000000..d7a23e9e74 --- /dev/null +++ b/src/OpenFOAM/db/dictionary/functionEntries/functionEntry/functionEntryTemplates.C @@ -0,0 +1,61 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2017 OpenCFD Ltd. + \\/ 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 "functionEntry.H" +#include "ISstream.H" + +// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // + +template +Foam::List +Foam::functionEntry::readStringList(Istream& is) +{ + ISstream& iss = dynamic_cast(is); + token firstToken(iss); + + List list; + + if (firstToken.isWord() || firstToken.isString()) + { + // The first token appears viable as non-list + // - treated like list with one entry + + iss.putBack(firstToken); + + list.setSize(1); + + iss >> list[0]; + } + else + { + iss.putBack(firstToken); + iss >> list; + } + + return list; +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/db/dictionary/functionEntries/removeEntry/removeEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/removeEntry/removeEntry.C index ebe2baf216..6a41293989 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/removeEntry/removeEntry.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/removeEntry/removeEntry.C @@ -54,7 +54,7 @@ bool Foam::functionEntries::removeEntry::execute Istream& is ) { - const List patterns = readList(is); + const List patterns = functionEntry::readStringList(is); for (const keyType& key : patterns) { diff --git a/src/OpenFOAM/global/argList/argList.C b/src/OpenFOAM/global/argList/argList.C index 2d53059de7..8aa7d30235 100644 --- a/src/OpenFOAM/global/argList/argList.C +++ b/src/OpenFOAM/global/argList/argList.C @@ -916,8 +916,19 @@ void Foam::argList::parse { distributed_ = true; source = "-roots"; - IStringStream is(options_["roots"]); - roots = readList(is); + ITstream is("roots", options_["roots"]); + + if (is.size() == 1) + { + // Single token - treated like list with one entry + roots.setSize(1); + + is >> roots[0]; + } + else + { + is >> roots; + } if (roots.size() != 1) { diff --git a/src/OpenFOAM/global/argList/argList.H b/src/OpenFOAM/global/argList/argList.H index d03b856c8f..fe4f5cb724 100644 --- a/src/OpenFOAM/global/argList/argList.H +++ b/src/OpenFOAM/global/argList/argList.H @@ -101,8 +101,8 @@ SourceFiles #include "word.H" #include "fileName.H" #include "parRun.H" -#include "StringStream.H" #include "OSspecific.H" +#include "ITstream.H" #include // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -302,8 +302,8 @@ public: //- Return true if the named option is found inline bool optionFound(const word& optionName) const; - //- Return an IStringStream from the named option - inline IStringStream optionLookup(const word& optionName) const; + //- Return an input token stream for the named option + inline ITstream optionLookup(const word& optionName) const; //- Read a value from the named option template diff --git a/src/OpenFOAM/global/argList/argListI.H b/src/OpenFOAM/global/argList/argListI.H index d8841ae3af..d3c733222f 100644 --- a/src/OpenFOAM/global/argList/argListI.H +++ b/src/OpenFOAM/global/argList/argListI.H @@ -24,7 +24,6 @@ License \*---------------------------------------------------------------------------*/ #include "argList.H" -#include "ITstream.H" // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // @@ -106,12 +105,12 @@ inline bool Foam::argList::optionFound(const word& optionName) const } -inline Foam::IStringStream Foam::argList::optionLookup +inline Foam::ITstream Foam::argList::optionLookup ( const word& optionName ) const { - return IStringStream(options_[optionName]); + return ITstream(optionName, options_[optionName]); } @@ -205,9 +204,12 @@ namespace Foam template inline T Foam::argList::argRead(const label argIndex) const { - T val; + ITstream is(Foam::name(argIndex), args_[argIndex]); - IStringStream(args_[argIndex])() >> val; + T val; + is >> val; + + // Could also check is.nRemainingTokens() to detect trailing rubbish return val; } @@ -215,9 +217,12 @@ inline T Foam::argList::argRead(const label argIndex) const template inline T Foam::argList::optionRead(const word& optionName) const { - T val; + ITstream is(optionName, options_[optionName]); - IStringStream(options_[optionName])() >> val; + T val; + is >> val; + + // Could also check is.nRemainingTokens() to detect trailing rubbish return val; } @@ -281,18 +286,18 @@ inline Foam::List Foam::argList::optionReadList { List list; - ITstream its(optionName, options_[optionName]); + ITstream is(optionName, options_[optionName]); - if (its.size() == 1) + if (is.size() == 1) { // Single token - treated like list with one entry list.setSize(1); - its >> list[0]; + is >> list[0]; } else { - its >> list; + is >> list; } return list;