Test-wordRe.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 Description
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "IOstreams.H"
29 #include "IOobject.H"
30 #include "IFstream.H"
31 #include "List.H"
32 #include "Tuple2.H"
33 #include "wordRe.H"
34 
35 using namespace Foam;
36 
37 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
38 // Main program:
39 
40 int main(int argc, char *argv[])
41 {
42  wordRe wre;
43  std::string s1("this .* file");
44  Foam::string s2("this .* file");
45  const char * s3 = "this .* file";
46 
48  wordRe(s2).info(Info) << endl;
51 
52  wre = "this .* file";
53  wre.info(Info) << endl;
54  wre = s1;
55  wre.info(Info) << endl;
56  wre.uncompile();
57  wre.info(Info) << endl;
58 
59  wre = "something";
60  wre.info(Info) << " before" << endl;
61  wre.uncompile();
62  wre.info(Info) << " uncompiled" << endl;
64  wre.info(Info) << " after DETECT" << endl;
66  wre.info(Info) << " after NOCASE" << endl;
68  wre.info(Info) << " after DETECT_NOCASE" << endl;
69 
70  wre = "something .* value";
71  wre.info(Info) << " before" << endl;
72  wre.uncompile();
73  wre.info(Info) << " uncompiled" << endl;
75  wre.info(Info) << " after DETECT" << endl;
76  wre.uncompile();
77  wre.info(Info) << " uncompiled" << endl;
78  wre.recompile();
79  wre.info(Info) << " recompiled" << endl;
80 
81  wre.set("something .* value", wordRe::LITERAL);
82  wre.info(Info) << " set as LITERAL" << endl;
83 
85 
86  List<Tuple2<wordRe, string> > rawList(IFstream("testRegexps")());
87  Info<< "input list:" << rawList << endl;
89 
90  forAll(rawList, elemI)
91  {
92  const wordRe& wre = rawList[elemI].first();
93  const string& str = rawList[elemI].second();
94 
95  wre.info(Info)
96  << " equals:" << (wre == str)
97  << "(" << wre.match(str, true) << ")"
98  << " match:" << wre.match(str)
99  << " str=" << str
100  << endl;
101 
102  wordRe wre2;
103  wre2.set(wre, wordRe::NOCASE);
104 
105  wre2.info(Info)
106  << " match:" << wre2.match(str)
107  << " str=" << str
108  << endl;
109 
110  }
111 
112  Info<< endl;
113 
114  return 0;
115 }
116 
117 
118 // ************************************************************************* //
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
List.H
Foam::wordRe::NOCASE
@ NOCASE
Definition: wordRe.H:100
Foam::wordRe::DETECT_NOCASE
@ DETECT_NOCASE
Definition: wordRe.H:101
Foam::wordRe::REGEXP
@ REGEXP
Definition: wordRe.H:99
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Tuple2.H
Foam::wordRe::info
Ostream & info(Ostream &) const
Output some basic info.
Definition: wordRe.C:103
Foam::IFstream
Input from file stream.
Definition: IFstream.H:81
Foam::wordRe::DETECT
@ DETECT
Definition: wordRe.H:98
Foam::wordRe::compile
bool compile() const
Compile the regular expression.
Definition: wordReI.H:161
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
main
int main(int argc, char *argv[])
Definition: Test-wordRe.C:40
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:74
Foam::wordRe::match
bool match(const std::string &, bool literalMatch=false) const
Smart match as regular expression or as a string.
Definition: wordReI.H:202
Foam::wordRe
A wordRe is a word, but can also have a regular expression for matching words.
Definition: wordRe.H:74
Foam::wordRe::set
void set(const std::string &, const compOption=DETECT)
Copy string, auto-test for regular expression or other options.
Definition: wordReI.H:223
Foam::Info
messageStream Info
Foam::wordRe::recompile
bool recompile() const
Recompile an existing regular expression.
Definition: wordReI.H:168
wordRe.H
Foam::wordRe::uncompile
void uncompile(const bool doStripInvalid=false) const
Frees precompiled regular expression, making wordRe a literal.
Definition: wordReI.H:179
IFstream.H
Foam::wordRe::LITERAL
@ LITERAL
Definition: wordRe.H:97
IOobject.H
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::IOobject::writeDivider
static Stream & writeDivider(Stream &os)
Write the standard file section divider.
Definition: IOobjectI.H:108
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59