ENH: add ITstream::toString() method

- creates a std::string with space-delimited tokens, which can be sent
  to another application or even re-parsed into tokens
This commit is contained in:
Mark Olesen
2019-08-14 19:29:02 +02:00
committed by Andrew Heather
parent 771410e546
commit ba69505225
3 changed files with 38 additions and 3 deletions

View File

@ -67,6 +67,14 @@ int main(int argc, char *argv[])
dictionary dict(is); dictionary dict(is);
Info<< dict << endl; Info<< dict << endl;
for (const entry& e : dict)
{
if (e.isStream())
{
Info<< e.keyword() << " :: "
<< e.stream().toString() << nl;
}
}
} }
} }

View File

@ -27,6 +27,7 @@ License
#include "error.H" #include "error.H"
#include "ITstream.H" #include "ITstream.H"
#include "StringStream.H"
#include "UIListStream.H" #include "UIListStream.H"
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
@ -187,6 +188,28 @@ void Foam::ITstream::print(Ostream& os) const
} }
std::string Foam::ITstream::toString() const
{
OStringStream buf;
const tokenList& tokens = *this;
label len = tokens.size();
for (const token& tok : tokens)
{
buf << tok;
if (--len)
{
buf << ' ';
}
}
return buf.str();
}
Foam::Istream& Foam::ITstream::read(token& tok) Foam::Istream& Foam::ITstream::read(token& tok)
{ {
// Return the put back token if it exists // Return the put back token if it exists

View File

@ -192,7 +192,7 @@ public:
); );
// Member functions // Member Functions
// Inquiry // Inquiry
@ -233,7 +233,7 @@ public:
} }
// Read functions // Read Functions
//- Return next token from stream //- Return next token from stream
virtual Istream& read(token& tok); virtual Istream& read(token& tok);
@ -289,10 +289,14 @@ public:
} }
// Print // Output
//- Print description of stream to Ostream //- Print description of stream to Ostream
void print(Ostream& os) const; void print(Ostream& os) const;
//- Concatenate tokens into a space-separated std::string.
//- The resulting string may contain quote characters.
std::string toString() const;
}; };