wordRe.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-2016 OpenFOAM Foundation
9  Copyright (C) 2018-2021 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 "wordRe.H"
30 #include "keyType.H"
31 #include "token.H"
32 #include "IOstreams.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
37 
38 
39 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
40 
42 :
43  word(str, false) // No stripping
44 {
45  if (str.isPattern())
46  {
47  compile();
48  }
49 }
50 
51 
53 {
54  is >> *this;
55 }
56 
57 
58 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
59 
60 bool Foam::wordRe::assign(const token& tok)
61 {
62  if (tok.isWord())
63  {
64  // Assign from word - literal
65  assign(tok.wordToken());
66  uncompile();
67  return true;
68  }
69  else if (tok.isQuotedString())
70  {
71  // Assign from quoted string - auto-detect regex
72  assign(tok.stringToken());
73  compile(wordRe::DETECT);
74  return true;
75  }
76 
77  return false;
78 }
79 
80 
81 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
82 
84 {
85  assign(str);
86  if (str.isPattern())
87  {
88  compile();
89  }
90  else
91  {
92  uncompile();
93  }
94 }
95 
96 
97 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
98 
100 {
101  token tok(is);
102 
103  if (val.assign(tok))
104  {
105  if (val.empty())
106  {
107  // Empty strings are an error
109  << "Zero-length regex"
110  << exit(FatalIOError);
111  is.setBad();
112  return is;
113  }
114  }
115  else
116  {
118  if (tok.good())
119  {
121  << "Wrong token type - expected word or string, found "
122  << tok.info();
123  }
124  else
125  {
127  << "Bad token - could not get wordRe";
128  }
130  is.setBad();
131  return is;
132  }
133 
134  is.check(FUNCTION_NAME);
135  return is;
136 }
137 
138 
140 {
141  os.writeQuoted(val, val.isPattern());
143  return os;
144 }
145 
146 
147 // ************************************************************************* //
token.H
Foam::wordRe::isPattern
bool isPattern() const noexcept
The wordRe is treated as a pattern, not as literal string.
Definition: wordReI.H:107
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:65
Foam::keyType::isPattern
bool isPattern() const noexcept
The keyType is treated as a pattern, not as literal string.
Definition: keyTypeI.H:104
Foam::wordRe::operator=
void operator=(const wordRe &str)
Copy assignment, retaining type (literal or regex)
Definition: wordReI.H:219
Foam::token::stringToken
const string & stringToken() const
Return const reference to the string contents.
Definition: tokenI.H:689
Foam::OBJstream::writeQuoted
virtual Ostream & writeQuoted(const std::string &str, const bool quoted=true)
Write std::string surrounded by quotes.
Definition: OBJstream.C:108
Foam::wordRe::DETECT
Detect if the string contains meta-characters.
Definition: wordRe.H:108
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::isQuotedString
bool isQuotedString() const noexcept
Token is (quoted) STRING (string variant)
Definition: tokenI.H:647
Foam::wordRe
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:80
Foam::token::isWord
bool isWord() const noexcept
Token is word-variant (WORD, DIRECTIVE)
Definition: tokenI.H:609
Foam::wordRe::compile
bool compile()
Compile as regular expression.
Definition: wordReI.H:144
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:68
Foam::wordRe::wordRe
wordRe()
Default construct, empty literal.
Definition: wordReI.H:40
Foam::token::info
InfoProxy< token > info() const
Return info proxy for printing token information to a stream.
Definition: token.H:586
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
wordRe.H
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:58
os
OBJstream os(runTime.globalPath()/outputName)
Foam::token::wordToken
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.H:631
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:405
Foam::wordRe::assign
bool assign(const token &tok)
Assign from word or string token.
Definition: wordRe.C:60
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:295
Foam::IOstream::setBad
void setBad()
Set stream state to be 'bad'.
Definition: IOstream.H:369
Foam::FieldOps::assign
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:35
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
keyType.H
Foam::wordRe::null
static const wordRe null
An empty wordRe.
Definition: wordRe.H:95