keyType.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 Description
25  Istream constructor and IOstream operators for keyType.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "keyType.H"
30 #include "regExp.H"
31 #include "IOstreams.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
36 
37 
38 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
39 
41 :
42  word(),
43  isPattern_(false)
44 {
45  is >> *this;
46 }
47 
48 
49 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
50 
52 (
53  const std::string& str,
54  bool literalMatch
55 ) const
56 {
57  if (literalMatch || !isPattern_)
58  {
59  // check as string
60  return (str == *this);
61  }
62  else
63  {
64  // check as regex
65  return regExp(*this).match(str);
66  }
67 }
68 
69 
70 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
71 
73 {
74  token t(is);
75 
76  if (!t.good())
77  {
78  is.setBad();
79  return is;
80  }
81 
82  if (t.isWord())
83  {
84  kw = t.wordToken();
85  }
86  else if (t.isString())
87  {
88  // Assign from string. Set as regular expression.
89  kw = t.stringToken();
90  kw.isPattern_ = true;
91 
92  // flag empty strings as an error
93  if (kw.empty())
94  {
95  is.setBad();
97  << "empty word/expression "
98  << exit(FatalIOError);
99  return is;
100  }
101  }
102  else
103  {
104  is.setBad();
106  << "wrong token type - expected word or string, found "
107  << t.info()
108  << exit(FatalIOError);
109 
110  return is;
111  }
112 
113  // Check state of IOstream
114  is.check("Istream& operator>>(Istream&, keyType&)");
115 
116  return is;
117 }
118 
119 
121 {
122  os.write(kw);
123  os.check("Ostream& operator<<(Ostream&, const keyType&)");
124  return os;
125 }
126 
127 
128 // ************************************************************************* //
A class for handling keywords in dictionaries.
Definition: keyType.H:64
bool isWord() const
Definition: tokenI.H:230
bool match(const std::string &) const
Return true if it matches the entire string.
Definition: regExp.C:201
Wrapper around POSIX extended regular expressions.
Definition: regExp.H:61
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:380
const word & wordToken() const
Definition: tokenI.H:235
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:69
bool match(const std::string &, bool literalMatch=false) const
Smart match as regular expression or as a string.
Definition: keyType.C:52
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:487
A class for handling words, derived from string.
Definition: word.H:59
Istream & operator>>(Istream &, directionInfo &)
bool good() const
Definition: tokenI.H:197
keyType()
Construct null.
Definition: keyTypeI.H:28
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
const string & stringToken() const
Definition: tokenI.H:258
bool isString() const
Definition: tokenI.H:253
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
Ostream & operator<<(Ostream &, const ensightPart &)
virtual Ostream & write(const token &)=0
Write next token to stream.
static const keyType null
An empty keyType.
Definition: keyType.H:83
IOerror FatalIOError