entry.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) 2011-2015 OpenFOAM Foundation
9  Copyright (C) 2017-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 "entry.H"
30 #include "dictionary.H"
31 #include "StringStream.H"
32 #include "JobInfo.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
37 (
38  Foam::debug::infoSwitch("disableFunctionEntries", 0)
39 );
40 
41 
43 
44 
45 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
46 
48 (
49  const IOstream& is,
50  const std::string& msg
51 )
52 {
53  std::cerr
54  << "--> FOAM Warning :\n"
55  << " Reading \"" << is.relativeName()
56  << "\" at line " << is.lineNumber() << '\n'
57  << " " << msg << std::endl;
58 }
59 
60 
62 {
63  globalInputMode = inputMode::MERGE;
64 }
65 
66 
67 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
68 
69 Foam::entry::entry(const keyType& keyword)
70 :
71  IDLList<entry>::link(),
72  keyword_(keyword)
73 {}
74 
75 
77 :
78  IDLList<entry>::link(),
79  keyword_(e.keyword_)
80 {}
81 
82 
84 {
85  return clone(dictionary::null);
86 }
87 
88 
89 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
90 
91 void Foam::entry::raiseBadInput(const ITstream& is) const
92 {
93  const word& keyword = keyword_;
94 
95  // Can use FatalIOError instead of SafeFatalIOError
96  // since predicate checks are not used at the earliest stages
98  (
99  "", // functionName
100  "", // sourceFileName
101  0, // sourceFileLineNumber
102  this->relativeName(), // ioFileName
103  is.lineNumber() // ioStartLineNumber
104  )
105  << "Entry '" << keyword << "' with invalid input" << nl << nl
106  << exit(FatalIOError);
107 }
108 
109 
110 void Foam::entry::checkITstream(const ITstream& is) const
111 {
112  const label remaining = (is.size() ? is.nRemainingTokens() : -100);
113 
114  if (!remaining)
115  {
116  return;
117  }
118 
119  const word& keyword = keyword_;
120 
121  // Similar to SafeFatalIOError
123  {
124  OSstream& err =
126  (
127  "", // functionName
128  "", // sourceFileName
129  0, // sourceFileLineNumber
130  this->relativeName(), // ioFileName
131  is.lineNumber() // ioStartLineNumber
132  );
133 
134  if (remaining > 0)
135  {
136  err
137  << "Entry '" << keyword
138  << "' has " << remaining << " excess tokens in stream"
139  << nl << nl
140  << " ";
141  is.writeList(err, 0); // <- flatOutput
142  }
143  else
144  {
145  err
146  << "Entry '" << keyword
147  << "' had no tokens in stream"
148  << nl << nl;
149  }
150 
151  err << exit(FatalIOError);
152  }
153  else
154  {
155  // Not yet constructed
156 
157  std::cerr
158  << nl
159  << "--> FOAM FATAL IO ERROR:" << nl;
160 
161  if (remaining > 0)
162  {
163  std::cerr
164  << "Entry '" << keyword << "' has "
165  << remaining << " excess tokens in stream" << nl << nl;
166  }
167  else
168  {
169  std::cerr
170  << "Entry '" << keyword
171  << "' had no tokens in stream" << nl << nl;
172  }
173 
174  std::cerr
175  << "file: " << this->relativeName()
176  << " at line " << is.lineNumber() << '.' << nl
177  << std::endl;
178 
180  }
181 }
182 
183 
184 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
185 
186 void Foam::entry::operator=(const entry& e)
187 {
188  if (this == &e)
189  {
190  return; // Self-assignment is a no-op
191  }
192 
193  keyword_ = e.keyword_;
194 }
195 
196 
197 bool Foam::entry::operator==(const entry& e) const
198 {
199  if (this == &e)
200  {
201  return true;
202  }
203  if (keyword_ != e.keyword_)
204  {
205  return false;
206  }
207 
208  // Compare contents (as strings)
209 
210  OStringStream oss1;
211  oss1 << *this;
212 
214  oss2 << e;
215 
216  return oss1.str() == oss2.str();
217 }
218 
219 
220 bool Foam::entry::operator!=(const entry& e) const
221 {
222  return !operator==(e);
223 }
224 
225 
226 // ************************************************************************* //
Template class for intrusive linked lists.
Definition: ILList.H:45
A class for handling keywords in dictionaries.
Definition: keyType.H:66
entry(const keyType &keyword)
Construct from keyword.
Definition: entry.C:62
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
fileName relativeName() const
Return the name of the stream relative to the current case.
Definition: IOstream.C:39
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
Foam::string str() const
Get the string - as Foam::string rather than std::string.
Definition: StringStream.H:96
Input/output from string buffers.
void operator=(const entry &e)
Definition: entry.C:179
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
void checkITstream(const ITstream &is) const
Check after reading if the input token stream has unconsumed tokens remaining or if there were no tok...
Definition: entry.C:103
inputMode
The input mode options.
Definition: entry.H:74
int infoSwitch(const char *name, const int deflt=0)
Lookup info switch or add default value.
Definition: debug.C:228
label nRemainingTokens() const noexcept
Number of tokens remaining.
Definition: ITstream.H:371
static void resetInputMode()
Reset the globalInputMode to merge.
Definition: entry.C:54
bool operator!=(const entry &e) const
Definition: entry.C:213
static void reportReadWarning(const IOstream &, const std::string &)
Report a read warning (on std::cerr)
Definition: entry.C:41
Ostream & writeList(Ostream &os, const label shortLen=0) const
Write List, with line-breaks in ASCII when length exceeds shortLen.
Definition: UListIO.C:76
static int disableFunctionEntries
Enable or disable use of function entries and variable expansions.
Definition: entry.H:139
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
static const dictionary null
An empty dictionary, which is also the parent for all dictionaries.
Definition: dictionary.H:474
label lineNumber() const noexcept
Const access to the current stream line number.
Definition: IOstream.H:390
An IOstream is an abstract base class for all input/output systems; be they streams, files, token lists etc.
Definition: IOstream.H:82
virtual autoPtr< entry > clone() const
Construct on freestore as copy.
Definition: entry.C:76
static inputMode globalInputMode
The current global input-mode.
Definition: entry.H:144
bool operator==(const entry &e) const
Definition: entry.C:190
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
Output to string buffer, using a OSstream. Always UNCOMPRESSED.
Definition: StringStream.H:256
An input stream of tokens.
Definition: ITstream.H:52
static bool constructed
Global value for constructed job info.
Definition: JobInfo.H:115
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 ...