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 "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  { 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  }
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 * * * * * * * * * * * * * * //
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,
206  const IOstreamOption::versionNumber& ver
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 // ************************************************************************* //
dictionary dict
bool good() const noexcept
True if the Switch represents a valid enumeration.
Definition: Switch.C:296
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:120
scalar number() const
Return label, float or double value.
Definition: tokenI.H:586
InfoProxy< token > info() const
Return info proxy for printing token information to a stream.
Definition: token.H:788
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
static const Enum< streamFormat > formatNames
Stream format names (ascii, binary)
A token holds an item read from Istream.
Definition: token.H:64
bool found(const word &enumName) const
True if there is an enumeration corresponding to the given name.
Definition: EnumI.H:96
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
static streamFormat formatEnum(const word &formatName, const streamFormat deflt=streamFormat::ASCII)
The stream format enum corresponding to the string (ascii | binary).
bool isStringType() const noexcept
Token is word-variant or string-variant (WORD, DIRECTIVE, STRING, EXPRESSION, VARIABLE, VERBATIM)
Definition: tokenI.H:676
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:55
OBJstream os(runTime.globalPath()/outputName)
const string & stringToken() const
Return const reference to the string contents.
Definition: tokenI.H:682
#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...
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
constexpr versionNumber() noexcept
Default construct current version. The value (2.0) corresponds to the current version.
streamFormat
Data format (ascii | binary)
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:580
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