ENH: static parse methods for ITstream

This commit is contained in:
Mark Olesen
2017-11-09 08:39:36 +01:00
parent cea5ca147d
commit 59e8c39747
5 changed files with 115 additions and 33 deletions

View File

@ -236,23 +236,23 @@ public:
inline void append(const UIndirectList<T>& lst); inline void append(const UIndirectList<T>& lst);
//- Transfer the contents of the argument List into this list //- Transfer the contents of the argument List into this list
// and annul the argument list //- and annul the argument list
void transfer(List<T>& lst); void transfer(List<T>& lst);
//- Transfer the contents of the argument List into this list //- Transfer the contents of the argument List into this list
// and annul the argument list //- and annul the argument list
template<int SizeMin> template<int SizeMin>
void transfer(DynamicList<T, SizeMin>& lst); void transfer(DynamicList<T, SizeMin>& lst);
//- Transfer the contents of the argument List into this list //- Transfer the contents of the argument List into this list
// and annul the argument list //- and annul the argument list
void transfer(SortableList<T>& lst); void transfer(SortableList<T>& lst);
//- Transfer contents to the Xfer container //- Transfer contents to the Xfer container
inline Xfer<List<T>> xfer(); inline Xfer<List<T>> xfer();
//- Return subscript-checked element of UList. //- Return subscript-checked element of UList and resizing the list
// Resize list if required. //- if required.
inline T& newElmt(const label i); inline T& newElmt(const label i);
@ -301,7 +301,10 @@ public:
//- Read List from Istream, discarding contents of existing List //- Read List from Istream, discarding contents of existing List
friend Istream& operator>> <T> friend Istream& operator>> <T>
(Istream& is, List<T>& L); (
Istream& is,
List<T>& L
);
}; };

View File

@ -27,23 +27,72 @@ License
#include "ITstream.H" #include "ITstream.H"
#include "UIListStream.H" #include "UIListStream.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
void Foam::ITstream::toTokenList(ISstream& is) Foam::label Foam::ITstream::parseStream(ISstream& is, tokenList& tokens)
{ {
tokenIndex_ = 0; label nTok = 0;
tokens.clear();
tokens.setSize(64, token::undefinedToken);
token tok; token tok;
while (!is.read(tok).bad() && tok.good()) while (!is.read(tok).bad() && tok.good())
{ {
newElmt(tokenIndex()++) = std::move(tok); tokens.newElmt(nTok++) = std::move(tok);
} }
tokenList::setSize(tokenIndex()); tokens.setSize(nTok);
setOpened(); return nTok;
ITstream::rewind(); }
Foam::tokenList Foam::ITstream::parse
(
const UList<char>& input,
streamFormat format
)
{
UIListStream is(input, format, IOstream::currentVersion);
tokenList tokens;
parseStream(is, tokens);
return tokens;
}
Foam::tokenList Foam::ITstream::parse
(
const std::string& input,
streamFormat format
)
{
UIListStream is
(
input.data(),
input.size(),
format,
IOstream::currentVersion
);
tokenList tokens;
parseStream(is, tokens);
return tokens;
}
Foam::tokenList Foam::ITstream::parse
(
const char* input,
streamFormat format
)
{
UIListStream is(input, strlen(input), format, IOstream::currentVersion);
tokenList tokens;
parseStream(is, tokens);
return tokens;
} }
@ -58,13 +107,14 @@ Foam::ITstream::ITstream
) )
: :
Istream(format, version), Istream(format, version),
tokenList(16, token::undefinedToken), tokenList(),
name_(name), name_(name),
tokenIndex_(0) tokenIndex_(0)
{ {
UIListStream is(input, format, version); UIListStream is(input, format, version);
toTokenList(is); parseStream(is, static_cast<tokenList&>(*this));
ITstream::rewind();
} }
@ -77,13 +127,14 @@ Foam::ITstream::ITstream
) )
: :
Istream(format, version), Istream(format, version),
tokenList(16, token::undefinedToken), tokenList(),
name_(name), name_(name),
tokenIndex_(0) tokenIndex_(0)
{ {
UIListStream is(input.data(), input.size(), format, version); UIListStream is(input.data(), input.size(), format, version);
toTokenList(is); parseStream(is, static_cast<tokenList&>(*this));
ITstream::rewind();
} }
@ -96,14 +147,14 @@ Foam::ITstream::ITstream
) )
: :
Istream(format, version), Istream(format, version),
tokenList(16, token::undefinedToken), tokenList(),
name_(name), name_(name),
tokenIndex_(0) tokenIndex_(0)
{ {
const size_t len = strlen(input); UIListStream is(input, strlen(input), format, version);
UIListStream is(input, len, format, version);
toTokenList(is); parseStream(is, static_cast<tokenList&>(*this));
ITstream::rewind();
} }
@ -245,6 +296,7 @@ void Foam::ITstream::rewind()
lineNumber_ = tokenList::first().lineNumber(); lineNumber_ = tokenList::first().lineNumber();
} }
setOpened();
setGood(); setGood();
} }

View File

@ -25,7 +25,7 @@ Class
Foam::ITstream Foam::ITstream
Description Description
Input token stream. An input stream of tokens.
SourceFiles SourceFiles
ITstream.C ITstream.C
@ -46,7 +46,6 @@ namespace Foam
// Forward declaration // Forward declaration
class ISstream; class ISstream;
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class ITstream Declaration Class ITstream Declaration
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
@ -67,8 +66,9 @@ class ITstream
// Private Member Functions // Private Member Functions
//- Convert input sequence into tokens and append to the token list //- Convert input sequence into a list of tokens.
void toTokenList(ISstream& input); // \return the number of tokens in the resulting list.
static label parseStream(ISstream& input, tokenList& tokens);
public: public:
@ -164,6 +164,33 @@ public:
{} {}
// Static Functions
//- Create token list by parsing the input character sequence until
//- no good tokens remain.
static tokenList parse
(
const UList<char>& input,
streamFormat format=ASCII
);
//- Create token list by parsing the input string until
//- no good tokens remain.
static tokenList parse
(
const std::string& input,
streamFormat format=ASCII
);
//- Create token list by parsing the input character sequence until
//- no good tokens remain.
static tokenList parse
(
const char* input,
streamFormat format=ASCII
);
// Member functions // Member functions
// Inquiry // Inquiry

View File

@ -223,10 +223,7 @@ Foam::tokenList Foam::dictionary::tokens() const
} }
// String re-parsed as a list of tokens // String re-parsed as a list of tokens
return static_cast<tokenList> return ITstream::parse(os.str());
(
ITstream("tokens", os.str(), os.format(), os.version())
);
} }

View File

@ -96,15 +96,18 @@ bool Foam::primitiveEntry::expandVariable
return false; return false;
} }
// String parsed as a list of tokens // Parse string into a series of tokens
ITstream its("env", str);
appendTokenList(std::move(static_cast<tokenList&>(its))); tokenList toks(ITstream::parse(str, IOstream::ASCII));
appendTokenList(std::move(toks));
} }
else if (eptr->isDict()) else if (eptr->isDict())
{ {
// Found dictionary entry // Found dictionary entry
tokenList toks(eptr->dict().tokens().xfer()); tokenList toks(eptr->dict().tokens().xfer());
appendTokenList(std::move(toks)); appendTokenList(std::move(toks));
} }
else else