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-2020 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 "error.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 { streamFormat::ASCII, "ascii" },
45 { streamFormat::BINARY, "binary" },
46});
47
48
49// * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
50
53(
54 const word& formatName,
55 const streamFormat deflt
56)
57{
58 // Handle bad input graciously. A no-op for an empty string
59
60 if (!formatName.empty())
61 {
62 if (formatNames.found(formatName))
63 {
64 return formatNames[formatName];
65 }
66
67 // Fall-through to warning
68
70 << "Unknown format specifier '" << formatName
71 << "', using '" << formatNames[deflt] << "'\n";
72 }
73
74 return deflt;
75}
76
77
80(
81 const word& key,
82 const dictionary& dict,
83 const streamFormat deflt
84)
85{
86 return formatNames.getOrDefault(key, dict, deflt, true); // failsafe=true
87}
88
89
92(
93 const word& compName,
94 const compressionType deflt
95)
96{
97 // Handle bad input graciously. A no-op for an empty string
98
99 if (!compName.empty())
100 {
101 const Switch sw = Switch::find(compName);
102
103 if (sw.good())
104 {
105 return
106 (
107 sw
108 ? compressionType::COMPRESSED
109 : compressionType::UNCOMPRESSED
110 );
111 }
112
113 // Fall-through to warning
114
116 << "Unknown compression specifier '" << compName
117 << "', using compression "
118 << (deflt ? "on" : "off" ) << nl;
119 }
120
121 return deflt;
122}
123
124
127(
128 const word& key,
129 const dictionary& dict,
130 const compressionType deflt
131)
132{
133 return
134 (
135 Switch(key, dict, Switch(bool(deflt)), true) // failsafe=true
136 ? compressionType::COMPRESSED
137 : compressionType::UNCOMPRESSED
138 );
139}
140
141
142// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
143
145:
146 versionNumber(readFloat(verNum))
147{}
148
149
151:
153{
154 if (tok.isStringType())
155 {
156 (*this) = versionNumber(tok.stringToken());
157 }
158 else if (tok.isNumber())
159 {
160 // Accept integer or floating-point
161 // Eg, '2.0' becomes '2' after foamDictionary -expand
162 (*this) = versionNumber(float(tok.number()));
163 }
164 else
165 {
167 << "Wrong token for version - expected word/number, found "
168 << tok.info() << nl;
169 }
170}
171
172
174(
175 const word& key,
176 const dictionary& dict
177)
178:
180{
181 token tok;
182
184 {
185 (*this) = versionNumber(tok);
186 }
187}
188
189
190// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
191
192Foam::Ostream& Foam::operator<<
193(
194 Ostream& os,
196)
197{
199 return os;
200}
201
202
203Foam::Ostream& Foam::operator<<
204(
205 Ostream& os,
207)
208{
209 // Emit unquoted char sequence (eg, word)
210 // for correct behaviour when sending in parallel
211
212 os.writeQuoted(ver.str(), false);
213 return os;
214}
215
216
217// ************************************************************************* //
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: Enum.H:61
bool found(const word &enumName) const
True if there is an enumeration corresponding to the given name.
Definition: EnumI.H:103
Representation of a major/minor version number.
constexpr versionNumber() noexcept
static compressionType compressionEnum(const word &compName, const compressionType deflt=compressionType::UNCOMPRESSED)
The compression enum corresponding to the string.
streamFormat
Data format (ascii | binary)
static streamFormat formatEnum(const word &formatName, const streamFormat deflt=streamFormat::ASCII)
static const Enum< streamFormat > formatNames
Stream format names (ascii, binary)
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
static const versionNumber currentVersion
The current version number (2.0)
virtual Ostream & writeQuoted(const std::string &str, const bool quoted=true)
Write std::string surrounded by quotes.
Definition: OBJstream.C:108
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A simple wrapper around bool so that it can be read as a word: true/false, on/off,...
Definition: Switch.H:78
bool good() const noexcept
True if the Switch represents a valid enumeration.
Definition: Switch.C:300
static Switch find(const std::string &str)
Definition: Switch.C:151
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
bool readIfPresent(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX) const
@ LITERAL
String literal.
Definition: keyType.H:81
A token holds an item read from Istream.
Definition: token.H:69
bool isNumber() const noexcept
Token is LABEL, FLOAT or DOUBLE.
Definition: tokenI.H:587
const string & stringToken() const
Return const reference to the string contents.
Definition: tokenI.H:689
InfoProxy< token > info() const
Return info proxy for printing token information to a stream.
Definition: token.H:586
bool isStringType() const noexcept
Definition: tokenI.H:683
scalar number() const
Return label, float or double value.
Definition: tokenI.H:593
A class for handling words, derived from Foam::string.
Definition: word.H:68
OBJstream os(runTime.globalPath()/outputName)
#define WarningInFunction
Report a warning using Foam::Warning.
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict