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 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "blockMeshTools.H"
29 
30 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
31 
33 (
34  Istream& is,
35  label& val,
36  const dictionary& dict
37 )
38 {
39  token t(is);
40  if (t.isLabel())
41  {
42  val = t.labelToken();
43  }
44  else if (t.isWord())
45  {
46  const word& varName = t.wordToken();
47  const entry* eptr =
48  dict.findScoped(varName, keyType::REGEX_RECURSIVE);
49 
50  if (eptr)
51  {
52  // Read as label
53  val = Foam::readLabel(eptr->stream());
54  }
55  else
56  {
58  << "Undefined variable "
59  << varName << ". Valid variables are " << dict
60  << exit(FatalIOError);
61  }
62  }
63  else
64  {
66  << "Illegal token " << t.info()
67  << " when trying to read label"
68  << exit(FatalIOError);
69  }
70 
72 }
73 
74 
76 (
77  Istream& is,
78  const dictionary& dict
79 )
80 {
81  label val(0);
82  read(is, val, dict);
83  return val;
84 }
85 
86 
88 (
89  Ostream& os,
90  const label val,
91  const dictionary& dict
92 )
93 {
94  for (const entry& e : dict)
95  {
96  if (e.isStream())
97  {
98  label keyVal(Foam::readLabel(e.stream()));
99  if (keyVal == val)
100  {
101  os << e.keyword();
102  return;
103  }
104  }
105  }
106  os << val;
107 }
108 
109 
111 (
112  const dictionary& dict,
113  const label val
114 )
115 {
116  for (const entry& e : dict)
117  {
118  if (e.isStream())
119  {
120  label keyVal(Foam::readLabel(e.stream()));
121  if (keyVal == val)
122  {
123  return e.keyword();
124  }
125  }
126  }
127 
128  return keyType::null;
129 }
130 
131 
132 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
Foam::token::labelToken
label labelToken() const
Return label value.
Definition: tokenI.H:487
Foam::token::isLabel
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:481
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
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:57
Foam::FatalIOError
IOerror FatalIOError
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::blockMeshTools::write
void write(Ostream &, const label, const dictionary &)
Write with dictionary lookup.
Definition: blockMeshTools.C:88
Foam::token::isWord
bool isWord() const noexcept
Token is WORD or DIRECTIVE word.
Definition: tokenI.H:583
Foam::keyType
A class for handling keywords in dictionaries.
Definition: keyType.H:60
Foam::token::info
InfoProxy< token > info() const
Return info proxy for printing token information to a stream.
Definition: token.H:551
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::blockMeshTools::read
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:33
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:121
Foam::blockMeshTools::findEntry
const keyType & findEntry(const dictionary &, const label)
Linear search for label entry.
Definition: blockMeshTools.C:111
Foam::token::wordToken
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.H:599
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
blockMeshTools.H
Foam::readLabel
label readLabel(const char *buf)
Parse entire buffer as a label, skipping leading/trailing whitespace.
Definition: label.H:66
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:270
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56