FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

IOstream.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2010 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software: you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
00023 
00024 Class
00025     Foam::IOstream
00026 
00027 Description
00028     An IOstream is an abstract base class for all input/output systems; be
00029     they streams, files, token lists etc.
00030 
00031     The basic operations are construct, close, read token, read primitive
00032     and read binary block.  In addition version control and line number
00033     counting is incorporated.  Usually one would use the read primitive
00034     member functions, but if one were reading a stream on unknown data
00035     sequence one can read token by token, and then analyse.
00036 
00037 SourceFiles
00038     IOstream.C
00039 
00040 \*---------------------------------------------------------------------------*/
00041 
00042 #ifndef IOstream_H
00043 #define IOstream_H
00044 
00045 #include <OpenFOAM/char.H>
00046 #include <OpenFOAM/bool.H>
00047 #include <OpenFOAM/label.H>
00048 #include <OpenFOAM/uLabel.H>
00049 #include <OpenFOAM/scalar.H>
00050 #include <OpenFOAM/fileName.H>
00051 #include "InfoProxy.H"
00052 
00053 #include <iostream>
00054 
00055 #if __GNUC__ < 3
00056 #   define ios_base ios
00057 #endif
00058 
00059 using std::ios_base;
00060 using std::istream;
00061 using std::ostream;
00062 
00063 using std::cin;
00064 using std::cout;
00065 using std::cerr;
00066 
00067 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00068 
00069 namespace Foam
00070 {
00071 
00072 /*---------------------------------------------------------------------------*\
00073                            Class IOstream Declaration
00074 \*---------------------------------------------------------------------------*/
00075 
00076 class IOstream
00077 {
00078 
00079 public:
00080 
00081     // Public data types
00082 
00083         //- Enumeration for whether the stream open or closed
00084         enum streamAccess
00085         {
00086             OPENED,
00087             CLOSED
00088         };
00089 
00090         //- Enumeration for the format of data in the stream
00091         enum streamFormat
00092         {
00093             ASCII,
00094             BINARY
00095         };
00096 
00097         //- Ostream operator
00098         friend Ostream& operator<<(Ostream& os, const streamFormat& sf);
00099 
00100         //- Version number type
00101         class versionNumber
00102         {
00103             //- The version number
00104             scalar versionNumber_;
00105 
00106             //- The version number as an integer
00107             int index_;
00108 
00109 
00110         public:
00111 
00112             // Constructors
00113 
00114                 //- Construct from number
00115                 versionNumber(const scalar num)
00116                 :
00117                     versionNumber_(num),
00118                     index_(numberToIndex(num))
00119                 {}
00120 
00121                 //- Construct from Istream
00122                 versionNumber(Istream& is)
00123                 :
00124                     versionNumber_(readScalar(is)),
00125                     index_(numberToIndex(versionNumber_))
00126                 {}
00127 
00128 
00129             // Member functions
00130 
00131                 //- Convert a version number into an index
00132                 int numberToIndex(const scalar num) const
00133                 {
00134                     return int(10*num + SMALL);
00135                 }
00136 
00137                 //- Return major version
00138                 int majorVersion() const
00139                 {
00140                     return int(versionNumber_);
00141                 }
00142 
00143                 //- Return minor version
00144                 int minorVersion() const
00145                 {
00146                     return int(10.0*(versionNumber_ - majorVersion()));
00147                 }
00148 
00149                 //- Return the versionNumber as a character string
00150                 string str() const;
00151 
00152 
00153             // Member operators
00154 
00155                 //- Are these versionNumbers the same?
00156                 bool operator==(const versionNumber& vn)
00157                 {
00158                     return index_ == vn.index_;
00159                 }
00160 
00161                 //- Are these versionNumbers different?
00162                 bool operator!=(const versionNumber& vn)
00163                 {
00164                     return index_ != vn.index_;
00165                 }
00166 
00167                 //- Is this version older than the one given
00168                 bool operator<(const versionNumber& vn)
00169                 {
00170                     return index_ < vn.index_;
00171                 }
00172 
00173                 //- Is this version the same as or older than the one given
00174                 bool operator<=(const versionNumber& vn)
00175                 {
00176                     return index_ <= vn.index_;
00177                 }
00178 
00179                 //- Is this version newer than the one given
00180                 bool operator>(const versionNumber& vn)
00181                 {
00182                     return index_ > vn.index_;
00183                 }
00184 
00185                 //- this version the same as or newer than the one given
00186                 bool operator>=(const versionNumber& vn)
00187                 {
00188                     return index_ >= vn.index_;
00189                 }
00190 
00191 
00192             //- Ostream operator
00193             friend Ostream& operator<<(Ostream& os, const versionNumber& vn);
00194         };
00195 
00196 
00197         //- Enumeration for the format of data in the stream
00198         enum compressionType
00199         {
00200             UNCOMPRESSED,
00201             COMPRESSED
00202         };
00203 
00204 
00205     // Public static data
00206 
00207         //- Original version number
00208         static const versionNumber originalVersion;
00209 
00210         //- Current version number
00211         static const versionNumber currentVersion;
00212 
00213         //- Default precision
00214         static unsigned int precision_;
00215 
00216 
00217 private:
00218 
00219     // Private data
00220 
00221         //- Name of the stream
00222         static fileName name_;
00223 
00224         streamFormat format_;
00225         versionNumber version_;
00226         compressionType compression_;
00227 
00228         streamAccess openClosed_;
00229         ios_base::iostate ioState_;
00230 
00231 
00232 protected:
00233 
00234     // Protected data
00235 
00236         label lineNumber_;
00237 
00238 
00239     // Protected member functions
00240 
00241         // Access
00242 
00243             //- Set stream opened
00244             void setOpened()
00245             {
00246                 openClosed_ = OPENED;
00247             }
00248 
00249             //- Set stream closed
00250             void setClosed()
00251             {
00252                 openClosed_ = CLOSED;
00253             }
00254 
00255             //- Set stream state
00256             void setState(ios_base::iostate state)
00257             {
00258                 ioState_ = state;
00259             }
00260 
00261             //- Set stream to be good
00262             void setGood()
00263             {
00264                 ioState_ = ios_base::iostate(0);
00265             }
00266 
00267 
00268 public:
00269 
00270     // Constructors
00271 
00272         //- Construct setting format and version
00273         IOstream
00274         (
00275             streamFormat format,
00276             versionNumber version,
00277             compressionType compression=UNCOMPRESSED
00278         )
00279         :
00280             format_(format),
00281             version_(version),
00282             compression_(compression),
00283             openClosed_(CLOSED),
00284             ioState_(ios_base::iostate(0)),
00285             lineNumber_(0)
00286         {
00287             setBad();
00288         }
00289 
00290 
00291     // Destructor
00292 
00293         virtual ~IOstream()
00294         {}
00295 
00296 
00297     // Member functions
00298 
00299         // Access
00300 
00301             //- Return the name of the stream
00302             //  Useful for Fstream to return the filename
00303             virtual const fileName& name() const
00304             {
00305                 return name_;
00306             }
00307 
00308             //- Return non-const access to the name of the stream
00309             //  Useful to alter the stream name
00310             virtual fileName& name()
00311             {
00312                 return name_;
00313             }
00314 
00315 
00316         // Check
00317 
00318             //- Check IOstream status for given operation
00319             //  print IOstream state if error has occured
00320             virtual bool check(const char* operation) const;
00321 
00322             //- Check IOstream status for given operation
00323             //  print IOstream state if error has occured and exit
00324             void fatalCheck(const char* operation) const;
00325 
00326             //- Return true if stream has been opened
00327             bool opened() const
00328             {
00329                 return openClosed_ == OPENED;
00330             }
00331 
00332             //- Return true if stream is closed
00333             bool closed() const
00334             {
00335                 return openClosed_ == CLOSED;
00336             }
00337 
00338             //- Return true if next operation might succeed
00339             bool good() const
00340             {
00341                 return ioState_ == 0;
00342             }
00343 
00344             //- Return true if end of input seen
00345             bool eof() const
00346             {
00347                 return ioState_ & ios_base::eofbit;
00348             }
00349 
00350             //- Return true if next operation will fail
00351             bool fail() const
00352             {
00353                 return ioState_ & (ios_base::badbit | ios_base::failbit);
00354             }
00355 
00356             //- Return true if stream is corrupted
00357             bool bad() const
00358             {
00359                 return ioState_ & ios_base::badbit;
00360             }
00361 
00362             //- Return non-zero if the stream has not failed
00363             operator void*() const
00364             {
00365                 return fail()
00366                     ? reinterpret_cast<void*>(0)
00367                     : reinterpret_cast<void*>(-1);
00368             }
00369 
00370             //- Return true if the stream has failed
00371             bool operator!() const
00372             {
00373                 return fail();
00374             }
00375 
00376 
00377         // Stream state functions
00378 
00379             //- Return stream format of given format name
00380             static streamFormat formatEnum(const word&);
00381 
00382             //- Return current stream format
00383             streamFormat format() const
00384             {
00385                 return format_;
00386             }
00387 
00388             //- Set the stream format
00389             streamFormat format(const streamFormat fmt)
00390             {
00391                 streamFormat fmt0 = format_;
00392                 format_ = fmt;
00393                 return fmt0;
00394             }
00395 
00396             //- Set the stream format from word
00397             streamFormat format(const word& fmt)
00398             {
00399                 streamFormat fmt0 = format_;
00400                 format_ = formatEnum(fmt);
00401                 return fmt0;
00402             }
00403 
00404             //- Return the stream version
00405             versionNumber version() const
00406             {
00407                 return version_;
00408             }
00409 
00410             //- Set the stream version
00411             versionNumber version(const versionNumber ver)
00412             {
00413                 versionNumber ver0 = version_;
00414                 version_ = ver;
00415                 return ver0;
00416             }
00417 
00418             //- Return compression of given compression name
00419             static compressionType compressionEnum(const word&);
00420 
00421             //- Return the stream compression
00422             compressionType compression() const
00423             {
00424                 return compression_;
00425             }
00426 
00427             //- Set the stream compression
00428             compressionType compression(const compressionType cmp)
00429             {
00430                 compressionType cmp0 = compression_;
00431                 compression_ = cmp;
00432                 return cmp0;
00433             }
00434 
00435             //- Set the stream compression from word
00436             compressionType compression(const word& cmp)
00437             {
00438                 compressionType cmp0 = compression_;
00439                 compression_ = compressionEnum(cmp);
00440                 return cmp0;
00441             }
00442 
00443             //- Return current stream line number
00444             label lineNumber() const
00445             {
00446                 return lineNumber_;
00447             }
00448 
00449             //- Return current stream line number
00450             label& lineNumber()
00451             {
00452                 return lineNumber_;
00453             }
00454 
00455             //- Set the stream line number
00456             label lineNumber(const label ln)
00457             {
00458                 label ln0 = lineNumber_;
00459                 lineNumber_ = ln;
00460                 return ln0;
00461             }
00462 
00463             //- Return flags of stream
00464             virtual ios_base::fmtflags flags() const = 0;
00465 
00466             //- Return the default precision
00467             static unsigned int defaultPrecision()
00468             {
00469                 return precision_;
00470             }
00471 
00472             //- Reset the default precision (and return old precision)
00473             static unsigned int defaultPrecision(unsigned int p)
00474             {
00475                 unsigned int precision0 = precision_;
00476                 precision_ = p;
00477                 return precision0;
00478             }
00479 
00480             //- Set stream to have reached eof
00481             void setEof()
00482             {
00483                 ioState_ |= ios_base::eofbit;
00484             }
00485 
00486             //- Set stream to have failed
00487             void setFail()
00488             {
00489                 ioState_ |= ios_base::failbit;
00490             }
00491 
00492             //- Set stream to be bad
00493             void setBad()
00494             {
00495                 ioState_ |= ios_base::badbit;
00496             }
00497 
00498             //- Set flags of stream
00499             virtual ios_base::fmtflags flags(const ios_base::fmtflags f) = 0;
00500 
00501             //- Set flags of stream
00502             ios_base::fmtflags setf(const ios_base::fmtflags f)
00503             {
00504                 return flags(flags() | f);
00505             }
00506 
00507             //- Set flags of given field of stream
00508             ios_base::fmtflags setf
00509             (
00510                 const ios_base::fmtflags f,
00511                 const ios_base::fmtflags mask
00512             )
00513             {
00514                 return flags((flags() & ~mask) | (f & mask));
00515             }
00516 
00517             //- Unset flags of stream
00518             void unsetf(const ios_base::fmtflags uf)
00519             {
00520                 flags(flags()&~uf);
00521             }
00522 
00523 
00524         // Print
00525 
00526             //- Print description of IOstream to Ostream
00527             virtual void print(Ostream&) const;
00528 
00529             //- Check given stream state bits
00530             void print(Ostream&, const int streamState) const;
00531 
00532 
00533         // Info
00534 
00535             //- Return info proxy.
00536             //  Used to print IOstream information to a stream
00537             InfoProxy<IOstream> info() const
00538             {
00539                 return *this;
00540             }
00541 };
00542 
00543 
00544 Ostream& operator<<(Ostream& os, const IOstream::streamFormat& sf);
00545 Ostream& operator<<(Ostream& os, const IOstream::versionNumber& vn);
00546 
00547 
00548 // --------------------------------------------------------------------
00549 // ------ Manipulators (not taking arguments)
00550 // --------------------------------------------------------------------
00551 
00552 typedef IOstream& (*IOstreamManip)(IOstream&);
00553 
00554 //- operator<< handling for manipulators without arguments
00555 inline IOstream& operator<<(IOstream& io, IOstreamManip f)
00556 {
00557     return f(io);
00558 }
00559 
00560 
00561 inline IOstream& dec(IOstream& io)
00562 {
00563     io.setf(ios_base::dec, ios_base::dec|ios_base::hex|ios_base::oct);
00564     return io;
00565 }
00566 
00567 inline IOstream& hex(IOstream& io)
00568 {
00569     io.setf(ios_base::hex, ios_base::dec|ios_base::hex|ios_base::oct);
00570     return io;
00571 }
00572 
00573 inline IOstream& oct(IOstream& io)
00574 {
00575     io.setf(ios_base::oct, ios_base::dec|ios_base::hex|ios_base::oct);
00576     return io;
00577 }
00578 
00579 inline IOstream& fixed(IOstream& io)
00580 {
00581     io.setf(ios_base::fixed, ios_base::floatfield);
00582     return io;
00583 }
00584 
00585 inline IOstream& scientific(IOstream& io)
00586 {
00587     io.setf(ios_base::scientific, ios_base::floatfield);
00588     return io;
00589 }
00590 
00591 
00592 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00593 
00594 } // End namespace Foam
00595 
00596 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00597 
00598 #endif
00599 
00600 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines