OpenFOAM logo
Open Source CFD Toolkit

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