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-2019 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)
153  {
154  return index_ == vn.index_;
155  }
156 
157  //- Are these versionNumbers different?
158  bool operator!=(const versionNumber& vn)
159  {
160  return index_ != vn.index_;
161  }
162 
163  //- Is this version older than the one given
164  bool operator<(const versionNumber& vn)
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)
171  {
172  return index_ <= vn.index_;
173  }
174 
175  //- Is this version newer than the one given
176  bool operator>(const versionNumber& vn)
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)
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  //- Original version number
204  static const versionNumber originalVersion;
205 
206  //- Current version number
207  static const versionNumber currentVersion;
208 
209  //- Default precision
210  static unsigned int precision_;
211 
212 
213 private:
214 
215  // Private Data
216 
217  //- Name of the stream
218  static fileName name_;
219 
220  streamFormat format_;
221  versionNumber version_;
222  compressionType compression_;
223 
224  streamAccess openClosed_;
225  ios_base::iostate ioState_;
226 
227 
228 protected:
229 
230  // Protected data
233 
234 
235  // Protected Member Functions
236 
237  // Access
238 
239  //- Set stream opened
240  void setOpened()
241  {
242  openClosed_ = OPENED;
243  }
244 
245  //- Set stream closed
246  void setClosed()
247  {
248  openClosed_ = CLOSED;
249  }
250 
251  //- Set stream state
252  void setState(ios_base::iostate state)
253  {
254  ioState_ = state;
255  }
256 
257  //- Set stream to be good
258  void setGood()
259  {
260  ioState_ = ios_base::iostate(0);
261  }
262 
263 
264 public:
265 
266  // Constructors
267 
268  //- Construct setting format and version
270  (
271  streamFormat format,
272  versionNumber version,
274  )
275  :
276  format_(format),
277  version_(version),
278  compression_(compression),
279  openClosed_(CLOSED),
280  ioState_(ios_base::iostate(0)),
281  lineNumber_(0)
282  {
283  setBad();
284  }
285 
286 
287  //- Destructor
288  virtual ~IOstream()
289  {}
290 
291 
292  // Member Functions
293 
294  // Access
295 
296  //- Return the name of the stream
297  // Useful for Fstream to return the filename
298  virtual const fileName& name() const
299  {
300  return name_;
301  }
302 
303  //- Return non-const access to the name of the stream
304  // Useful to alter the stream name
305  virtual fileName& name()
306  {
307  return name_;
308  }
309 
310 
311  // Check
312 
313  //- Check IOstream status for given operation
314  // print IOstream state if error has occurred
315  virtual bool check(const char* operation) const;
316 
317  //- Check IOstream status for given operation
318  // print IOstream state if error has occurred and exit
319  void fatalCheck(const char* operation) const;
320 
321  //- Return true if stream has been opened
322  bool opened() const
323  {
324  return openClosed_ == OPENED;
325  }
326 
327  //- Return true if stream is closed
328  bool closed() const
329  {
330  return openClosed_ == CLOSED;
331  }
332 
333  //- Return true if next operation might succeed
334  bool good() const
335  {
336  return ioState_ == 0;
337  }
338 
339  //- Return true if end of input seen
340  bool eof() const
341  {
342  return ioState_ & ios_base::eofbit;
343  }
344 
345  //- Return true if next operation will fail
346  bool fail() const
347  {
348  return ioState_ & (ios_base::badbit | ios_base::failbit);
349  }
350 
351  //- Return true if stream is corrupted
352  bool bad() const
353  {
354  return ioState_ & ios_base::badbit;
355  }
356 
357  //- Return non-zero if the stream has not failed
358  operator void*() const
359  {
360  return fail()
361  ? reinterpret_cast<void*>(0)
362  : reinterpret_cast<void*>(-1);
363  }
364 
365  //- Return true if the stream has failed
366  bool operator!() const
367  {
368  return fail();
369  }
370 
371 
372  // Stream state functions
373 
374  //- Return stream format of given format name
375  static streamFormat formatEnum(const word&);
376 
377  //- Return current stream format
378  streamFormat format() const
379  {
380  return format_;
381  }
382 
383  //- Set the stream format
384  streamFormat format(const streamFormat fmt)
385  {
386  streamFormat fmt0 = format_;
387  format_ = fmt;
388  return fmt0;
389  }
390 
391  //- Set the stream format from word
392  streamFormat format(const word& fmt)
393  {
394  streamFormat fmt0 = format_;
395  format_ = formatEnum(fmt);
396  return fmt0;
397  }
398 
399  //- Return the stream version
400  versionNumber version() const
401  {
402  return version_;
403  }
404 
405  //- Set the stream version
407  {
408  versionNumber ver0 = version_;
409  version_ = ver;
410  return ver0;
411  }
412 
413  //- Return compression of given compression name
414  static compressionType compressionEnum(const word&);
415 
416  //- Return the stream compression
418  {
419  return compression_;
420  }
421 
422  //- Set the stream compression
424  {
425  compressionType cmp0 = compression_;
426  compression_ = cmp;
427  return cmp0;
428  }
429 
430  //- Set the stream compression from word
431  compressionType compression(const word& cmp)
432  {
433  compressionType cmp0 = compression_;
434  compression_ = compressionEnum(cmp);
435  return cmp0;
436  }
437 
438  //- Return current stream line number
439  label lineNumber() const
440  {
441  return lineNumber_;
442  }
443 
444  //- Return current stream line number
445  label& lineNumber()
446  {
447  return lineNumber_;
448  }
449 
450  //- Set the stream line number
451  label lineNumber(const label ln)
452  {
453  label ln0 = lineNumber_;
454  lineNumber_ = ln;
455  return ln0;
456  }
457 
458  //- Return flags of stream
459  virtual ios_base::fmtflags flags() const = 0;
460 
461  //- Return the default precision
462  static unsigned int defaultPrecision()
463  {
464  return precision_;
465  }
466 
467  //- Reset the default precision (and return old precision)
468  static unsigned int defaultPrecision(unsigned int p)
469  {
470  unsigned int precision0 = precision_;
471  precision_ = p;
472  return precision0;
473  }
474 
475  //- Set stream to have reached eof
476  void setEof()
477  {
478  ioState_ |= ios_base::eofbit;
479  }
480 
481  //- Set stream to have failed
482  void setFail()
483  {
484  ioState_ |= ios_base::failbit;
485  }
486 
487  //- Set stream to be bad
488  void setBad()
489  {
490  ioState_ |= ios_base::badbit;
491  }
492 
493  //- Set flags of stream
494  virtual ios_base::fmtflags flags(const ios_base::fmtflags f) = 0;
495 
496  //- Set flags of stream
497  ios_base::fmtflags setf(const ios_base::fmtflags f)
498  {
499  return flags(flags() | f);
500  }
501 
502  //- Set flags of given field of stream
503  ios_base::fmtflags setf
504  (
505  const ios_base::fmtflags f,
506  const ios_base::fmtflags mask
507  )
508  {
509  return flags((flags() & ~mask) | (f & mask));
510  }
511 
512  //- Unset flags of stream
513  void unsetf(const ios_base::fmtflags uf)
514  {
515  flags(flags()&~uf);
516  }
517 
518 
519  // Print
520 
521  //- Print description of IOstream to Ostream
522  virtual void print(Ostream&) const;
523 
524  //- Check given stream state bits
525  void print(Ostream&, const int streamState) const;
526 
527 
528  // Info
529 
530  //- Return info proxy.
531  // Used to print IOstream information to a stream
532  InfoProxy<IOstream> info() const
533  {
534  return *this;
535  }
536 };
537 
538 
541 
542 template<>
543 Ostream& operator<<(Ostream& os, const InfoProxy<IOstream>& ip);
544 
545 
546 // --------------------------------------------------------------------
547 // ------ Manipulators (not taking arguments)
548 // --------------------------------------------------------------------
550 typedef IOstream& (*IOstreamManip)(IOstream&);
551 
552 //- operator<< handling for manipulators without arguments
554 {
555  return f(io);
556 }
557 
559 inline IOstream& dec(IOstream& io)
560 {
562  return io;
563 }
565 inline IOstream& hex(IOstream& io)
566 {
568  return io;
569 }
571 inline IOstream& oct(IOstream& io)
572 {
574  return io;
575 }
577 inline IOstream& fixed(IOstream& io)
578 {
579  io.setf(ios_base::fixed, ios_base::floatfield);
580  return io;
581 }
583 inline IOstream& scientific(IOstream& io)
584 {
585  io.setf(ios_base::scientific, ios_base::floatfield);
586  return io;
587 }
588 
589 
590 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
591 
592 } // End namespace Foam
593 
594 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
595 
596 #endif
597 
598 // ************************************************************************* //
label lineNumber_
Definition: IOstream.H:231
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
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
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:512
void setClosed()
Set stream closed.
Definition: IOstream.H:245
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:351
static const versionNumber originalVersion
Original version number.
Definition: IOstream.H:203
ios_base::fmtflags setf(const ios_base::fmtflags f)
Set flags of stream.
Definition: IOstream.H:496
IOstream & hex(IOstream &io)
Definition: IOstream.H:564
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:257
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.H:461
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
streamAccess
Enumeration for whether the stream open or closed.
Definition: IOstream.H:79
versionNumber version() const
Return the stream version.
Definition: IOstream.H:399
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:438
bool operator>(const versionNumber &vn)
Is this version newer than the one given.
Definition: IOstream.H:175
IOstream & fixed(IOstream &io)
Definition: IOstream.H:576
void setFail()
Set stream to have failed.
Definition: IOstream.H:481
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
int minorVersion() const
Return minor version.
Definition: IOstream.H:139
IOstream & oct(IOstream &io)
Definition: IOstream.H:570
virtual ~IOstream()
Destructor.
Definition: IOstream.H:287
A character and a pointer to a character string.
void setBad()
Set stream to be bad.
Definition: IOstream.H:487
bool operator==(const versionNumber &vn)
Are these versionNumbers the same?
Definition: IOstream.H:151
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:297
bool operator>=(const versionNumber &vn)
This version the same as or newer than the one given.
Definition: IOstream.H:181
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:68
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
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:339
bool ln(const fileName &src, const fileName &dst)
Create a softlink. dst should not exist. Returns true if successful.
Definition: POSIX.C:912
bool operator!=(const versionNumber &vn)
Are these versionNumbers different?
Definition: IOstream.H:157
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:53
bool opened() const
Return true if stream has been opened.
Definition: IOstream.H:321
labelList f(nPoints)
volScalarField sf(fieldObject, mesh)
bool operator<(const versionNumber &vn)
Is this version older than the one given.
Definition: IOstream.H:163
bool fail() const
Return true if next operation will fail.
Definition: IOstream.H:345
compressionType compression() const
Return the stream compression.
Definition: IOstream.H:416
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:209
void setEof()
Set stream to have reached eof.
Definition: IOstream.H:475
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:239
static const versionNumber currentVersion
Current version number.
Definition: IOstream.H:206
string str() const
Return the versionNumber as a character string.
Definition: IOstream.C:116
Version number type.
Definition: IOstream.H:96
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:558
void setState(ios_base::iostate state)
Set stream state.
Definition: IOstream.H:251
volScalarField & p
virtual void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: IOstream.C:126
bool operator<=(const versionNumber &vn)
Is this version the same as or older than the one given.
Definition: IOstream.H:169
bool operator!() const
Return true if the stream has failed.
Definition: IOstream.H:365
IOstream &(* IOstreamManip)(IOstream &)
Definition: IOstream.H:549
InfoProxy< IOstream > info() const
Return info proxy.
Definition: IOstream.H:531
System bool.
IOstream & scientific(IOstream &io)
Definition: IOstream.H:582
Namespace for OpenFOAM.
bool closed() const
Return true if stream is closed.
Definition: IOstream.H:327
IOstream(streamFormat format, versionNumber version, compressionType compression=UNCOMPRESSED)
Construct setting format and version.
Definition: IOstream.H:269