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