Test-tokenize.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Application
25 
26 Description
27  Test the tokenizing of various things
28 \*---------------------------------------------------------------------------*/
29 
30 #include "argList.H"
31 #include "IOobject.H"
32 #include "IOstreams.H"
33 #include "IFstream.H"
34 #include "IStringStream.H"
35 #include "cpuTime.H"
36 
37 using namespace Foam;
38 
39 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
40 // Main program:
41 
42 int main(int argc, char *argv[])
43 {
45  argList::validArgs.insert("string .. stringN");
46  argList::addOption("file", "name");
47  argList::addOption("repeat", "count");
48 
49  argList args(argc, argv, false, true);
50 
51  const label repeat = args.optionLookupOrDefault<label>("repeat", 1);
52 
53  cpuTime timer;
54  for (label count = 0; count < repeat; ++count)
55  {
56  for (label argI=1; argI < args.size(); ++argI)
57  {
58  const string& rawArg = args[argI];
59  if (count == 0)
60  {
61  Info<< "input string: " << rawArg << nl;
62  }
63 
64  IStringStream is(rawArg);
65 
66  while (is.good())
67  {
68  token tok(is);
69  // char ch;
70  // is.get(ch);
71  // is.putback(ch);
72  int lookahead = is.peek();
73 
74  if (count == 0)
75  {
76  Info<< "token: " << tok.info();
77  Info<< " lookahead: '" << char(lookahead) << "'" << endl;
78  }
79  }
80 
81  if (count == 0)
82  {
83  Info<< nl;
85  }
86  }
87  }
88 
89  Info<< "tokenized args " << repeat << " times in "
90  << timer.cpuTimeIncrement() << " s\n\n";
91 
92  if (args.optionFound("file"))
93  {
94  for (label count = 0; count < repeat; ++count)
95  {
96  IFstream is(args["file"]);
97 
98  if (count == 0)
99  {
100  Info<< "tokenizing file: " << args["file"] << nl;
101  }
102 
103  while (is.good())
104  {
105  token tok(is);
106  if (count == 0)
107  {
108  Info<< "token: " << tok.info() << endl;
109  }
110  }
111 
112  if (count == 0)
113  {
114  Info<< nl;
116  }
117  }
118 
119  Info<< "tokenized file " << repeat << " times in "
120  << timer.cpuTimeIncrement() << " s\n\n";
121  }
122 
123  return 0;
124 }
125 
126 // ************************************************************************* //
Foam::argList::validArgs
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:143
Foam::cpuTime
Starts timing CPU usage and return elapsed time from start.
Definition: cpuTime.H:52
Foam::ISstream::peek
int peek()
Raw, low-level peek function.
Definition: ISstreamI.H:71
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::argList::addOption
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:108
Foam::IFstream
Input from file stream.
Definition: IFstream.H:81
Foam::argList::size
label size() const
Return the number of arguments.
Definition: argListI.H:84
Foam::argList
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:97
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
main
int main(int argc, char *argv[])
Definition: Test-tokenize.C:39
Foam::token
A token holds items read from Istream.
Definition: token.H:67
IStringStream.H
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::token::info
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:372
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
argList.H
IFstream.H
Foam::argList::optionLookupOrDefault
T optionLookupOrDefault(const word &opt, const T &deflt) const
Read a value from the named option if present.
Definition: argListI.H:237
IOobject.H
Foam::IStringStream
Input from memory buffer stream.
Definition: IStringStream.H:49
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
cpuTime.H
Foam::IOobject::writeDivider
static Stream & writeDivider(Stream &os)
Write the standard file section divider.
Definition: IOobjectI.H:108
Foam::timer
Implements a timeout mechanism via sigalarm.
Definition: timer.H:81
Foam::argList::optionFound
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
Foam::argList::noParallel
static void noParallel()
Remove the parallel options.
Definition: argList.C:161
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
args
Foam::argList args(argc, argv)