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-2019 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 "keyType.H"
27 #include "regExp.H"
28 #include "IOstreams.H"
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
33 
34 
35 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
36 
38 :
39  variable(),
40  type_(UNDEFINED)
41 {
42  operator=(t);
43 }
44 
45 
47 :
48  variable(),
49  type_(UNDEFINED)
50 {
51  is >> *this;
52 }
53 
54 
55 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
56 
58 (
59  const std::string& str,
60  bool literalMatch
61 ) const
62 {
63  if (literalMatch || !isPattern())
64  {
65  // Check as string
66  return (str == *this);
67  }
68  else
69  {
70  // Check as regex
71  return regExp(*this).match(str);
72  }
73 }
74 
75 
76 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
77 
79 {
80  if (t.isWord())
81  {
82  operator=(t.wordToken());
83  }
84  else if (t.isFunctionName())
85  {
87  }
88  else if (t.isVariable())
89  {
91  }
92  else if (t.isString())
93  {
94  // Assign from string. Set as pattern.
96 
97  // An empty pattern string is a fatal error
98  if (empty())
99  {
101  << "Empty pattern string"
102  << exit(FatalIOError);
103  }
104  }
105  else
106  {
107  variable::clear();
108  type_ = UNDEFINED;
109  }
110 }
111 
112 
113 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
114 
116 {
117  token t(is);
118 
119  if (!t.good())
120  {
121  is.setBad();
122  return is;
123  }
124 
125  kw = t;
126 
127  if (kw.isUndefined())
128  {
129  is.setBad();
131  << "wrong token type - expected word or string, found "
132  << t.info()
133  << exit(FatalIOError);
134 
135  return is;
136  }
137 
138  // Check state of IOstream
139  is.check("Istream& operator>>(Istream&, keyType&)");
140 
141  return is;
142 }
143 
144 
146 {
147  os.writeQuoted(kw, kw.isPattern());
148  os.check("Ostream& operator<<(Ostream&, const keyType&)");
149  return os;
150 }
151 
152 
153 // * * * * * * * * * * * * * * * * Global Function * * * * * * * * * * * * * //
154 
156 {
157  //- Indentation of the entry from the start of the keyword
158  static const unsigned short entryIndentation_ = 16;
159 
160  os.indent();
161  os << kw;
162 
163  label nSpaces = entryIndentation_ - label(kw.size());
164 
165  // Pattern is surrounded by quotes
166  if (kw.isPattern())
167  {
168  nSpaces -= 2;
169  }
170 
171  nSpaces = max(nSpaces, 1);
172 
173  while (nSpaces--)
174  {
175  os.write(char(token::SPACE));
176  }
177 
178  return os;
179 }
180 
181 
182 // ************************************************************************* //
const variable & variableToken() const
Definition: tokenI.H:302
A class for handling keywords in dictionaries.
Definition: keyType.H:66
const functionName & functionNameToken() const
Definition: tokenI.H:284
bool isWord() const
Definition: tokenI.H:261
layerAndWeight max(const layerAndWeight &a, const layerAndWeight &b)
virtual Ostream & write(const char)=0
Write character.
tUEqn clear()
FvWallInfoData< WallInfo, label > label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
bool match(const std::string &) const
Return true if it matches the entire string.
Definition: regExp.C:201
Ostream & writeKeyword(Foam::Ostream &os, const keyType &kw)
Write the keyword to the Ostream with the current level of indentation.
Definition: keyType.C:155
Wrapper around POSIX extended regular expressions.
Definition: regExp.H:61
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
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 match(const std::string &, bool literalMatch=false) const
Smart match as regular expression or as a string.
Definition: keyType.C:58
virtual Ostream & writeQuoted(const std::string &, const bool quoted=true)=0
Write std::string surrounded by quotes.
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
Istream & operator>>(Istream &, directionInfo &)
bool isUndefined() const
Return true if the type has not been defined.
Definition: keyTypeI.H:79
bool good() const
Definition: tokenI.H:228
keyType()
Construct null.
Definition: keyTypeI.H:28
bool isVariable() const
Definition: tokenI.H:297
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:318
void operator=(const keyType &)
Assignment operator.
Definition: keyTypeI.H:105
Ostream & operator<<(Ostream &, const ensightPart &)
bool isFunctionName() const
Definition: tokenI.H:279
static const keyType null
An empty keyType.
Definition: keyType.H:92
A variable is a word with support for additional characters, in particular &#39;$&#39; and &#39;/&#39;...
Definition: variable.H:58
virtual void indent()=0
Add indentation characters.
bool isPattern() const
Should be treated as a match rather than a literal string.
Definition: keyTypeI.H:97
IOerror FatalIOError