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-2023 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  const auto iter = floatFormatNames.cfind(fmtName);
74 
75  if (iter.good())
76  {
77  return iter.val();
78  }
79 
80  // Fall-through to warning
81 
83  << "Unknown float format specifier '" << fmtName
84  << "' using '" << floatFormatNames[deflt]
85  << "' from " << floatFormatNames << nl;
86  }
87 
88  return deflt;
89 }
90 
91 
94 (
95  const word& key,
96  const dictionary& dict,
97  const floatFormat deflt
98 )
99 {
100  return floatFormatNames.getOrDefault(key, dict, deflt, true); // warnOnly
101 }
102 
103 
106 (
107  const word& fmtName,
108  const streamFormat deflt
109 )
110 {
111  // Handle bad input graciously. A no-op for an empty string
112 
113  if (!fmtName.empty())
114  {
115  const auto iter = formatNames.cfind(fmtName);
116 
117  if (iter.good())
118  {
119  return iter.val();
120  }
121 
122  // Fall-through to warning
123 
125  << "Unknown stream format specifier '" << fmtName
126  << "' using '" << formatNames[deflt]
127  << "' from " << formatNames << nl;
128  }
130  return deflt;
131 }
132 
133 
136 (
137  const word& key,
138  const dictionary& dict,
139  const streamFormat deflt
140 )
141 {
142  return formatNames.getOrDefault(key, dict, deflt, true); // warnOnly
143 }
144 
145 
148 (
149  const word& compName,
150  const compressionType deflt
151 )
152 {
153  // Handle bad input graciously. A no-op for an empty string
154 
155  if (!compName.empty())
156  {
157  const Switch sw = Switch::find(compName);
158 
159  if (sw.good())
160  {
161  return
162  (
163  sw
164  ? compressionType::COMPRESSED
165  : compressionType::UNCOMPRESSED
166  );
167  }
168 
169  // Fall-through to warning
170 
172  << "Unknown compression specifier '" << compName
173  << "' using compression " << (deflt ? "on" : "off") << nl;
174  }
176  return deflt;
177 }
178 
179 
182 (
183  const word& key,
184  const dictionary& dict,
185  const compressionType deflt
186 )
187 {
188  return
189  (
190  Switch(key, dict, Switch(bool(deflt)), true) // warnOnly
191  ? compressionType::COMPRESSED
192  : compressionType::UNCOMPRESSED
193  );
194 }
195 
196 
197 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
200 :
201  versionNumber(readFloat(verNum))
202 {}
203 
204 
206 :
207  versionNumber()
208 {
209  if (tok.isStringType())
210  {
211  (*this) = versionNumber(tok.stringToken());
212  }
213  else if (tok.isNumber())
214  {
215  // Accept integer or floating-point
216  // Eg, '2.0' becomes '2' after foamDictionary -expand
217  (*this) = versionNumber(float(tok.number()));
218  }
219  else
220  {
222  << "Wrong token for version - expected word/number, found "
223  << tok.info() << nl;
224  }
225 }
226 
227 
229 (
230  const word& key,
231  const dictionary& dict
232 )
233 :
234  versionNumber()
235 {
236  token tok;
237 
238  if (dict.readIfPresent<token>(key, tok, keyType::LITERAL))
239  {
240  (*this) = versionNumber(tok);
241  }
242 }
243 
244 
245 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
246 
247 Foam::Ostream& Foam::operator<<
248 (
249  Ostream& os,
251 )
252 {
254  return os;
255 }
256 
257 
258 Foam::Ostream& Foam::operator<<
259 (
260  Ostream& os,
261  const IOstreamOption::versionNumber& ver
262 )
263 {
264  // Emit unquoted char sequence (eg, word)
265  // for correct behaviour when sending in parallel
266 
267  os.writeQuoted(ver.str(), false);
268  return os;
269 }
270 
271 
272 // ************************************************************************* //
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:695
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 the enumeration by 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:557
bool isStringType() const noexcept
Token is word-variant or string-variant (WORD, DIRECTIVE, STRING, EXPRESSION, VARIABLE, VERBATIM, CHAR_DATA)
Definition: tokenI.H:777
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:783
#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:1078
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:689
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:563