IOstream.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2025 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "IOstream.H"
27 #include "error.H"
28 #include "Switch.H"
29 #include <sstream>
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 Foam::fileName Foam::IOstream::name_("IOstream");
34 
35 
36 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
37 
40 {
41  if (format == "ascii")
42  {
43  return IOstream::ASCII;
44  }
45  else if (format == "binary")
46  {
47  return IOstream::BINARY;
48  }
49  else
50  {
52  << "bad format specifier '" << format << "', using 'ascii'"
53  << endl;
54 
55  return IOstream::ASCII;
56  }
57 }
58 
59 
62 {
63  // get Switch (bool) value, but allow it to fail
64  Switch sw(compression, true);
65 
66  if (sw.valid())
67  {
69  }
70  else if (compression == "uncompressed")
71  {
73  }
74  else if (compression == "compressed")
75  {
76  return IOstream::COMPRESSED;
77  }
78  else
79  {
81  << "bad compression specifier '" << compression
82  << "', using 'uncompressed'"
83  << endl;
84 
86  }
87 }
88 
89 
91 {
92  static const unsigned int p = log10(1/pow(small, 0.75));
93 
94  return max(defaultPrecision(), p);
95 }
96 
97 
99 {
100  static const unsigned int p = log10(1/small);
101 
102  return max(defaultPrecision(), p);
103 }
104 
105 
106 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
107 
108 bool Foam::IOstream::check(const char* operation) const
109 {
110  if (bad())
111  {
113  << "error in IOstream " << name() << " for operation " << operation
114  << exit(FatalIOError);
115  }
116 
117  return !bad();
118 }
119 
120 
121 void Foam::IOstream::fatalCheck(const char* operation) const
122 {
123  if (bad())
124  {
126  << "error in IOstream " << name() << " for operation " << operation
127  << exit(FatalIOError);
128  }
129 }
130 
131 
133 {
134  std::ostringstream os;
135  os.precision(1);
136  os.setf(ios_base::fixed, ios_base::floatfield);
137  os << versionNumber_;
138  return os.str();
139 }
140 
141 
143 {
144  os << "IOstream: " << "Version " << version_ << ", format ";
145 
146  switch (format_)
147  {
148  case ASCII:
149  os << "ASCII";
150  break;
151 
152  case BINARY:
153  os << "BINARY";
154  break;
155  }
156 
157  os << ", line " << lineNumber();
158 
159  if (opened())
160  {
161  os << ", OPENED";
162  }
163 
164  if (closed())
165  {
166  os << ", CLOSED";
167  }
168 
169  if (good())
170  {
171  os << ", GOOD";
172  }
173 
174  if (eof())
175  {
176  os << ", EOF";
177  }
178 
179  if (fail())
180  {
181  os << ", FAIL";
182  }
183 
184  if (bad())
185  {
186  os << ", BAD";
187  }
188 
189  os << endl;
190 }
191 
192 
193 void Foam::IOstream::print(Ostream& os, const int streamState) const
194 {
195  if (streamState == ios_base::goodbit)
196  {
197  os << "ios_base::goodbit set : the last operation on stream succeeded"
198  << endl;
199  }
200  else if (streamState & ios_base::badbit)
201  {
202  os << "ios_base::badbit set : characters possibly lost"
203  << endl;
204  }
205  else if (streamState & ios_base::failbit)
206  {
207  os << "ios_base::failbit set : some type of formatting error"
208  << endl;
209  }
210  else if (streamState & ios_base::eofbit)
211  {
212  os << "ios_base::eofbit set : at end of stream"
213  << endl;
214  }
215 }
216 
217 
218 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
219 
221 {
222  if (sf == IOstream::ASCII)
223  {
224  os << "ascii";
225  }
226  else
227  {
228  os << "binary";
229  }
230 
231  return os;
232 }
233 
234 
236 {
237  os << vn.str().c_str();
238  return os;
239 }
240 
241 
242 template<>
244 {
245  ip.t_.print(os);
246  return os;
247 }
248 
249 
250 // ************************************************************************* //
Version number type.
Definition: IOstream.H:97
string str() const
Return the versionNumber as a character string.
Definition: IOstream.C:132
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:108
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:121
static unsigned int fullPrecision()
Return a full precision for writing data that is *very*.
Definition: IOstream.C:98
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:87
static streamFormat formatEnum(const word &)
Return stream format of given format name.
Definition: IOstream.C:39
virtual void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: IOstream.C:142
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:194
static compressionType compressionEnum(const word &)
Return compression of given compression name.
Definition: IOstream.C:61
static unsigned int highPrecision()
Return a high precision for writing data that is.
Definition: IOstream.C:90
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:50
const T & t_
Definition: InfoProxy.H:53
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A simple wrapper around bool so that it can be read as a word: true/false, on/off,...
Definition: Switch.H:61
bool valid() const
Return true if the Switch has a valid value.
Definition: Switch.C:112
A class for handling file names.
Definition: fileName.H:82
A class for handling character strings derived from std::string.
Definition: string.H:79
A class for handling words, derived from string.
Definition: word.H:63
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
volScalarField sf(fieldObject, mesh)
#define WarningInFunction
Report a warning using Foam::Warning.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
IOstream & fixed(IOstream &io)
Definition: IOstream.H:597
dimensionedScalar log10(const dimensionedScalar &ds)
IOerror FatalIOError
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
tmp< DimensionedField< typename powProduct< Type, r >::type, GeoMesh, Field > > pow(const DimensionedField< Type, GeoMesh, PrimitiveField > &df, typename powProduct< Type, r >::type)
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
dimensioned< Type > max(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
word format(conversionProperties.lookup("format"))
volScalarField & p