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  {
82  OPENED,
84  };
85 
86  //- Enumeration for the format of data in the stream
87  enum streamFormat
88  {
89  ASCII,
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  {
196  UNCOMPRESSED,
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
228 
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
266  IOstream
267  (
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 
536 Ostream& operator<<(Ostream& os, const IOstream::streamFormat& sf);
537 Ostream& operator<<(Ostream& os, const IOstream::versionNumber& vn);
538 
539 template<>
540 Ostream& operator<<(Ostream& os, const InfoProxy<IOstream>& ip);
541 
542 
543 // --------------------------------------------------------------------
544 // ------ Manipulators (not taking arguments)
545 // --------------------------------------------------------------------
546 
547 typedef IOstream& (*IOstreamManip)(IOstream&);
548 
549 //- operator<< handling for manipulators without arguments
551 {
552  return f(io);
553 }
554 
555 
556 inline IOstream& dec(IOstream& io)
557 {
559  return io;
560 }
561 
562 inline IOstream& hex(IOstream& io)
563 {
565  return io;
566 }
567 
568 inline IOstream& oct(IOstream& io)
569 {
571  return io;
572 }
573 
574 inline IOstream& fixed(IOstream& io)
575 {
576  io.setf(ios_base::fixed, ios_base::floatfield);
577  return io;
578 }
579 
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 // ************************************************************************* //
System bool.
A character and a pointer to a character string.
Version number type.
Definition: IOstream.H:97
friend Ostream & operator<<(Ostream &os, const versionNumber &vn)
Ostream operator.
bool operator<=(const versionNumber &vn) const
Is this version the same as or older than the one given.
Definition: IOstream.H:169
bool operator>(const versionNumber &vn) const
Is this version newer than the one given.
Definition: IOstream.H:175
string str() const
Return the versionNumber as a character string.
Definition: IOstream.C:116
int minorVersion() const
Return minor version.
Definition: IOstream.H:139
bool operator!=(const versionNumber &vn) const
Are these versionNumbers different?
Definition: IOstream.H:157
bool operator>=(const versionNumber &vn) const
This version the same as or newer than the one given.
Definition: IOstream.H:181
int majorVersion() const
Return major version.
Definition: IOstream.H:133
bool operator==(const versionNumber &vn) const
Are these versionNumbers the same?
Definition: IOstream.H:151
int numberToIndex(const scalar num) const
Convert a version number into an index.
Definition: IOstream.H:127
bool operator<(const versionNumber &vn) const
Is this version older than the one given.
Definition: IOstream.H:163
versionNumber(const scalar num)
Construct from number.
Definition: IOstream.H:110
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:72
friend Ostream & operator<<(Ostream &os, const streamFormat &sf)
Ostream operator.
bool opened() const
Return true if stream has been opened.
Definition: IOstream.H:318
void setEof()
Set stream to have reached eof.
Definition: IOstream.H:472
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:435
static const versionNumber currentVersion
Current version number.
Definition: IOstream.H:203
streamFormat format() const
Return current stream format.
Definition: IOstream.H:374
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:294
static unsigned int precision_
Default precision.
Definition: IOstream.H:206
void setGood()
Set stream to be good.
Definition: IOstream.H:254
bool fail() const
Return true if next operation will fail.
Definition: IOstream.H:342
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:105
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:87
bool operator!() const
Return true if the stream has failed.
Definition: IOstream.H:362
void setBad()
Set stream to be bad.
Definition: IOstream.H:484
label lineNumber_
Definition: IOstream.H:228
void setState(ios_base::iostate state)
Set stream state.
Definition: IOstream.H:248
void setClosed()
Set stream closed.
Definition: IOstream.H:242
ios_base::fmtflags setf(const ios_base::fmtflags f)
Set flags of stream.
Definition: IOstream.H:493
versionNumber version() const
Return the stream version.
Definition: IOstream.H:396
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.H:458
static streamFormat formatEnum(const word &)
Return stream format of given format name.
Definition: IOstream.C:39
void unsetf(const ios_base::fmtflags uf)
Unset flags of stream.
Definition: IOstream.H:509
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:348
compressionType compression() const
Return the stream compression.
Definition: IOstream.H:413
virtual void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: IOstream.C:126
virtual ~IOstream()
Destructor.
Definition: IOstream.H:284
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:194
virtual ios_base::fmtflags flags() const =0
Return flags of stream.
void setOpened()
Set stream opened.
Definition: IOstream.H:236
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
streamAccess
Enumeration for whether the stream open or closed.
Definition: IOstream.H:80
InfoProxy< IOstream > info() const
Return info proxy.
Definition: IOstream.H:528
void setFail()
Set stream to have failed.
Definition: IOstream.H:478
bool closed() const
Return true if stream is closed.
Definition: IOstream.H:324
static compressionType compressionEnum(const word &)
Return compression of given compression name.
Definition: IOstream.C:61
bool eof() const
Return true if end of input seen.
Definition: IOstream.H:336
IOstream(streamFormat format, versionNumber version, compressionType compression=UNCOMPRESSED)
Construct setting format and version.
Definition: IOstream.H:266
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:50
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A class for handling file names.
Definition: fileName.H:82
A class for handling words, derived from string.
Definition: word.H:62
volScalarField sf(fieldObject, mesh)
Namespace for OpenFOAM.
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
IOstream & fixed(IOstream &io)
Definition: IOstream.H:573
IOstream & dec(IOstream &io)
Definition: IOstream.H:555
IOstream & hex(IOstream &io)
Definition: IOstream.H:561
IOstream &(* IOstreamManip)(IOstream &)
Definition: IOstream.H:546
IOstream & scientific(IOstream &io)
Definition: IOstream.H:579
bool readScalar(const char *buf, doubleScalar &s)
Read whole of buf as a scalar. Return true if successful.
Definition: doubleScalar.H:75
Ostream & operator<<(Ostream &, const ensightPart &)
bool ln(const fileName &src, const fileName &dst)
Create a softlink. dst should not exist. Returns true if successful.
Definition: POSIX.C:908
IOstream & oct(IOstream &io)
Definition: IOstream.H:567
labelList f(nPoints)
volScalarField & p