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-------------------------------------------------------------------------------
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 "blockMeshTools.H"
30
31// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
32
33namespace Foam
34{
35
36static inline const Foam::entry* resolveLabel(const entry& e, const label val)
37{
38 if (e.isStream())
39 {
40 const tokenList& toks = e.stream();
41
42 if (!toks.empty() && toks[0].isLabel(val))
43 {
44 return &e;
45 }
46 }
47
48 return nullptr;
49}
50
51} // End namespace Foam
52
53
54// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
55
57(
58 Istream& is,
59 label& val,
60 const dictionary& dict
61)
62{
63 token t(is);
64 if (t.isLabel())
65 {
66 val = t.labelToken();
67 }
68 else if (t.isWord())
69 {
70 const word& varName = t.wordToken();
71 const entry* eptr =
72 dict.findScoped(varName, keyType::REGEX_RECURSIVE);
73
74 if (eptr)
75 {
76 // Read as label
77 val = Foam::readLabel(eptr->stream());
78 }
79 else
80 {
82 << "Undefined variable "
83 << varName << ". Valid variables are " << dict
85 }
86 }
87 else
88 {
90 << "Illegal token " << t.info()
91 << " when trying to read label"
93 }
94
96}
97
98
100(
101 Istream& is,
102 const dictionary& dict
103)
104{
105 label val(0);
106 read(is, val, dict);
107 return val;
108}
109
110
112(
113 Ostream& os,
114 const label val,
115 const dictionary& dict
116)
117{
118 for (const entry& e : dict)
119 {
120 const entry* eptr = resolveLabel(e, val);
121 if (eptr)
122 {
123 os << eptr->keyword();
124 return;
125 }
126 }
127 os << val;
128}
129
130
132(
133 const dictionary& dict,
134 const label val
135)
136{
137 for (const entry& e : dict)
138 {
139 const entry* eptr = resolveLabel(e, val);
140 if (eptr)
141 {
142 return eptr;
143 }
144 }
145
146 return nullptr;
147}
148
149
150// ************************************************************************* //
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:64
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
bool empty() const noexcept
True if the UList is empty (ie, size() is zero)
Definition: UListI.H:427
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:70
virtual ITstream & stream() const =0
Return token stream, if entry is a primitive entry.
const keyType & keyword() const noexcept
Return keyword.
Definition: entry.H:195
A token holds an item read from Istream.
Definition: token.H:69
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:497
label labelToken() const
Return label value.
Definition: tokenI.H:513
InfoProxy< token > info() const
Return info proxy for printing token information to a stream.
Definition: token.H:586
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.H:631
bool isWord() const noexcept
Token is word-variant (WORD, DIRECTIVE)
Definition: tokenI.H:609
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
void write(Ostream &, const label val, const dictionary &)
Write with dictionary lookup.
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
const entry * findEntry(const dictionary &dict, const label val)
Linear search for labelled entry, nullptr if not found.
Namespace for OpenFOAM.
label readLabel(const char *buf)
Parse entire buffer as a label, skipping leading/trailing whitespace.
Definition: label.H:66
static const Foam::entry * resolveLabel(const entry &e, const label val)
IOerror FatalIOError
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
dictionary dict
volScalarField & e
Definition: createFields.H:11