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 \)
This commit is contained in:
Mark Olesen
2009-05-18 12:05:52 +02:00
parent 06517656e8
commit a0a9cd3366
3 changed files with 85 additions and 7 deletions

View File

@ -29,6 +29,8 @@ Description
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "OSspecific.H" #include "OSspecific.H"
#include "argList.H"
#include "wordReList.H"
#include "IOstreams.H" #include "IOstreams.H"
#include "IStringStream.H" #include "IStringStream.H"
@ -38,11 +40,19 @@ Description
using namespace Foam; using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program: // Main program:
int main(int argc, char *argv[]) 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))")()); List<vector> list1(IStringStream("1 ((0 1 2))")());
Info<< "list1: " << list1 << endl; Info<< "list1: " << list1 << endl;
@ -69,6 +79,31 @@ int main(int argc, char *argv[])
Info<< "Elements " << map << " out of " << list3 Info<< "Elements " << map << " out of " << list3
<< " => " << subList3 << endl; << " => " << 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; return 0;
} }

View File

@ -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> template<class T>
void sort(List<T>& a); List<T> readList(Istream&);
template<class T, class Cmp>
void sort(List<T>& a, const Cmp&);
template<class T> template<class T>
void stableSort(List<T>& a); void sort(List<T>&);
template<class T, class Cmp> template<class T, class Cmp>
void stableSort(List<T>& a, const Cmp&); void sort(List<T>&, const Cmp&);
template<class T>
void stableSort(List<T>&);
template<class T, class Cmp>
void stableSort(List<T>&, const Cmp&);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -131,7 +131,7 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& L)
if (firstToken.pToken() != token::BEGIN_LIST) if (firstToken.pToken() != token::BEGIN_LIST)
{ {
FatalIOErrorIn("operator>>(Istream&, List<T>&)", is) FatalIOErrorIn("operator>>(Istream&, List<T>&)", is)
<< "incorrect first token, expected '(' or '{', found " << "incorrect first token, expected '(', found "
<< firstToken.info() << firstToken.info()
<< exit(FatalIOError); << exit(FatalIOError);
} }
@ -156,4 +156,37 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& L)
return is; 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;
}
// ************************************************************************* // // ************************************************************************* //