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 -------------------------------------------------------------------------------
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 "error.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  { 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 :
152  versionNumber()
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 :
179  versionNumber()
180 {
181  token tok;
182 
183  if (dict.readIfPresent<token>(key, tok, keyType::LITERAL))
184  {
185  (*this) = versionNumber(tok);
186  }
187 }
188 
189 
190 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
191 
192 Foam::Ostream& Foam::operator<<
193 (
194  Ostream& os,
196 )
197 {
199  return os;
200 }
201 
202 
203 Foam::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 // ************************************************************************* //
Foam::Switch
A simple wrapper around bool so that it can be read as a word: true/false, on/off,...
Definition: Switch.H:77
Foam::Enum
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: IOstreamOption.H:57
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::token::stringToken
const string & stringToken() const
Return const reference to the string contents.
Definition: tokenI.H:689
Foam::token::isStringType
bool isStringType() const noexcept
Definition: tokenI.H:683
Foam::glTF::key
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
Foam::IOstreamOption::currentVersion
static const versionNumber currentVersion
The current version number (2.0)
Definition: IOstreamOption.H:165
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::Switch::good
bool good() const noexcept
True if the Switch represents a valid enumeration.
Definition: Switch.C:300
Foam::token::isNumber
bool isNumber() const noexcept
Token is LABEL, FLOAT or DOUBLE.
Definition: tokenI.H:587
Foam::token::number
scalar number() const
Return label, float or double value.
Definition: tokenI.H:593
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:85
error.H
Foam::token::info
InfoProxy< token > info() const
Return info proxy for printing token information to a stream.
Definition: token.H:586
IOstreamOption.H
Switch.H
Foam::IOstreamOption::versionNumber::versionNumber
constexpr versionNumber() noexcept
Definition: IOstreamOption.H:96
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::IOstreamOption::compressionEnum
static compressionType compressionEnum(const word &compName, const compressionType deflt=compressionType::UNCOMPRESSED)
The compression enum corresponding to the string.
Definition: IOstreamOption.C:92
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:70
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
Foam::ListOps::find
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Find index of the first occurrence that satisfies the predicate.
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::IOstreamOption::formatEnum
static streamFormat formatEnum(const word &formatName, const streamFormat deflt=streamFormat::ASCII)
Definition: IOstreamOption.C:53
dictionary.H
Foam::IOstreamOption::compressionType
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
Definition: IOstreamOption.H:77
Foam::keyType::LITERAL
String literal.
Definition: keyType.H:81
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:328
Foam::IOstreamOption::formatNames
static const Enum< streamFormat > formatNames
Stream format names (ascii, binary)
Definition: IOstreamOption.H:162
Enum.H