diff --git a/applications/test/List/ListTest.C b/applications/test/List/ListTest.C index d4834cb1db2b02bc79f4d6e8508e9f3b3361c695..d06c65d98aa9740243accf3c69b3b57ca9918ff0 100644 --- a/applications/test/List/ListTest.C +++ b/applications/test/List/ListTest.C @@ -29,6 +29,8 @@ Description \*---------------------------------------------------------------------------*/ #include "OSspecific.H" +#include "argList.H" +#include "wordReList.H" #include "IOstreams.H" #include "IStringStream.H" @@ -38,11 +40,19 @@ Description using namespace Foam; + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // Main program: int main(int argc, char *argv[]) { + argList::noParallel(); + argList::validOptions.insert("reList", "reList"); + argList::validOptions.insert("wordList", "wordList"); + argList::validOptions.insert("stringList", "stringList"); + +# include "setRootCase.H" + List<vector> list1(IStringStream("1 ((0 1 2))")()); Info<< "list1: " << list1 << endl; @@ -69,6 +79,31 @@ int main(int argc, char *argv[]) Info<< "Elements " << map << " out of " << list3 << " => " << subList3 << endl; + wordReList reLst; + wordList wLst; + stringList sLst; + + + if (args.options().found("reList")) + { + reLst = readList<wordRe>(IStringStream(args.options()["reList"])()); + } + + if (args.options().found("wordList")) + { + wLst = readList<word>(IStringStream(args.options()["wordList"])()); + } + + if (args.options().found("stringList")) + { + sLst = readList<string>(IStringStream(args.options()["stringList"])()); + } + + Info<< nl + << "-reList: " << reLst << nl + << "-wordList: " << wLst << nl + << "-stringList: " << sLst << endl; + return 0; } diff --git a/src/OpenFOAM/containers/Lists/List/List.H b/src/OpenFOAM/containers/Lists/List/List.H index 639db9bbd20f62d1b66c32affcb5f0d268ad3631..434320cf5b21b980bbbbf286a57df2186e4e98fe 100644 --- a/src/OpenFOAM/containers/Lists/List/List.H +++ b/src/OpenFOAM/containers/Lists/List/List.H @@ -241,17 +241,27 @@ public: }; +//- Read a bracket-delimited list, or handle a single value as list of size 1. +// For example, +// @code +// wList = readList<word>(IStringStream("(patch1 patch2 patch3)")()); +// wList = readList<word>(IStringStream("patch0")()); +// @endcode +// Mostly useful for handling command-line arguments. template<class T> -void sort(List<T>& a); +List<T> readList(Istream&); + +template<class T> +void sort(List<T>&); template<class T, class Cmp> -void sort(List<T>& a, const Cmp&); +void sort(List<T>&, const Cmp&); template<class T> -void stableSort(List<T>& a); +void stableSort(List<T>&); template<class T, class Cmp> -void stableSort(List<T>& a, const Cmp&); +void stableSort(List<T>&, const Cmp&); // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/containers/Lists/List/ListIO.C b/src/OpenFOAM/containers/Lists/List/ListIO.C index d6238ae09108a455f29a28b070f05728d1adf098..2ba1d4f00e33205fd5fc3f891143cf79a0bdcc32 100644 --- a/src/OpenFOAM/containers/Lists/List/ListIO.C +++ b/src/OpenFOAM/containers/Lists/List/ListIO.C @@ -131,7 +131,7 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& L) if (firstToken.pToken() != token::BEGIN_LIST) { FatalIOErrorIn("operator>>(Istream&, List<T>&)", is) - << "incorrect first token, expected '(' or '{', found " + << "incorrect first token, expected '(', found " << firstToken.info() << exit(FatalIOError); } @@ -156,4 +156,37 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& L) return is; } + +template<class T> +Foam::List<T> Foam::readList(Istream& is) +{ + List<T> L; + token firstToken(is); + is.putBack(firstToken); + + if (firstToken.isPunctuation()) + { + if (firstToken.pToken() != token::BEGIN_LIST) + { + FatalIOErrorIn("readList<T>(Istream&)", is) + << "incorrect first token, expected '(', found " + << firstToken.info() + << exit(FatalIOError); + } + + // read via a singly-linked list + L = SLList<T>(is); + } + else + { + // create list with a single item + L.setSize(1); + + is >> L[0]; + } + + return L; +} + + // ************************************************************************* //