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-2024 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  bool global_;
221 
222  streamAccess openClosed_;
223  ios_base::iostate ioState_;
224 
225 
226 protected:
227 
228  // Protected data
229 
231 
232 
233  // Protected Member Functions
234 
235  // Access
236 
237  //- Set stream opened
238  void setOpened()
239  {
240  openClosed_ = OPENED;
241  }
242 
243  //- Set stream closed
244  void setClosed()
245  {
246  openClosed_ = CLOSED;
247  }
248 
249  //- Set stream state
250  void setState(ios_base::iostate state)
251  {
252  ioState_ = state;
253  }
254 
255  //- Set stream to be good
256  void setGood()
257  {
258  ioState_ = ios_base::iostate(0);
259  }
260 
261 
262 public:
263 
264  // Constructors
265 
266  //- Construct setting format and version
267  IOstream
268  (
269  const streamFormat format,
270  const versionNumber version,
272  const bool global = false
273  )
274  :
275  format_(format),
276  version_(version),
277  compression_(compression),
278  global_(global),
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 global state
439  bool global() const
440  {
441  return global_;
442  }
443 
444  //- Return global state
445  bool& global()
446  {
447  return global_;
448  }
449 
450  //- Return current stream line number
451  label lineNumber() const
452  {
453  return lineNumber_;
454  }
455 
456  //- Return current stream line number
457  label& lineNumber()
458  {
459  return lineNumber_;
460  }
461 
462  //- Set the stream line number
463  label lineNumber(const label ln)
464  {
465  label ln0 = lineNumber_;
466  lineNumber_ = ln;
467  return ln0;
468  }
469 
470  //- Return flags of stream
471  virtual ios_base::fmtflags flags() const = 0;
472 
473  //- Return the default precision
474  static unsigned int defaultPrecision()
475  {
476  return precision_;
477  }
478 
479  //- Reset the default precision (and return old precision)
480  static unsigned int defaultPrecision(unsigned int p)
481  {
482  unsigned int precision0 = precision_;
483  precision_ = p;
484  return precision0;
485  }
486 
487  //- Set stream to have reached eof
488  void setEof()
489  {
490  ioState_ |= ios_base::eofbit;
491  }
492 
493  //- Set stream to have failed
494  void setFail()
495  {
496  ioState_ |= ios_base::failbit;
497  }
498 
499  //- Set stream to be bad
500  void setBad()
501  {
502  ioState_ |= ios_base::badbit;
503  }
504 
505  //- Set flags of stream
506  virtual ios_base::fmtflags flags(const ios_base::fmtflags f) = 0;
507 
508  //- Set flags of stream
509  ios_base::fmtflags setf(const ios_base::fmtflags f)
510  {
511  return flags(flags() | f);
512  }
513 
514  //- Set flags of given field of stream
515  ios_base::fmtflags setf
516  (
517  const ios_base::fmtflags f,
518  const ios_base::fmtflags mask
519  )
520  {
521  return flags((flags() & ~mask) | (f & mask));
522  }
523 
524  //- Unset flags of stream
525  void unsetf(const ios_base::fmtflags uf)
526  {
527  flags(flags()&~uf);
528  }
529 
530 
531  // Print
532 
533  //- Print description of IOstream to Ostream
534  virtual void print(Ostream&) const;
535 
536  //- Check given stream state bits
537  void print(Ostream&, const int streamState) const;
538 
539 
540  // Info
541 
542  //- Return info proxy.
543  // Used to print IOstream information to a stream
544  InfoProxy<IOstream> info() const
545  {
546  return *this;
547  }
548 };
549 
550 
551 Ostream& operator<<(Ostream& os, const IOstream::streamFormat& sf);
552 Ostream& operator<<(Ostream& os, const IOstream::versionNumber& vn);
553 
554 template<>
555 Ostream& operator<<(Ostream& os, const InfoProxy<IOstream>& ip);
556 
557 
558 // --------------------------------------------------------------------
559 // ------ Manipulators (not taking arguments)
560 // --------------------------------------------------------------------
561 
562 typedef IOstream& (*IOstreamManip)(IOstream&);
563 
564 //- operator<< handling for manipulators without arguments
566 {
567  return f(io);
568 }
569 
570 
571 inline IOstream& dec(IOstream& io)
572 {
574  return io;
575 }
576 
577 inline IOstream& hex(IOstream& io)
578 {
580  return io;
581 }
582 
583 inline IOstream& oct(IOstream& io)
584 {
586  return io;
587 }
588 
589 inline IOstream& fixed(IOstream& io)
590 {
591  io.setf(ios_base::fixed, ios_base::floatfield);
592  return io;
593 }
594 
595 inline IOstream& scientific(IOstream& io)
596 {
597  io.setf(ios_base::scientific, ios_base::floatfield);
598  return io;
599 }
600 
601 
602 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
603 
604 } // End namespace Foam
605 
606 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
607 
608 #endif
609 
610 // ************************************************************************* //
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:321
void setEof()
Set stream to have reached eof.
Definition: IOstream.H:487
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:450
static const versionNumber currentVersion
Current version number.
Definition: IOstream.H:203
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
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:297
static unsigned int precision_
Default precision.
Definition: IOstream.H:206
void setGood()
Set stream to be good.
Definition: IOstream.H:255
bool fail() const
Return true if next operation will fail.
Definition: IOstream.H:345
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:105
bool global() const
Return global state.
Definition: IOstream.H:438
IOstream(const streamFormat format, const versionNumber version, const compressionType compression=UNCOMPRESSED, const bool global=false)
Construct setting format and version.
Definition: IOstream.H:267
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:365
void setBad()
Set stream to be bad.
Definition: IOstream.H:499
label lineNumber_
Definition: IOstream.H:229
void setState(ios_base::iostate state)
Set stream state.
Definition: IOstream.H:249
void setClosed()
Set stream closed.
Definition: IOstream.H:243
ios_base::fmtflags setf(const ios_base::fmtflags f)
Set flags of stream.
Definition: IOstream.H:508
versionNumber version() const
Return the stream version.
Definition: IOstream.H:399
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.H:473
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:524
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:351
compressionType compression() const
Return the stream compression.
Definition: IOstream.H:416
virtual void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: IOstream.C:126
virtual ~IOstream()
Destructor.
Definition: IOstream.H:287
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:237
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
streamAccess
Enumeration for whether the stream open or closed.
Definition: IOstream.H:80
InfoProxy< IOstream > info() const
Return info proxy.
Definition: IOstream.H:543
void setFail()
Set stream to have failed.
Definition: IOstream.H:493
bool closed() const
Return true if stream is closed.
Definition: IOstream.H:327
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:339
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:588
IOstream & dec(IOstream &io)
Definition: IOstream.H:570
IOstream & hex(IOstream &io)
Definition: IOstream.H:576
IOstream &(* IOstreamManip)(IOstream &)
Definition: IOstream.H:561
IOstream & scientific(IOstream &io)
Definition: IOstream.H:594
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 &os, const fvConstraints &constraints)
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:582
labelList f(nPoints)
volScalarField & p