schemesLookupDetail.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) 2021 OpenCFD Ltd.
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 "schemesLookup.H"
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
32 Foam::schemesLookup::lookupDetail::lookupDetail
33 (
34  const word& dictName,
35  const fileName& parentDictPath
36 )
37 :
38  name_(dictName),
39  dict_(),
40  default_()
41 {
42  if (parentDictPath.empty())
43  {
44  dict_.name() = name_;
45  }
46  else if (name_.empty())
47  {
48  dict_.name() = parentDictPath;
49  name_ = dict_.dictName();
50  }
51  else
52  {
53  dict_.name() = parentDictPath + "." + name_;
54  }
55  default_.name() = dict_.name() + ".default";
56 }
57 
58 
59 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
60 
62 {
63  dict_.clear();
64  default_.clear();
65 }
66 
67 
68 Foam::ITstream& Foam::schemesLookup::lookupDetail::fallback() const
69 {
70  ITstream& is = const_cast<ITstream&>(default_);
71  is.rewind();
72  return is;
73 }
74 
75 
76 Foam::ITstream& Foam::schemesLookup::lookupDetail::lookup
77 (
78  const word& name
79 ) const
80 {
81  if (dict_.found(name) || default_.empty())
82  {
83  // Fails when 'name' is not found and no default is available,
84  // which provides some traceback error messages
85  return dict_.lookup(name);
86  }
87 
88  return fallback();
89 }
90 
91 
92 void Foam::schemesLookup::lookupDetail::populate
93 (
94  const dictionary& dict,
95  const word& defaultName,
96  const bool mandatory
97 )
98 {
99  if (mandatory || dict.found(name_))
100  {
101  // Fails when 'name' is not found but it is mandatory,
102  // which provides some traceback error messages
103  dict_ = dict.subDict(name_);
104  }
105  else if (!defaultName.empty() && !dict_.found("default"))
106  {
107  dict_.add("default", defaultName);
108  }
109 
110  // Clear or set the default stream
111  if
112  (
113  !dict_.found("default")
114  || dict_.lookup("default").peek() == "none"
115  )
116  {
117  default_.clear();
118  default_.rewind();
119  }
120  else
121  {
122  default_ = dict_.lookup("default");
123  }
124 }
125 
126 
127 void Foam::schemesLookup::lookupDetail::writeEntry(Ostream& os) const
128 {
129  dict_.writeEntry(os);
130 }
131 
132 
133 void Foam::schemesLookup::lookupDetail::writeEntryOptional(Ostream& os) const
134 {
135  if (!dict_.empty())
136  {
137  dict_.writeEntry(os);
138  }
139 }
140 
141 
142 // ************************************************************************* //
Foam::ITstream::rewind
virtual void rewind()
Rewind the stream so that it may be read again.
Definition: ITstream.C:561
Foam::dictionary::found
bool found(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Search for an entry (const access) with the given keyword.
Definition: dictionaryI.H:87
dictName
const word dictName("faMeshDefinition")
schemesLookup.H
Foam::ITstream
An input stream of tokens.
Definition: ITstream.H:52
Foam::dictionary::subDict
const dictionary & subDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a sub-dictionary.
Definition: dictionary.C:460
dict
dictionary dict
Definition: searchingEngine.H:14
os
OBJstream os(runTime.globalPath()/outputName)
clear
patchWriters clear()
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::dictionary::add
entry * add(entry *entryPtr, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:640