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-2018 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 
90 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
91 
92 bool Foam::IOstream::check(const char* operation) const
93 {
94  if (bad())
95  {
97  << "error in IOstream " << name() << " for operation " << operation
98  << exit(FatalIOError);
99  }
100 
101  return !bad();
102 }
103 
104 
105 void Foam::IOstream::fatalCheck(const char* operation) const
106 {
107  if (bad())
108  {
110  << "error in IOstream " << name() << " for operation " << operation
111  << exit(FatalIOError);
112  }
113 }
114 
115 
117 {
118  std::ostringstream os;
119  os.precision(1);
120  os.setf(ios_base::fixed, ios_base::floatfield);
121  os << versionNumber_;
122  return os.str();
123 }
124 
125 
127 {
128  os << "IOstream: " << "Version " << version_ << ", format ";
129 
130  switch (format_)
131  {
132  case ASCII:
133  os << "ASCII";
134  break;
135 
136  case BINARY:
137  os << "BINARY";
138  break;
139  }
140 
141  os << ", line " << lineNumber();
142 
143  if (opened())
144  {
145  os << ", OPENED";
146  }
147 
148  if (closed())
149  {
150  os << ", CLOSED";
151  }
152 
153  if (good())
154  {
155  os << ", GOOD";
156  }
157 
158  if (eof())
159  {
160  os << ", EOF";
161  }
162 
163  if (fail())
164  {
165  os << ", FAIL";
166  }
167 
168  if (bad())
169  {
170  os << ", BAD";
171  }
172 
173  os << endl;
174 }
175 
176 
177 void Foam::IOstream::print(Ostream& os, const int streamState) const
178 {
179  if (streamState == ios_base::goodbit)
180  {
181  os << "ios_base::goodbit set : the last operation on stream succeeded"
182  << endl;
183  }
184  else if (streamState & ios_base::badbit)
185  {
186  os << "ios_base::badbit set : characters possibly lost"
187  << endl;
188  }
189  else if (streamState & ios_base::failbit)
190  {
191  os << "ios_base::failbit set : some type of formatting error"
192  << endl;
193  }
194  else if (streamState & ios_base::eofbit)
195  {
196  os << "ios_base::eofbit set : at end of stream"
197  << endl;
198  }
199 }
200 
201 
202 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
203 
205 {
206  if (sf == IOstream::ASCII)
207  {
208  os << "ascii";
209  }
210  else
211  {
212  os << "binary";
213  }
214 
215  return os;
216 }
217 
218 
220 {
221  os << vn.str().c_str();
222  return os;
223 }
224 
225 
226 template<>
227 Foam::Ostream& Foam::operator<<(Ostream& os, const InfoProxy<IOstream>& ip)
228 {
229  ip.t_.print(os);
230  return os;
231 }
232 
233 
234 // ************************************************************************* //
bool valid() const
Return true if the Switch has a valid value.
Definition: Switch.C:123
static streamFormat formatEnum(const word &)
Return stream format of given format name.
Definition: IOstream.C:39
static compressionType compressionEnum(const word &)
Return compression of given compression name.
Definition: IOstream.C:61
A class for handling file names.
Definition: fileName.H:79
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:348
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
A simple wrapper around bool so that it can be read as a word: true/false, on/off, yes/no, y/n, t/f, or none/any.
Definition: Switch.H:60
word format(conversionProperties.lookup("format"))
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:435
IOstream & fixed(IOstream &io)
Definition: IOstream.H:573
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
A class for handling words, derived from string.
Definition: word.H:59
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:294
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:105
bool eof() const
Return true if end of input seen.
Definition: IOstream.H:336
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
bool opened() const
Return true if stream has been opened.
Definition: IOstream.H:318
volScalarField sf(fieldObject, mesh)
bool fail() const
Return true if next operation will fail.
Definition: IOstream.H:342
compressionType compression() const
Return the stream compression.
Definition: IOstream.H:413
#define WarningInFunction
Report a warning using Foam::Warning.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:318
string str() const
Return the versionNumber as a character string.
Definition: IOstream.C:116
Ostream & operator<<(Ostream &, const ensightPart &)
Version number type.
Definition: IOstream.H:96
virtual void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: IOstream.C:126
A class for handling character strings derived from std::string.
Definition: string.H:76
bool closed() const
Return true if stream is closed.
Definition: IOstream.H:324
IOerror FatalIOError