blockMeshTools.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) 2016 OpenFOAM Foundation
9  Copyright (C) 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 "blockMeshTools.H"
30 
31 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35 
36 static inline const Foam::entry* resolveLabel(const entry& e, const label val)
37 {
38  return
39  (
40  (e.isStream() && e.stream().front().isLabel(val))
41  ? &e : nullptr
42  );
43 }
44 
45 } // End namespace Foam
46 
47 
48 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
49 
51 (
52  Istream& is,
53  label& val,
54  const dictionary& dict
55 )
56 {
57  token t(is);
58  if (t.isLabel())
59  {
60  val = t.labelToken();
61  }
62  else if (t.isWord())
63  {
64  const word& varName = t.wordToken();
65  const entry* eptr =
66  dict.findScoped(varName, keyType::REGEX_RECURSIVE);
67 
68  if (eptr)
69  {
70  // Read as label
71  val = Foam::readLabel(eptr->stream());
72  }
73  else
74  {
76  << "Undefined variable "
77  << varName << ". Valid variables are " << dict
78  << exit(FatalIOError);
79  }
80  }
81  else
82  {
84  << "Illegal token " << t.info()
85  << " when trying to read label"
86  << exit(FatalIOError);
87  }
88 
90 }
91 
92 
94 (
95  Istream& is,
96  const dictionary& dict
97 )
98 {
99  label val(0);
100  read(is, val, dict);
101  return val;
102 }
103 
104 
106 (
107  Ostream& os,
108  const label val,
109  const dictionary& dict
110 )
111 {
112  for (const entry& e : dict)
113  {
114  const entry* eptr = resolveLabel(e, val);
115  if (eptr)
116  {
117  os << eptr->keyword();
118  return;
119  }
120  }
121  os << val;
122 }
123 
124 
126 (
127  const dictionary& dict,
128  const label val
129 )
130 {
131  for (const entry& e : dict)
132  {
133  const entry* eptr = resolveLabel(e, val);
134  if (eptr)
135  {
136  return eptr;
137  }
138  }
139 
140  return nullptr;
141 }
142 
143 
144 // ************************************************************************* //
dictionary dict
bool isWord() const noexcept
Token is word-variant (WORD, DIRECTIVE)
Definition: tokenI.H:729
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.H:747
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
label readLabel(const char *buf)
Parse entire buffer as a label, skipping leading/trailing whitespace.
Definition: label.H:63
const entry * findEntry(const dictionary &dict, const label val)
Linear search for labelled entry, nullptr if not found.
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
const keyType & keyword() const noexcept
Return keyword.
Definition: entry.H:231
void write(Ostream &, const label val, const dictionary &)
Write with dictionary lookup.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:51
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:637
label labelToken() const
Return label value.
Definition: tokenI.H:633
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:599
InfoProxy< token > info() const noexcept
Return info proxy, for printing token information to a stream.
Definition: token.H:1106
static const Foam::entry * resolveLabel(const entry &e, const label val)
Namespace for OpenFOAM.
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:63
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...