Loading...
Searching...
No Matches
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-2021 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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 "token.H"
32#include "IOstreams.H"
33#include <utility> // For std::swap
34
35// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36
38
39
40// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
41
44 is >> *this;
45}
46
47
48// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
49
51{
52 if (this == &rhs)
53 {
54 return; // Self-swap is a no-op
55 }
56
57 word::swap(static_cast<word&>(rhs));
58 std::swap(type_, rhs.type_);
59}
60
61
62bool Foam::keyType::match(const std::string& text, bool literal) const
63{
64 if (!literal && isPattern())
65 {
66 return regExp(*this).match(text); // Match as regex
67 }
68
69 return !compare(text); // Compare as literal
70}
71
72
73bool Foam::keyType::assign(const token& tok)
74{
75 if (tok.isWord())
76 {
77 // Assign from word - literal
78 assign(tok.wordToken());
79 uncompile();
80 return true;
81 }
82 else if (tok.isQuotedString())
83 {
84 // Assign from quoted string - regular expression
85 assign(tok.stringToken());
86 compile();
87 return true;
88 }
90 return false;
91}
92
93
94// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
95
96Foam::Istream& Foam::operator>>(Istream& is, keyType& val)
97{
98 token tok(is);
99
100 if (val.assign(tok))
101 {
102 if (val.empty())
103 {
104 // Empty strings are an error
106 << "Zero-length regex"
107 << exit(FatalIOError);
108 is.setBad();
109 return is;
110 }
111 }
112 else
113 {
115 if (tok.good())
116 {
118 << "Wrong token type - expected word or string, found "
119 << tok.info();
120 }
121 else
122 {
124 << "Bad token - could not get keyType";
125 }
127 is.setBad();
128 return is;
130
131 is.check(FUNCTION_NAME);
132 return is;
133}
134
135
136Foam::Ostream& Foam::operator<<(Ostream& os, const keyType& val)
137{
138 os.writeQuoted(val, val.isPattern());
140 return os;
141}
142
143
144// ************************************************************************* //
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
void setBad() noexcept
Set stream state to be 'bad'.
Definition IOstream.H:488
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition IOstream.C:45
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
virtual Ostream & writeQuoted(const char *str, std::streamsize len, const bool quoted=true) override
Write character/string content, with/without surrounding quotes.
Definition OBJstream.C:78
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A class for handling keywords in dictionaries.
Definition keyType.H:69
bool assign(const token &tok)
Assign from word or string token.
Definition keyType.C:66
static const keyType null
An empty keyType.
Definition keyType.H:111
bool compile() noexcept
Mark as regular expression.
Definition keyTypeI.H:120
void uncompile() noexcept
Mark as literal string.
Definition keyTypeI.H:127
keyType()
Default construct, empty literal.
Definition keyTypeI.H:33
void swap(keyType &rhs)
Swap contents. Self-swapping is a no-op.
Definition keyType.C:43
bool isPattern() const noexcept
The keyType is treated as a pattern, not as literal string.
Definition keyTypeI.H:97
bool match(const std::string &text, bool literal=false) const
Smart match as regular expression or as a string.
Definition keyType.C:55
bool match(const std::string &text) const
True if the regex matches the entire text.
Definition regExpCxxI.H:282
void swap(std::string &str)
Swap contents. Self-swapping is a no-op.
Definition stringI.H:223
A token holds an item read from Istream.
Definition token.H:70
const string & stringToken() const
Return const reference to the string contents.
Definition tokenI.H:1076
bool good() const noexcept
True if token is not UNDEFINED or ERROR.
Definition tokenI.H:584
bool isQuotedString() const noexcept
Token is (quoted) STRING (string variant).
Definition tokenI.H:1034
const word & wordToken() const
Return const reference to the word contents.
Definition tokenI.H:1022
bool isWord() const noexcept
Token is word-variant (WORD, DIRECTIVE).
Definition tokenI.H:1004
InfoProxy< token > info() const noexcept
Return info proxy, for printing token information to a stream.
Definition token.H:1253
A class for handling words, derived from Foam::string.
Definition word.H:66
word()=default
Default construct.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition error.H:629
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
void assign(Field< Tout > &result, const Field< T1 > &a, const UnaryOp &op)
Populate a field as the result of a unary operation on an input.
Definition FieldOps.C:28
regExpCxx regExp
Selection of preferred regular expression implementation.
Definition regExpFwd.H:37
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
Istream & operator>>(Istream &, directionInfo &)
IOerror FatalIOError
Error stream (stdout output on all processes), with additional 'FOAM FATAL IO ERROR' header text and ...
void rhs(fvMatrix< typename Expr::value_type > &m, const Expr &expression)
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125