keyType.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2015 OpenFOAM Foundation
9  Copyright (C) 2018-2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
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  type_(option::LITERAL)
44 {
45  is >> *this;
46 }
47 
48 
49 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
50 
51 bool Foam::keyType::match(const std::string& text, bool literal) const
52 {
53  if (!literal && isPattern())
54  {
55  return regExp(*this).match(text); // Match as regex
56  }
57 
58  return !compare(text); // Compare as literal
59 }
60 
61 
62 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
63 
65 {
66  token t(is);
67 
68  if (!t.good())
69  {
71  << "Bad token - could not get a word/regex"
72  << exit(FatalIOError);
73  is.setBad();
74  return is;
75  }
76 
77  if (t.isWord())
78  {
79  val = t.wordToken();
81  }
82  else if (t.isString())
83  {
84  // Assign from string, treat as regular expression
85  val = t.stringToken();
87 
88  // Flag empty strings as an error
89  if (val.empty())
90  {
92  << "Empty word/expression"
93  << exit(FatalIOError);
94  is.setBad();
95  return is;
96  }
97  }
98  else
99  {
101  << "Wrong token type - expected word or string, found "
102  << t.info()
103  << exit(FatalIOError);
104  is.setBad();
105  return is;
106  }
107 
108  is.check(FUNCTION_NAME);
109  return is;
110 }
111 
112 
114 {
115  os.writeQuoted(val, val.isPattern());
116  os.check(FUNCTION_NAME);
117  return os;
118 }
119 
120 
121 // ************************************************************************* //
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::keyType::keyType
keyType()
Construct null.
Definition: keyTypeI.H:48
Foam::keyType::isPattern
bool isPattern() const
The keyType is treated as a pattern, not as literal string.
Definition: keyTypeI.H:128
Foam::token::stringToken
const string & stringToken() const
Return const reference to the string contents.
Definition: tokenI.H:644
Foam::Ostream::writeQuoted
virtual Ostream & writeQuoted(const std::string &str, const bool quoted=true)=0
Write std::string surrounded by quotes.
Foam::FatalIOError
IOerror FatalIOError
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::token::isWord
bool isWord() const noexcept
Token is WORD or DIRECTIVE word.
Definition: tokenI.H:583
Foam::regExp
regExpCxx regExp
Selection of preferred regular expression implementation.
Definition: regExpFwd.H:41
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::keyType
A class for handling keywords in dictionaries.
Definition: keyType.H:60
Foam::token::info
InfoProxy< token > info() const
Return info proxy for printing token information to a stream.
Definition: token.H:551
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::regExpCxx::match
bool match(const std::string &text) const
True if the regex matches the entire text.
Definition: regExpCxxI.H:185
Foam::keyType::null
static const keyType null
An empty keyType.
Definition: keyType.H:103
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:51
Foam::token::wordToken
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.H:599
Foam::keyType::setType
void setType(option opt, bool adjust=false)
Change the representation.
Definition: keyTypeI.H:134
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::token::good
bool good() const noexcept
True if token is not UNDEFINED or ERROR.
Definition: tokenI.H:399
Foam::keyType::REGEX
Regular expression.
Definition: keyType.H:74
Foam::token::isString
bool isString() const noexcept
Token is STRING, VARIABLE or VERBATIM string.
Definition: tokenI.H:615
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:270
Foam::IOstream::setBad
void setBad()
Set stream to be bad.
Definition: IOstream.H:360
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401
Foam::keyType::LITERAL
String literal.
Definition: keyType.H:73
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
keyType.H
Foam::keyType::option
option
Enumeration for the data type and search/match modes (bitmask)
Definition: keyType.H:70
Foam::keyType::match
bool match(const std::string &text, bool literal=false) const
Smart match as regular expression or as a string.
Definition: keyType.C:51