1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation, either version 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
28 An IOstream is an abstract base class for all input/output systems; be
29 they streams, files, token lists etc.
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.
40 \*---------------------------------------------------------------------------*/
51 #include "InfoProxy.H"
52 #include "infoSwitch.H"
71 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
76 /*---------------------------------------------------------------------------*\
77 Class IOstream Declaration
78 \*---------------------------------------------------------------------------*/
87 //- Enumeration for whether the stream open or closed
94 //- Enumeration for the format of data in the stream
102 friend Ostream& operator<<(Ostream& os, const streamFormat& sf);
104 //- Version number type
107 //- The version number
108 scalar versionNumber_;
110 //- The version number as an integer
118 //- Construct from number
119 versionNumber(const scalar num)
122 index_(numberToIndex(num))
125 //- Construct from Istream
126 versionNumber(Istream& is)
128 versionNumber_(readScalar(is)),
129 index_(numberToIndex(versionNumber_))
135 //- Convert a version number into an index
136 int numberToIndex(const scalar num) const
138 return int(10*num + SMALL);
141 //- Return major version
142 int majorVersion() const
144 return int(versionNumber_);
147 //- Return minor version
148 int minorVersion() const
150 return int(10.0*(versionNumber_ - majorVersion()));
153 //- Return the versionNumber as a character string
159 //- Are these versionNumbers the same?
160 bool operator==(const versionNumber& vn)
162 return index_ == vn.index_;
165 //- Are these versionNumbers different?
166 bool operator!=(const versionNumber& vn)
168 return index_ != vn.index_;
171 //- Is this version older than the one given
172 bool operator<(const versionNumber& vn)
174 return index_ < vn.index_;
177 //- Is this version the same as or older than the one given
178 bool operator<=(const versionNumber& vn)
180 return index_ <= vn.index_;
183 //- Is this version newer than the one given
184 bool operator>(const versionNumber& vn)
186 return index_ > vn.index_;
189 //- this version the same as or newer than the one given
190 bool operator>=(const versionNumber& vn)
192 return index_ >= vn.index_;
197 friend Ostream& operator<<(Ostream& os, const versionNumber& vn);
201 //- Enumeration for the format of data in the stream
209 // Public static data
211 //- Original version number
212 static const versionNumber originalVersion;
214 //- Current version number
215 static const versionNumber currentVersion;
217 //- Default precision
218 static Foam::debug::infoSwitch precision_;
224 //- Name of the stream
225 static fileName name_;
227 streamFormat format_;
228 versionNumber version_;
229 compressionType compression_;
231 streamAccess openClosed_;
232 ios_base::iostate ioState_;
242 // Protected member functions
246 //- Set stream opened
249 openClosed_ = OPENED;
252 //- Set stream closed
255 openClosed_ = CLOSED;
259 void setState(ios_base::iostate state)
264 //- Set stream to be good
267 ioState_ = ios_base::iostate(0);
275 //- Construct setting format and version
279 versionNumber version,
280 compressionType compression=UNCOMPRESSED
285 compression_(compression),
287 ioState_(ios_base::iostate(0)),
304 //- Return the name of the stream
305 // Useful for Fstream to return the filename
306 virtual const fileName& name() const
311 //- Return non-const access to the name of the stream
312 // Useful to alter the stream name
313 virtual fileName& name()
321 //- Check IOstream status for given operation
322 // print IOstream state if error has occured
323 virtual bool check(const char* operation) const;
325 //- Check IOstream status for given operation
326 // print IOstream state if error has occured and exit
327 void fatalCheck(const char* operation) const;
329 //- Return true if stream has been opened
332 return openClosed_ == OPENED;
335 //- Return true if stream is closed
338 return openClosed_ == CLOSED;
341 //- Return true if next operation might succeed
344 return ioState_ == 0;
347 //- Return true if end of input seen
350 return ioState_ & ios_base::eofbit;
353 //- Return true if next operation will fail
356 return ioState_ & (ios_base::badbit | ios_base::failbit);
359 //- Return true if stream is corrupted
362 return ioState_ & ios_base::badbit;
365 //- Return non-zero if the stream has not failed
366 operator void*() const
369 ? reinterpret_cast<void*>(0)
370 : reinterpret_cast<void*>(-1);
373 //- Return true if the stream has failed
374 bool operator!() const
380 // Stream state functions
382 //- Return stream format of given format name
383 static streamFormat formatEnum(const word&);
385 //- Return current stream format
386 streamFormat format() const
391 //- Set the stream format
392 streamFormat format(const streamFormat fmt)
394 streamFormat fmt0 = format_;
399 //- Set the stream format from word
400 streamFormat format(const word& fmt)
402 streamFormat fmt0 = format_;
403 format_ = formatEnum(fmt);
407 //- Return the stream version
408 versionNumber version() const
413 //- Set the stream version
414 versionNumber version(const versionNumber ver)
416 versionNumber ver0 = version_;
421 //- Return compression of given compression name
422 static compressionType compressionEnum(const word&);
424 //- Return the stream compression
425 compressionType compression() const
430 //- Set the stream compression
431 compressionType compression(const compressionType cmp)
433 compressionType cmp0 = compression_;
438 //- Set the stream compression from word
439 compressionType compression(const word& cmp)
441 compressionType cmp0 = compression_;
442 compression_ = compressionEnum(cmp);
446 //- Return current stream line number
447 label lineNumber() const
452 //- Return current stream line number
458 //- Set the stream line number
459 label lineNumber(const label ln)
461 label ln0 = lineNumber_;
466 //- Return flags of stream
467 virtual ios_base::fmtflags flags() const = 0;
469 //- Return the default precision
470 static unsigned int defaultPrecision()
475 //- Reset the default precision (and return old precision)
476 static unsigned int defaultPrecision(unsigned int p)
478 unsigned int precision0 = precision_();
483 //- Set stream to have reached eof
486 ioState_ |= ios_base::eofbit;
489 //- Set stream to have failed
492 ioState_ |= ios_base::failbit;
495 //- Set stream to be bad
498 ioState_ |= ios_base::badbit;
501 //- Set flags of stream
502 virtual ios_base::fmtflags flags(const ios_base::fmtflags f) = 0;
504 //- Set flags of stream
505 ios_base::fmtflags setf(const ios_base::fmtflags f)
507 return flags(flags() | f);
510 //- Set flags of given field of stream
511 ios_base::fmtflags setf
513 const ios_base::fmtflags f,
514 const ios_base::fmtflags mask
517 return flags((flags() & ~mask) | (f & mask));
520 //- Unset flags of stream
521 void unsetf(const ios_base::fmtflags uf)
529 //- Print description of IOstream to Ostream
530 virtual void print(Ostream&) const;
532 //- Check given stream state bits
533 void print(Ostream&, const int streamState) const;
538 //- Return info proxy.
539 // Used to print IOstream information to a stream
540 InfoProxy<IOstream> info() const
547 Ostream& operator<<(Ostream& os, const IOstream::streamFormat& sf);
548 Ostream& operator<<(Ostream& os, const IOstream::versionNumber& vn);
551 // --------------------------------------------------------------------
552 // ------ Manipulators (not taking arguments)
553 // --------------------------------------------------------------------
555 typedef IOstream& (*IOstreamManip)(IOstream&);
557 //- operator<< handling for manipulators without arguments
558 inline IOstream& operator<<(IOstream& io, IOstreamManip f)
564 inline IOstream& dec(IOstream& io)
566 io.setf(ios_base::dec, ios_base::dec|ios_base::hex|ios_base::oct);
570 inline IOstream& hex(IOstream& io)
572 io.setf(ios_base::hex, ios_base::dec|ios_base::hex|ios_base::oct);
576 inline IOstream& oct(IOstream& io)
578 io.setf(ios_base::oct, ios_base::dec|ios_base::hex|ios_base::oct);
582 inline IOstream& fixed(IOstream& io)
584 io.setf(ios_base::fixed, ios_base::floatfield);
588 inline IOstream& scientific(IOstream& io)
590 io.setf(ios_base::scientific, ios_base::floatfield);
595 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
597 } // End namespace Foam
599 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
603 // ************************************************************************* //