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