Skip to content
Snippets Groups Projects
Commit cae8a894 authored by Mark OLESEN's avatar Mark OLESEN
Browse files

ENH: allow creation of ITstream by parsing string

parent 061a8585
Branches
Tags
No related merge requests found
Test-ITstream.C
EXE = $(FOAM_USER_APPBIN)/Test-ITstream
/* EXE_INC = */
/* EXE_LIBS = */
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2017 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Description
\*---------------------------------------------------------------------------*/
#include "ListStream.H"
#include "UListStream.H"
#include "wordList.H"
#include "IOstreams.H"
#include "argList.H"
#include "ITstream.H"
using namespace Foam;
template<class T>
Ostream& toString(Ostream& os, const T& str)
{
os << str;
return os;
}
template<>
Ostream& toString(Ostream& os, const UList<char>& list)
{
for (const char c : list)
{
os << c;
}
return os;
}
template<>
Ostream& toString(Ostream& os, const List<char>& list)
{
for (const char c : list)
{
os << c;
}
return os;
}
void printTokens(Istream& is)
{
label count = 0;
token t;
while (is.good())
{
is >> t;
if (t.good())
{
++count;
Info<<"token: " << t << endl;
}
}
Info<< count << " tokens" << endl;
}
template<class BUF>
void doTest(const string& name, const BUF& input, bool verbose=false)
{
Info<<"test " << name.c_str() << ":" << nl
<<"====" << nl;
toString(Info, input)
<< nl
<<"====" << nl << endl;
ITstream its(name, input);
Info<< "got " << its.size() << " tokens - index at "
<< its.tokenIndex() << endl;
if (verbose)
{
for (const token& tok : its)
{
Info<< " " << tok.info() << nl;
}
Info<< nl;
}
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
const char* charInput =
"( const char input \"string\" to tokenize )"
"List<label> 5(0 1 2 3 4);";
string stringInput("( string ; input \"string\" to tokenize )");
List<char> listInput(stringInput.cbegin(), stringInput.cend());
doTest("char*", charInput, true);
doTest("string", stringInput, true);
doTest("List<char>", listInput, true);
reverse(listInput);
doTest("List<char>", listInput, true);
Info<< "\nEnd\n" << endl;
return 0;
}
// ************************************************************************* //
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -25,8 +25,89 @@ License
#include "error.H"
#include "ITstream.H"
#include "UIListStream.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
void Foam::ITstream::toTokenList(ISstream& is)
{
tokenIndex_ = 0;
token tok;
while (!is.read(tok).bad() && tok.good())
{
newElmt(tokenIndex()++) = std::move(tok);
}
tokenList::setSize(tokenIndex());
setOpened();
ITstream::rewind();
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::ITstream::ITstream
(
const string& name,
const UList<char>& input,
streamFormat format,
versionNumber version
)
:
Istream(format, version),
tokenList(16, token::undefinedToken),
name_(name),
tokenIndex_(0)
{
UIListStream is(input, format, version);
toTokenList(is);
}
Foam::ITstream::ITstream
(
const string& name,
const std::string& input,
streamFormat format,
versionNumber version
)
:
Istream(format, version),
tokenList(16, token::undefinedToken),
name_(name),
tokenIndex_(0)
{
UIListStream is(input.data(), input.size(), format, version);
toTokenList(is);
}
Foam::ITstream::ITstream
(
const string& name,
const char* input,
streamFormat format,
versionNumber version
)
:
Istream(format, version),
tokenList(16, token::undefinedToken),
name_(name),
tokenIndex_(0)
{
const size_t len = strlen(input);
UIListStream is(input, len, format, version);
toTokenList(is);
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void Foam::ITstream::print(Ostream& os) const
{
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -43,6 +43,10 @@ SourceFiles
namespace Foam
{
// Forward declaration
class ISstream;
/*---------------------------------------------------------------------------*\
Class ITstream Declaration
\*---------------------------------------------------------------------------*/
......@@ -61,6 +65,12 @@ class ITstream
label tokenIndex_;
// Private Member Functions
//- Convert input sequence into tokens and append to the token list
void toTokenList(ISstream& input);
public:
// Constructors
......@@ -103,6 +113,39 @@ public:
}
//- Construct token list by parsing the input character sequence
// Uses UIListStream internally.
ITstream
(
const string& name,
const UList<char>& input,
streamFormat format=ASCII,
versionNumber version=currentVersion
);
//- Construct token list by parsing the input string
// Uses UIListStream internally.
ITstream
(
const string& name,
const std::string& input,
streamFormat format=ASCII,
versionNumber version=currentVersion
);
//- Construct token list by parsing the input character sequence
// Uses UIListStream internally.
ITstream
(
const string& name,
const char* input,
streamFormat format=ASCII,
versionNumber version=currentVersion
);
//- Construct as copy
ITstream(const ITstream& its)
:
......
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