Commit a0a9cd33 authored by Mark Olesen's avatar Mark Olesen
Browse files

introduce readList<T> function

- Read a bracket-delimited list, or handle a single value as list of size 1.
  Mostly useful for handling command-line arguments.
  eg,
      if (args.options().found("patches"))
      {
          patches = readList<word>(IStringStream(args.options()["patches"])());
      }
  can handle both of these:
      -patches  patch0
      -patches \( patch1 patch2 patch3 \)
parent 06517656
......@@ -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;
}
......
......@@ -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&);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -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;
}
// ************************************************************************* //
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment