Loading...
Searching...
No Matches
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-2025 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
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
38const Foam::Enum
39<
41>
43({
44 { floatFormat::general, "general" },
45 { floatFormat::fixed, "fixed" },
46 { floatFormat::scientific, "scientific" },
47});
48
49const Foam::Enum
50<
52>
54({
55 { streamFormat::ASCII, "ascii" },
56 { streamFormat::BINARY, "binary" },
57 // No selection by name: UNKNOWN_FORMAT
58});
59
60
61// * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
62
65(
66 const word& fmtName,
67 const floatFormat deflt
68)
69{
70 if (fmtName.empty())
71 {
72 // Empty string (no-op)
73 }
74 else if (auto iter = floatFormatNames.cfind(fmtName); iter.good())
75 {
76 return iter.val();
77 }
78 else
79 {
80 // Emit warning for bad input
81
82 auto& err = WarningInFunction
83 << "Unknown float format '" << fmtName << "' using ";
84
85 if (auto iter = floatFormatNames.cfind(deflt); iter.good())
86 {
87 err << '\'' << iter.key() << '\'';
88 }
89 else
90 {
91 err << "value=" << int(deflt);
92 }
93 err << " from " << floatFormatNames << nl;
103 const word& key,
104 const dictionary& dict,
105 const floatFormat deflt
107{
108 return floatFormatNames.getOrDefault(key, dict, deflt, true); // warnOnly
109}
110
111
114(
115 const word& fmtName,
116 const streamFormat deflt
117)
118{
119 if (fmtName.empty())
120 {
121 // Empty string (no-op)
122 }
123 else if (auto iter = formatNames.cfind(fmtName); iter.good())
124 {
125 return iter.val();
126 }
127 else
128 {
129 // Emit warning for bad input
130
131 auto& err = WarningInFunction
132 << "Unknown stream format '" << fmtName << "' using ";
133
134 if (auto iter = formatNames.cfind(deflt); iter.good())
135 {
136 err << '\'' << iter.key() << '\'';
137 }
138 else
139 {
140 err << "value=" << int(deflt);
141 }
142 err << " from " << formatNames << nl;
144
145 return deflt;
146}
147
148
151(
152 const word& key,
153 const dictionary& dict,
154 const streamFormat deflt
156{
157 return formatNames.getOrDefault(key, dict, deflt, true); // warnOnly
158}
159
160
163(
164 const word& compName,
165 const compressionType deflt
166)
167{
168 if (compName.empty())
169 {
170 // Empty string (no-op)
171 }
172 else if (Switch sw = Switch::find(compName); sw.good())
173 {
174 return
175 (
176 sw
177 ? compressionType::COMPRESSED
178 : compressionType::UNCOMPRESSED
179 );
180 }
181 else
182 {
183 // Emit warning
184
186 << "Unknown compression specifier '" << compName
187 << "' using compression " << (deflt ? "on" : "off") << nl;
189
190 return deflt;
191}
192
193
196(
197 const word& key,
198 const dictionary& dict,
199 const compressionType deflt
200)
201{
202 return
203 (
204 Switch(key, dict, Switch(bool(deflt)), true) // warnOnly
205 ? compressionType::COMPRESSED
210
211// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
214:
215 versionNumber(readFloat(verNum))
216{}
217
218
220:
222{
223 if (tok.isStringType())
224 {
225 (*this) = versionNumber(tok.stringToken());
226 }
227 else if (tok.isNumber())
228 {
229 // Accept integer or floating-point
230 // Eg, '2.0' becomes '2' after foamDictionary -expand
231 (*this) = versionNumber(float(tok.number()));
232 }
233 else
234 {
235 WarningInFunction
236 << "Wrong token for version - expected word/number, found "
237 << tok.info() << nl;
238 }
239}
240
241
243(
244 const word& key,
245 const dictionary& dict
246)
247:
249{
250 token tok;
251
252 if (dict.readIfPresent<token>(key, tok, keyType::LITERAL))
253 {
254 (*this) = versionNumber(tok);
255 }
256}
257
258
259// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
260
261Foam::Ostream& Foam::operator<<
262(
263 Ostream& os,
265)
267 // Silently ignores unnamed formats
269 return os;
270}
271
272
273Foam::Ostream& Foam::operator<<
274(
275 Ostream& os,
276 const IOstreamOption::versionNumber& ver
277)
278{
279 // Emit unquoted char sequence (eg, word)
280 // for correct behaviour when sending in parallel
281
282 os.writeQuoted(ver.str(), false);
283 return os;
284}
285
286
287// ************************************************************************* //
if(patchID !=-1)
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition Enum.H:57
const_iterator cfind(const word &key) const
Find key/value pair by enumeration name.
Definition EnumI.H:236
Representation of a major/minor version number.
constexpr versionNumber() noexcept
Default construct current version. The value (2.0) corresponds to the current version.
floatFormat
Float formats (eg, time directory name formats).
static floatFormat floatFormatEnum(const word &fmtName, const floatFormat deflt=floatFormat::general)
Lookup floatFormat enum corresponding to the string (general | fixed | scientific).
static compressionType compressionEnum(const word &compName, const compressionType deflt=compressionType::UNCOMPRESSED)
The compression enum corresponding to the string.
streamFormat
Data format (ascii | binary | coherent).
static const Enum< floatFormat > floatFormatNames
Names for float formats (general, fixed, scientific).
static streamFormat formatEnum(const word &fmtName, const streamFormat deflt=streamFormat::ASCII)
Lookup streamFormat enum corresponding to the string (ascii | binary).
static const Enum< streamFormat > formatNames
Stream format names (ascii, binary).
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED).
@ UNCOMPRESSED
compression = false
@ COMPRESSED
compression = true
static const versionNumber currentVersion
The current version number (2.0).
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A simple wrapper around bool so that it can be read as a word: true/false, on/off,...
Definition Switch.H:81
bool good() const noexcept
True if the Switch represents a valid enumeration.
Definition Switch.H:307
static Switch find(const char *s)
Find switchType for the given string, returning a Switch that can be tested for good() or bad().
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
@ LITERAL
String literal.
Definition keyType.H:82
A token holds an item read from Istream.
Definition token.H:70
bool isNumber() const noexcept
Token is (signed/unsigned) integer type, FLOAT or DOUBLE.
Definition tokenI.H:992
bool isStringType() const noexcept
Token is word-variant or string-variant (WORD, DIRECTIVE, STRING, EXPRESSION, VARIABLE,...
Definition tokenI.H:1070
A class for handling words, derived from Foam::string.
Definition word.H:66
OBJstream os(runTime.globalPath()/outputName)
#define WarningInFunction
Report a warning using Foam::Warning.
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
dictionary dict