FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 Description
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "IOstream.H"
29 #include <OpenFOAM/error.H>
30 #include <sstream>
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 Foam::fileName Foam::IOstream::name_("IOstream");
35 
36 
37 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
38 
41 {
42  if (format == "ascii")
43  {
44  return IOstream::ASCII;
45  }
46  else if (format == "binary")
47  {
48  return IOstream::BINARY;
49  }
50  else
51  {
52  WarningIn("IOstream::formatEnum(const word&)")
53  << "bad format specifier '" << format << "', using 'ascii'"
54  << endl;
55 
56  return IOstream::ASCII;
57  }
58 }
59 
60 
63 {
64  if (compression == "uncompressed")
65  {
67  }
68  else if (compression == "compressed")
69  {
70  return IOstream::COMPRESSED;
71  }
72  else
73  {
74  WarningIn("IOstream::compressionEnum(const word&)")
75  << "bad compression specifier '" << compression
76  << "', using 'uncompressed'"
77  << endl;
78 
80  }
81 }
82 
83 
84 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
85 
86 bool Foam::IOstream::check(const char* operation) const
87 {
88  if (bad())
89  {
91  (
92  "IOstream::check(const char*) const", *this
93  ) << "error in IOstream " << name() << " for operation " << operation
94  << exit(FatalIOError);
95  }
96 
97  return !bad();
98 }
99 
100 
101 void Foam::IOstream::fatalCheck(const char* operation) const
102 {
103  if (bad())
104  {
106  (
107  "IOstream::fatalCheck(const char*) const", *this
108  ) << "error in IOstream " << name() << " for operation " << operation
109  << exit(FatalIOError);
110  }
111 }
112 
113 
115 {
116  std::ostringstream os;
117  os.precision(1);
118  os.setf(ios_base::fixed, ios_base::floatfield);
119  os << versionNumber_;
120  return os.str();
121 }
122 
123 
125 {
126  os << "IOstream: " << "Version " << version_ << ", format ";
127 
128  switch (format_)
129  {
130  case ASCII:
131  os << "ASCII";
132  break;
133 
134  case BINARY:
135  os << "BINARY";
136  break;
137  }
138 
139  os << ", line " << lineNumber();
140 
141  if (opened())
142  {
143  os << ", OPENED";
144  }
145 
146  if (closed())
147  {
148  os << ", CLOSED";
149  }
150 
151  if (good())
152  {
153  os << ", GOOD";
154  }
155 
156  if (eof())
157  {
158  os << ", EOF";
159  }
160 
161  if (fail())
162  {
163  os << ", FAIL";
164  }
165 
166  if (bad())
167  {
168  os << ", BAD";
169  }
170 
171  os << endl;
172 }
173 
174 
175 void Foam::IOstream::print(Ostream& os, const int streamState) const
176 {
177  if (streamState == ios_base::goodbit)
178  {
179  os << "ios_base::goodbit set : the last operation on stream succeeded"
180  << endl;
181  }
182  else if (streamState & ios_base::badbit)
183  {
184  os << "ios_base::badbit set : characters possibly lost"
185  << endl;
186  }
187  else if (streamState & ios_base::failbit)
188  {
189  os << "ios_base::failbit set : some type of formatting error"
190  << endl;
191  }
192  else if (streamState & ios_base::eofbit)
193  {
194  os << "ios_base::eofbit set : at end of stream"
195  << endl;
196  }
197 }
198 
199 
200 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
201 
203 {
204  if (sf == IOstream::ASCII)
205  {
206  os << "ascii";
207  }
208  else
209  {
210  os << "binary";
211  }
212 
213  return os;
214 }
215 
216 
218 {
219  os << vn.str().c_str();
220  return os;
221 }
222 
223 
224 
225 namespace Foam
226 {
227 
228 # if defined (__GNUC__)
229  template<>
230 # endif
231  Ostream& operator<<(Ostream& os, const InfoProxy<IOstream>& ip)
232  {
233  ip.t_.print(os);
234  return os;
235  }
236 
237 } // end namespace
238 
239 
240 // ************************ vim: set sw=4 sts=4 et: ************************ //