IOstream.H
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-2021 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 Class
25  Foam::IOstream
26 
27 Description
28  An IOstream is an abstract base class for all input/output systems; be
29  they streams, files, token lists etc.
30 
31  The basic operations are construct, close, read token, read primitive
32  and read binary block. In addition version control and line number
33  counting is incorporated. Usually one would use the read primitive
34  member functions, but if one were reading a stream on unknown data
35  sequence one can read token by token, and then analyse.
36 
37 SourceFiles
38  IOstream.C
39 
40 \*---------------------------------------------------------------------------*/
41 
42 #ifndef IOstream_H
43 #define IOstream_H
44 
45 #include "char.H"
46 #include "bool.H"
47 #include "label.H"
48 #include "uLabel.H"
49 #include "scalar.H"
50 #include "fileName.H"
51 #include "InfoProxy.H"
52 
53 #include <iostream>
54 
55 using std::ios_base;
56 using std::istream;
57 using std::ostream;
58 
59 using std::cin;
60 using std::cout;
61 using std::cerr;
62 
63 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
64 
65 namespace Foam
66 {
67 
68 /*---------------------------------------------------------------------------*\
69  Class IOstream Declaration
70 \*---------------------------------------------------------------------------*/
71 
72 class IOstream
73 {
74 
75 public:
76 
77  // Public data types
78 
79  //- Enumeration for whether the stream open or closed
80  enum streamAccess
81  {
83  CLOSED
84  };
85 
86  //- Enumeration for the format of data in the stream
87  enum streamFormat
88  {
90  BINARY
91  };
92 
93  //- Ostream operator
94  friend Ostream& operator<<(Ostream& os, const streamFormat& sf);
95 
96  //- Version number type
97  class versionNumber
98  {
99  //- The version number
100  scalar versionNumber_;
101 
102  //- The version number as an integer
103  int index_;
104 
105 
106  public:
107 
108  // Constructors
109 
110  //- Construct from number
111  versionNumber(const scalar num)
112  :
113  versionNumber_(num),
114  index_(numberToIndex(num))
115  {}
116 
117  //- Construct from Istream
119  :
120  versionNumber_(readScalar(is)),
121  index_(numberToIndex(versionNumber_))
122  {}
123 
124 
125  // Member Functions
126 
127  //- Convert a version number into an index
128  int numberToIndex(const scalar num) const
129  {
130  return int(10*num + small);
131  }
132 
133  //- Return major version
134  int majorVersion() const
135  {
136  return int(versionNumber_);
137  }
138 
139  //- Return minor version
140  int minorVersion() const
141  {
142  return int(10.0*(versionNumber_ - majorVersion()));
143  }
144 
145  //- Return the versionNumber as a character string
146  string str() const;
147 
148 
149  // Member Operators
150 
151  //- Are these versionNumbers the same?
152  bool operator==(const versionNumber& vn) const
153  {
154  return index_ == vn.index_;
155  }
156 
157  //- Are these versionNumbers different?
158  bool operator!=(const versionNumber& vn) const
159  {
160  return index_ != vn.index_;
161  }
162 
163  //- Is this version older than the one given
164  bool operator<(const versionNumber& vn) const
165  {
166  return index_ < vn.index_;
167  }
168 
169  //- Is this version the same as or older than the one given
170  bool operator<=(const versionNumber& vn) const
171  {
172  return index_ <= vn.index_;
173  }
174 
175  //- Is this version newer than the one given
176  bool operator>(const versionNumber& vn) const
177  {
178  return index_ > vn.index_;
179  }
180 
181  //- This version the same as or newer than the one given
182  bool operator>=(const versionNumber& vn) const
183  {
184  return index_ >= vn.index_;
185  }
186 
187 
188  //- Ostream operator
189  friend Ostream& operator<<(Ostream& os, const versionNumber& vn);
190  };
191 
192 
193  //- Enumeration for the format of data in the stream
194  enum compressionType
195  {
197  COMPRESSED
198  };
199 
200 
201  // Public static data
202 
203  //- Current version number
204  static const versionNumber currentVersion;
205 
206  //- Default precision
207  static unsigned int precision_;
208 
209 
210 private:
211 
212  // Private Data
213 
214  //- Name of the stream
215  static fileName name_;
216 
217  streamFormat format_;
218  versionNumber version_;
219  compressionType compression_;
220 
221  streamAccess openClosed_;
222  ios_base::iostate ioState_;
223 
224 
225 protected:
226 
227  // Protected data
230 
231 
232  // Protected Member Functions
233 
234  // Access
235 
236  //- Set stream opened
237  void setOpened()
238  {
239  openClosed_ = OPENED;
240  }
241 
242  //- Set stream closed
243  void setClosed()
244  {
245  openClosed_ = CLOSED;
246  }
247 
248  //- Set stream state
249  void setState(ios_base::iostate state)
250  {
251  ioState_ = state;
252  }
253 
254  //- Set stream to be good
255  void setGood()
256  {
257  ioState_ = ios_base::iostate(0);
258  }
259 
260 
261 public:
262 
263  // Constructors
264 
265  //- Construct setting format and version
267  (
268  streamFormat format,
269  versionNumber version,
271  )
272  :
273  format_(format),
274  version_(version),
275  compression_(compression),
276  openClosed_(CLOSED),
277  ioState_(ios_base::iostate(0)),
278  lineNumber_(0)
279  {
280  setBad();
281  }
282 
283 
284  //- Destructor
285  virtual ~IOstream()
286  {}
287 
288 
289  // Member Functions
290 
291  // Access
292 
293  //- Return the name of the stream
294  // Useful for Fstream to return the filename
295  virtual const fileName& name() const
296  {
297  return name_;
298  }
299 
300  //- Return non-const access to the name of the stream
301  // Useful to alter the stream name
302  virtual fileName& name()
303  {
304  return name_;
305  }
306 
307 
308  // Check
309 
310  //- Check IOstream status for given operation
311  // print IOstream state if error has occurred
312  virtual bool check(const char* operation) const;
313 
314  //- Check IOstream status for given operation
315  // print IOstream state if error has occurred and exit
316  void fatalCheck(const char* operation) const;
317 
318  //- Return true if stream has been opened
319  bool opened() const
320  {
321  return openClosed_ == OPENED;
322  }
323 
324  //- Return true if stream is closed
325  bool closed() const
326  {
327  return openClosed_ == CLOSED;
328  }
329 
330  //- Return true if next operation might succeed
331  bool good() const
332  {
333  return ioState_ == 0;
334  }
335 
336  //- Return true if end of input seen
337  bool eof() const
338  {
339  return ioState_ & ios_base::eofbit;
340  }
341 
342  //- Return true if next operation will fail
343  bool fail() const
344  {
345  return ioState_ & (ios_base::badbit | ios_base::failbit);
346  }
347 
348  //- Return true if stream is corrupted
349  bool bad() const
350  {
351  return ioState_ & ios_base::badbit;
352  }
353 
354  //- Return non-zero if the stream has not failed
355  operator void*() const
356  {
357  return fail()
358  ? reinterpret_cast<void*>(0)
359  : reinterpret_cast<void*>(-1);
360  }
361 
362  //- Return true if the stream has failed
363  bool operator!() const
364  {
365  return fail();
366  }
367 
368 
369  // Stream state functions
370 
371  //- Return stream format of given format name
372  static streamFormat formatEnum(const word&);
373 
374  //- Return current stream format
375  streamFormat format() const
376  {
377  return format_;
378  }
379 
380  //- Set the stream format
381  streamFormat format(const streamFormat fmt)
382  {
383  streamFormat fmt0 = format_;
384  format_ = fmt;
385  return fmt0;
386  }
387 
388  //- Set the stream format from word
389  streamFormat format(const word& fmt)
390  {
391  streamFormat fmt0 = format_;
392  format_ = formatEnum(fmt);
393  return fmt0;
394  }
395 
396  //- Return the stream version
397  versionNumber version() const
398  {
399  return version_;
400  }
401 
402  //- Set the stream version
404  {
405  versionNumber ver0 = version_;
406  version_ = ver;
407  return ver0;
408  }
409 
410  //- Return compression of given compression name
411  static compressionType compressionEnum(const word&);
412 
413  //- Return the stream compression
415  {
416  return compression_;
417  }
418 
419  //- Set the stream compression
421  {
422  compressionType cmp0 = compression_;
423  compression_ = cmp;
424  return cmp0;
425  }
426 
427  //- Set the stream compression from word
428  compressionType compression(const word& cmp)
429  {
430  compressionType cmp0 = compression_;
431  compression_ = compressionEnum(cmp);
432  return cmp0;
433  }
434 
435  //- Return current stream line number
436  label lineNumber() const
437  {
438  return lineNumber_;
439  }
440 
441  //- Return current stream line number
442  label& lineNumber()
443  {
444  return lineNumber_;
445  }
446 
447  //- Set the stream line number
448  label lineNumber(const label ln)
449  {
450  label ln0 = lineNumber_;
451  lineNumber_ = ln;
452  return ln0;
453  }
454 
455  //- Return flags of stream
456  virtual ios_base::fmtflags flags() const = 0;
457 
458  //- Return the default precision
459  static unsigned int defaultPrecision()
460  {
461  return precision_;
462  }
463 
464  //- Reset the default precision (and return old precision)
465  static unsigned int defaultPrecision(unsigned int p)
466  {
467  unsigned int precision0 = precision_;
468  precision_ = p;
469  return precision0;
470  }
471 
472  //- Set stream to have reached eof
473  void setEof()
474  {
475  ioState_ |= ios_base::eofbit;
476  }
477 
478  //- Set stream to have failed
479  void setFail()
480  {
481  ioState_ |= ios_base::failbit;
482  }
483 
484  //- Set stream to be bad
485  void setBad()
486  {
487  ioState_ |= ios_base::badbit;
488  }
489 
490  //- Set flags of stream
491  virtual ios_base::fmtflags flags(const ios_base::fmtflags f) = 0;
492 
493  //- Set flags of stream
494  ios_base::fmtflags setf(const ios_base::fmtflags f)
495  {
496  return flags(flags() | f);
497  }
498 
499  //- Set flags of given field of stream
500  ios_base::fmtflags setf
501  (
502  const ios_base::fmtflags f,
503  const ios_base::fmtflags mask
504  )
505  {
506  return flags((flags() & ~mask) | (f & mask));
507  }
508 
509  //- Unset flags of stream
510  void unsetf(const ios_base::fmtflags uf)
511  {
512  flags(flags()&~uf);
513  }
514 
515 
516  // Print
517 
518  //- Print description of IOstream to Ostream
519  virtual void print(Ostream&) const;
520 
521  //- Check given stream state bits
522  void print(Ostream&, const int streamState) const;
523 
524 
525  // Info
526 
527  //- Return info proxy.
528  // Used to print IOstream information to a stream
529  InfoProxy<IOstream> info() const
530  {
531  return *this;
532  }
533 };
534 
535 
538 
539 template<>
540 Ostream& operator<<(Ostream& os, const InfoProxy<IOstream>& ip);
541 
542 
543 // --------------------------------------------------------------------
544 // ------ Manipulators (not taking arguments)
545 // --------------------------------------------------------------------
547 typedef IOstream& (*IOstreamManip)(IOstream&);
548 
549 //- operator<< handling for manipulators without arguments
551 {
552  return f(io);
553 }
554 
556 inline IOstream& dec(IOstream& io)
557 {
559  return io;
560 }
562 inline IOstream& hex(IOstream& io)
563 {
565  return io;
566 }
568 inline IOstream& oct(IOstream& io)
569 {
571  return io;
572 }
574 inline IOstream& fixed(IOstream& io)
575 {
576  io.setf(ios_base::fixed, ios_base::floatfield);
577  return io;
578 }
580 inline IOstream& scientific(IOstream& io)
581 {
582  io.setf(ios_base::scientific, ios_base::floatfield);
583  return io;
584 }
585 
586 
587 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
588 
589 } // End namespace Foam
590 
591 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
592 
593 #endif
594 
595 // ************************************************************************* //
label lineNumber_
Definition: IOstream.H:228
bool operator>=(const versionNumber &vn) const
This version the same as or newer than the one given.
Definition: IOstream.H:181
versionNumber(const scalar num)
Construct from number.
Definition: IOstream.H:110
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
void unsetf(const ios_base::fmtflags uf)
Unset flags of stream.
Definition: IOstream.H:509
bool operator>(const versionNumber &vn) const
Is this version newer than the one given.
Definition: IOstream.H:175
void setClosed()
Set stream closed.
Definition: IOstream.H:242
int numberToIndex(const scalar num) const
Convert a version number into an index.
Definition: IOstream.H:127
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:348
ios_base::fmtflags setf(const ios_base::fmtflags f)
Set flags of stream.
Definition: IOstream.H:493
IOstream & hex(IOstream &io)
Definition: IOstream.H:561
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
virtual ios_base::fmtflags flags() const =0
Return flags of stream.
void setGood()
Set stream to be good.
Definition: IOstream.H:254
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.H:458
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
bool operator<(const versionNumber &vn) const
Is this version older than the one given.
Definition: IOstream.H:163
streamAccess
Enumeration for whether the stream open or closed.
Definition: IOstream.H:79
bool operator==(const versionNumber &vn) const
Are these versionNumbers the same?
Definition: IOstream.H:151
versionNumber version() const
Return the stream version.
Definition: IOstream.H:396
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:435
IOstream & fixed(IOstream &io)
Definition: IOstream.H:573
void setFail()
Set stream to have failed.
Definition: IOstream.H:478
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
int minorVersion() const
Return minor version.
Definition: IOstream.H:139
IOstream & oct(IOstream &io)
Definition: IOstream.H:567
virtual ~IOstream()
Destructor.
Definition: IOstream.H:284
A character and a pointer to a character string.
bool operator!=(const versionNumber &vn) const
Are these versionNumbers different?
Definition: IOstream.H:157
void setBad()
Set stream to be bad.
Definition: IOstream.H:484
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
bool readScalar(const char *buf, doubleScalar &s)
Read whole of buf as a scalar. Return true if successful.
Definition: doubleScalar.H:75
streamFormat format() const
Return current stream format.
Definition: IOstream.H:374
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
bool ln(const fileName &src, const fileName &dst)
Create a softlink. dst should not exist. Returns true if successful.
Definition: POSIX.C:908
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
labelList f(nPoints)
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
An IOstream is an abstract base class for all input/output systems; be they streams, files, token lists etc.
Definition: IOstream.H:71
static unsigned int precision_
Default precision.
Definition: IOstream.H:206
void setEof()
Set stream to have reached eof.
Definition: IOstream.H:472
friend Ostream & operator<<(Ostream &os, const versionNumber &vn)
Ostream operator.
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:45
void setOpened()
Set stream opened.
Definition: IOstream.H:236
static const versionNumber currentVersion
Current version number.
Definition: IOstream.H:203
string str() const
Return the versionNumber as a character string.
Definition: IOstream.C:116
Version number type.
Definition: IOstream.H:96
bool operator<=(const versionNumber &vn) const
Is this version the same as or older than the one given.
Definition: IOstream.H:169
friend Ostream & operator<<(Ostream &os, const streamFormat &sf)
Ostream operator.
int majorVersion() const
Return major version.
Definition: IOstream.H:133
IOstream & dec(IOstream &io)
Definition: IOstream.H:555
void setState(ios_base::iostate state)
Set stream state.
Definition: IOstream.H:248
volScalarField & p
virtual void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: IOstream.C:126
bool operator!() const
Return true if the stream has failed.
Definition: IOstream.H:362
IOstream &(* IOstreamManip)(IOstream &)
Definition: IOstream.H:546
InfoProxy< IOstream > info() const
Return info proxy.
Definition: IOstream.H:528
System bool.
IOstream & scientific(IOstream &io)
Definition: IOstream.H:579
Namespace for OpenFOAM.
bool closed() const
Return true if stream is closed.
Definition: IOstream.H:324
IOstream(streamFormat format, versionNumber version, compressionType compression=UNCOMPRESSED)
Construct setting format and version.
Definition: IOstream.H:266