wordRe.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 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 \*---------------------------------------------------------------------------*/
25 
26 #include "wordRe.H"
27 #include "IOstreams.H"
28 #include "InfoProxy.H"
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
33 
34 
35 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
36 
38 :
39  word(),
40  re_(nullptr)
41 {
42  is >> *this;
43 }
44 
45 
46 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
47 
49 {
50  token t(is);
51 
52  if (!t.good())
53  {
54  is.setBad();
55  return is;
56  }
57 
58  if (t.isWord())
59  {
60  w = t.wordToken();
61  }
62  else if (t.isString())
63  {
64  // Auto-tests for regular expression
65  w = t.stringToken();
66 
67  // flag empty strings as an error
68  if (w.empty())
69  {
70  is.setBad();
72  << "empty word/expression "
73  << exit(FatalIOError);
74  return is;
75  }
76  }
77  else
78  {
79  is.setBad();
81  << "wrong token type - expected word or string, found "
82  << t.info()
83  << exit(FatalIOError);
84 
85  return is;
86  }
87 
88  // Check state of IOstream
89  is.check("Istream& operator>>(Istream&, wordRe&)");
90 
91  return is;
92 }
93 
94 
96 {
97  os.writeQuoted(w, w.isPattern());
98  os.check("Ostream& operator<<(Ostream&, const wordRe&)");
99  return os;
100 }
101 
102 
104 {
105  if (isPattern())
106  {
107  os << "wordRe(regex) " << *this;
108  }
109  else
110  {
111  os << "wordRe(plain) \"" << *this << '"';
112  }
113  os.flush();
114 
115  return os;
116 }
117 
118 
119 // ************************************************************************* //
bool isWord() const
Definition: tokenI.H:261
virtual void flush()=0
Flush stream.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:391
const word & wordToken() const
Definition: tokenI.H:266
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A token holds items read from Istream.
Definition: token.H:72
bool isPattern() const
Should be treated as a match rather than a literal string?
Definition: wordReI.H:121
virtual Ostream & writeQuoted(const std::string &, const bool quoted=true)=0
Write std::string surrounded by quotes.
Ostream & info(Ostream &) const
Output some basic info.
Definition: wordRe.C:103
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
void setBad()
Set stream to be bad.
Definition: IOstream.H:484
A class for handling words, derived from string.
Definition: word.H:59
static bool isPattern(const string &)
Test string for regular expression meta characters.
Definition: wordReI.H:34
Istream & operator>>(Istream &, directionInfo &)
wordRe()
Construct null.
Definition: wordReI.H:42
bool good() const
Definition: tokenI.H:228
A wordRe is a word, but can also have a regular expression for matching words.
Definition: wordRe.H:74
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
const string & stringToken() const
Definition: tokenI.H:320
bool isString() const
Definition: tokenI.H:315
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:335
Ostream & operator<<(Ostream &, const ensightPart &)
static const wordRe null
An empty wordRe.
Definition: wordRe.H:88
IOerror FatalIOError