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  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
84  << exit(FatalIOError);
85  }
86  }
87  else
88  {
90  << "Illegal token " << t.info()
91  << " when trying to read label"
92  << exit(FatalIOError);
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 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
Foam::blockMeshTools::write
void write(Ostream &, const label val, const dictionary &)
Write with dictionary lookup.
Definition: blockMeshTools.C:112
Foam::token::labelToken
label labelToken() const
Return label value.
Definition: tokenI.H:513
Foam::token::isLabel
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:497
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::entry::stream
virtual ITstream & stream() const =0
Return token stream, if entry is a primitive entry.
Foam::IOstream::fatalCheck
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:64
Foam::entry::keyword
const keyType & keyword() const noexcept
Return keyword.
Definition: entry.H:195
Foam::FatalIOError
IOerror FatalIOError
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::token::isWord
bool isWord() const noexcept
Token is word-variant (WORD, DIRECTIVE)
Definition: tokenI.H:609
Foam::blockMeshTools::read
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:57
Foam::resolveLabel
static const Foam::entry * resolveLabel(const entry &e, const label val)
Definition: blockMeshTools.C:36
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
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
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
blockMeshTools.H
Foam::blockMeshTools::findEntry
const entry * findEntry(const dictionary &dict, const label val)
Linear search for labelled entry, nullptr if not found.
Definition: blockMeshTools.C:132
Foam::readLabel
label readLabel(const char *buf)
Parse entire buffer as a label, skipping leading/trailing whitespace.
Definition: label.H:66
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:295
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