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-2022 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 
41 Foam::wordRe::wordRe(const keyType& str)
42 :
43  word(str, false), // No stripping
44  regexPtr_(nullptr)
45 {
46  if (str.isPattern())
47  {
48  compile();
49  }
50 }
51 
52 
53 Foam::wordRe::wordRe(Istream& is)
54 :
55  word(),
56  regexPtr_(nullptr)
57 {
58  is >> *this;
59 }
60 
61 
62 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
63 
64 bool Foam::wordRe::assign(const token& tok)
65 {
66  if (tok.isWord())
67  {
68  // Assign from word - literal
69  assign(tok.wordToken());
70  uncompile();
71  return true;
72  }
73  else if (tok.isQuotedString())
74  {
75  // Assign from quoted string - auto-detect regex
76  assign(tok.stringToken());
77  compile(wordRe::DETECT);
78  return true;
79  }
80 
81  return false;
82 }
83 
84 
85 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
86 
87 void Foam::wordRe::operator=(const keyType& str)
88 {
89  assign(str);
90  if (str.isPattern())
91  {
92  compile();
93  }
94  else
95  {
96  uncompile();
97  }
98 }
99 
100 
101 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
102 
103 Foam::Istream& Foam::operator>>(Istream& is, wordRe& val)
104 {
105  token tok(is);
106 
107  if (val.assign(tok))
108  {
109  if (val.empty())
110  {
111  // Empty strings are an error
113  << "Zero-length regex"
114  << exit(FatalIOError);
115  is.setBad();
116  return is;
117  }
118  }
119  else
120  {
122  if (tok.good())
123  {
125  << "Wrong token type - expected word or string, found "
126  << tok.info();
127  }
128  else
129  {
131  << "Bad token - could not get wordRe";
132  }
134  is.setBad();
135  return is;
136  }
137 
138  is.check(FUNCTION_NAME);
139  return is;
140 }
141 
142 
143 Foam::Ostream& Foam::operator<<(Ostream& os, const wordRe& val)
144 {
145  os.writeQuoted(val, val.isPattern());
147  return os;
148 }
149 
150 
151 // ************************************************************************* //
bool good() const noexcept
True if token is not UNDEFINED or ERROR.
Definition: tokenI.H:507
A class for handling keywords in dictionaries.
Definition: keyType.H:66
bool isWord() const noexcept
Token is word-variant (WORD, DIRECTIVE)
Definition: tokenI.H:711
bool assign(const token &tok)
Assign from word or string token.
Definition: wordRe.C:57
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:45
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.H:729
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A token holds an item read from Istream.
Definition: token.H:65
bool compile()
Compile as regular expression (if possible)
Definition: wordReI.H:152
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
void operator=(const wordRe &str)
Copy assignment, retaining type (literal or regex)
Definition: wordReI.H:239
A class for handling words, derived from Foam::string.
Definition: word.H:63
Istream & operator>>(Istream &, directionInfo &)
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
wordRe()
Default construct, empty literal.
Definition: wordReI.H:33
bool isPattern() const noexcept
The keyType is treated as a pattern, not as literal string.
Definition: keyTypeI.H:97
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings...
Definition: wordRe.H:78
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
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
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
const string & stringToken() const
Return const reference to the string contents.
Definition: tokenI.H:783
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:627
InfoProxy< token > info() const noexcept
Return info proxy, for printing token information to a stream.
Definition: token.H:1078
static const wordRe null
An empty wordRe.
Definition: wordRe.H:97
Detect if the string contains meta-characters.
Definition: wordRe.H:113
bool isQuotedString() const noexcept
Token is (quoted) STRING (string variant)
Definition: tokenI.H:741
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...