IOstreamOption.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) 2018-2024 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 "IOstreamOption.H"
29 #include "debug.H"
30 #include "dictionary.H"
31 #include "Enum.H"
32 #include "Switch.H"
33 
34 // * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * * //
35 
37 
38 const Foam::Enum
39 <
41 >
43 ({
44  { floatFormat::general, "general" },
45  { floatFormat::fixed, "fixed" },
46  { floatFormat::scientific, "scientific" },
47 });
48 
49 const Foam::Enum
50 <
52 >
54 ({
55  { streamFormat::ASCII, "ascii" },
56  { streamFormat::BINARY, "binary" },
57 });
58 
59 
60 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
61 
64 (
65  const word& fmtName,
66  const floatFormat deflt
67 )
68 {
69  // Handle bad input graciously. A no-op for an empty string
70 
71  if (!fmtName.empty())
72  {
73  auto iter = floatFormatNames.cfind(fmtName);
74 
75  if (iter.good())
76  {
77  return iter.val();
78  }
79 
80  // Fall-through to warning
81 
82  auto& err = WarningInFunction
83  << "Unknown float format '" << fmtName << "' using ";
84 
85  iter = floatFormatNames.cfind(deflt);
86  if (iter.good())
87  {
88  err << '\'' << iter.key() << '\'';
89  }
90  else
91  {
92  err << "value=" << int(deflt);
93  }
94  err << " from " << floatFormatNames << nl;
95  }
96 
97  return deflt;
98 }
99 
100 
103 (
104  const word& key,
105  const dictionary& dict,
106  const floatFormat deflt
107 )
108 {
109  return floatFormatNames.getOrDefault(key, dict, deflt, true); // warnOnly
110 }
111 
112 
115 (
116  const word& fmtName,
117  const streamFormat deflt
118 )
119 {
120  // Handle bad input graciously. A no-op for an empty string
121 
122  if (!fmtName.empty())
123  {
124  auto iter = formatNames.cfind(fmtName);
125 
126  if (iter.good())
127  {
128  return iter.val();
129  }
130 
131  // Fall-through to warning
132 
133  auto& err = WarningInFunction
134  << "Unknown stream format '" << fmtName << "' using ";
135 
136  iter = formatNames.cfind(deflt);
137  if (iter.good())
138  {
139  err << '\'' << iter.key() << '\'';
140  }
141  else
142  {
143  err << "value=" << int(deflt);
144  }
145  err << " from " << formatNames << nl;
146  }
148  return deflt;
149 }
150 
151 
154 (
155  const word& key,
156  const dictionary& dict,
157  const streamFormat deflt
158 )
159 {
160  return formatNames.getOrDefault(key, dict, deflt, true); // warnOnly
161 }
162 
163 
166 (
167  const word& compName,
168  const compressionType deflt
169 )
170 {
171  // Handle bad input graciously. A no-op for an empty string
172 
173  if (!compName.empty())
174  {
175  const Switch sw = Switch::find(compName);
176 
177  if (sw.good())
178  {
179  return
180  (
181  sw
182  ? compressionType::COMPRESSED
183  : compressionType::UNCOMPRESSED
184  );
185  }
186 
187  // Fall-through to warning
188 
190  << "Unknown compression specifier '" << compName
191  << "' using compression " << (deflt ? "on" : "off") << nl;
192  }
194  return deflt;
195 }
196 
197 
200 (
201  const word& key,
202  const dictionary& dict,
203  const compressionType deflt
204 )
205 {
206  return
207  (
208  Switch(key, dict, Switch(bool(deflt)), true) // warnOnly
209  ? compressionType::COMPRESSED
210  : compressionType::UNCOMPRESSED
211  );
212 }
213 
214 
215 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
218 :
219  versionNumber(readFloat(verNum))
220 {}
221 
222 
224 :
225  versionNumber()
226 {
227  if (tok.isStringType())
228  {
229  (*this) = versionNumber(tok.stringToken());
230  }
231  else if (tok.isNumber())
232  {
233  // Accept integer or floating-point
234  // Eg, '2.0' becomes '2' after foamDictionary -expand
235  (*this) = versionNumber(float(tok.number()));
236  }
237  else
238  {
240  << "Wrong token for version - expected word/number, found "
241  << tok.info() << nl;
242  }
243 }
244 
245 
247 (
248  const word& key,
249  const dictionary& dict
250 )
251 :
252  versionNumber()
253 {
254  token tok;
255 
256  if (dict.readIfPresent<token>(key, tok, keyType::LITERAL))
257  {
258  (*this) = versionNumber(tok);
259  }
260 }
261 
262 
263 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
264 
265 Foam::Ostream& Foam::operator<<
266 (
267  Ostream& os,
269 )
270 {
272  return os;
273 }
274 
275 
276 Foam::Ostream& Foam::operator<<
277 (
278  Ostream& os,
279  const IOstreamOption::versionNumber& ver
280 )
281 {
282  // Emit unquoted char sequence (eg, word)
283  // for correct behaviour when sending in parallel
284 
285  os.writeQuoted(ver.str(), false);
286  return os;
287 }
288 
289 
290 // ************************************************************************* //
dictionary dict
bool good() const noexcept
True if the Switch represents a valid enumeration.
Definition: Switch.C:307
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
scalar number() const
Return label, float or double value.
Definition: tokenI.H:713
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
static const Enum< streamFormat > formatNames
Stream format names (ascii, binary)
floatFormat
Float formats (eg, time directory name formats)
A token holds an item read from Istream.
Definition: token.H:65
const_iterator cfind(const word &key) const
Find key/value pair by enumeration name.
Definition: EnumI.H:236
A simple wrapper around bool so that it can be read as a word: true/false, on/off, yes/no, any/none. Also accepts 0/1 as a string and shortcuts t/f, y/n.
Definition: Switch.H:77
IOstream & fixed(IOstream &io)
Definition: IOstream.H:572
bool isStringType() const noexcept
Token is word-variant or string-variant (WORD, DIRECTIVE, STRING, EXPRESSION, VARIABLE, VERBATIM, CHAR_DATA)
Definition: tokenI.H:795
A class for handling words, derived from Foam::string.
Definition: word.H:63
static const versionNumber currentVersion
The current version number (2.0)
String literal.
Definition: keyType.H:82
Representation of a major/minor version number.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
OBJstream os(runTime.globalPath()/outputName)
const string & stringToken() const
Return const reference to the string contents.
Definition: tokenI.H:801
#define WarningInFunction
Report a warning using Foam::Warning.
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: error.H:64
static floatFormat floatFormatEnum(const word &fmtName, const floatFormat deflt=floatFormat::general)
Lookup floatFormat enum corresponding to the string (general | fixed | scientific).
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
static const Enum< floatFormat > floatFormatNames
Names for float formats (general, fixed, scientific)
constexpr versionNumber() noexcept
Default construct current version. The value (2.0) corresponds to the current version.
static streamFormat formatEnum(const word &fmtName, const streamFormat deflt=streamFormat::ASCII)
Lookup streamFormat enum corresponding to the string (ascii | binary).
streamFormat
Data format (ascii | binary)
InfoProxy< token > info() const noexcept
Return info proxy, for printing token information to a stream.
Definition: token.H:1106
static compressionType compressionEnum(const word &compName, const compressionType deflt=compressionType::UNCOMPRESSED)
The compression enum corresponding to the string.
bool isNumber() const noexcept
Token is LABEL, FLOAT or DOUBLE.
Definition: tokenI.H:707
static Switch find(const std::string &str)
Find switchType for the given string, returning as a Switch that can be tested for good() or bad()...
Definition: Switch.C:147
IOstream & scientific(IOstream &io)
Definition: IOstream.H:578