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

IOstream.C

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 Description
00025 
00026 \*---------------------------------------------------------------------------*/
00027 
00028 #include "IOstream.H"
00029 #include <OpenFOAM/error.H>
00030 #include <sstream>
00031 
00032 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00033 
00034 Foam::fileName Foam::IOstream::name_("IOstream");
00035 
00036 
00037 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
00038 
00039 Foam::IOstream::streamFormat
00040 Foam::IOstream::formatEnum(const word& format)
00041 {
00042     if (format == "ascii")
00043     {
00044         return IOstream::ASCII;
00045     }
00046     else if (format == "binary")
00047     {
00048         return IOstream::BINARY;
00049     }
00050     else
00051     {
00052         WarningIn("IOstream::formatEnum(const word&)")
00053             << "bad format specifier '" << format << "', using 'ascii'"
00054             << endl;
00055 
00056         return IOstream::ASCII;
00057     }
00058 }
00059 
00060 
00061 Foam::IOstream::compressionType
00062 Foam::IOstream::compressionEnum(const word& compression)
00063 {
00064     if (compression == "uncompressed")
00065     {
00066         return IOstream::UNCOMPRESSED;
00067     }
00068     else if (compression == "compressed")
00069     {
00070         return IOstream::COMPRESSED;
00071     }
00072     else
00073     {
00074         WarningIn("IOstream::compressionEnum(const word&)")
00075             << "bad compression specifier '" << compression
00076             << "', using 'uncompressed'"
00077             << endl;
00078 
00079         return IOstream::UNCOMPRESSED;
00080     }
00081 }
00082 
00083 
00084 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00085 
00086 bool Foam::IOstream::check(const char* operation) const
00087 {
00088     if (bad())
00089     {
00090         FatalIOErrorIn
00091         (
00092             "IOstream::check(const char*) const", *this
00093         )   << "error in IOstream " << name() << " for operation " << operation
00094             << exit(FatalIOError);
00095     }
00096 
00097     return !bad();
00098 }
00099 
00100 
00101 void Foam::IOstream::fatalCheck(const char* operation) const
00102 {
00103     if (bad())
00104     {
00105         FatalIOErrorIn
00106         (
00107             "IOstream::fatalCheck(const char*) const", *this
00108         )   << "error in IOstream " << name() << " for operation " << operation
00109             << exit(FatalIOError);
00110     }
00111 }
00112 
00113 
00114 Foam::string Foam::IOstream::versionNumber::str() const
00115 {
00116     std::ostringstream os;
00117     os.precision(1);
00118     os.setf(ios_base::fixed, ios_base::floatfield);
00119     os << versionNumber_;
00120     return os.str();
00121 }
00122 
00123 
00124 void Foam::IOstream::print(Ostream& os) const
00125 {
00126     os  << "IOstream: " << "Version "  << version_ << ", format ";
00127 
00128     switch (format_)
00129     {
00130         case ASCII:
00131             os  << "ASCII";
00132         break;
00133 
00134         case BINARY:
00135             os  << "BINARY";
00136         break;
00137     }
00138 
00139     os  << ", line "       << lineNumber();
00140 
00141     if (opened())
00142     {
00143         os  << ", OPENED";
00144     }
00145 
00146     if (closed())
00147     {
00148         os  << ", CLOSED";
00149     }
00150 
00151     if (good())
00152     {
00153         os  << ", GOOD";
00154     }
00155 
00156     if (eof())
00157     {
00158         os  << ", EOF";
00159     }
00160 
00161     if (fail())
00162     {
00163         os  << ", FAIL";
00164     }
00165 
00166     if (bad())
00167     {
00168         os  << ", BAD";
00169     }
00170 
00171     os  << endl;
00172 }
00173 
00174 
00175 void Foam::IOstream::print(Ostream& os, const int streamState) const
00176 {
00177     if (streamState == ios_base::goodbit)
00178     {
00179         os  << "ios_base::goodbit set : the last operation on stream succeeded"
00180             << endl;
00181     }
00182     else if (streamState & ios_base::badbit)
00183     {
00184         os  << "ios_base::badbit set : characters possibly lost"
00185             << endl;
00186     }
00187     else if (streamState & ios_base::failbit)
00188     {
00189         os  << "ios_base::failbit set : some type of formatting error"
00190             << endl;
00191     }
00192     else if (streamState & ios_base::eofbit)
00193     {
00194         os  << "ios_base::eofbit set : at end of stream"
00195             << endl;
00196     }
00197 }
00198 
00199 
00200 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
00201 
00202 Foam::Ostream& Foam::operator<<(Ostream& os, const IOstream::streamFormat& sf)
00203 {
00204     if (sf == IOstream::ASCII)
00205     {
00206         os << "ascii";
00207     }
00208     else
00209     {
00210         os << "binary";
00211     }
00212 
00213     return os;
00214 }
00215 
00216 
00217 Foam::Ostream& Foam::operator<<(Ostream& os, const IOstream::versionNumber& vn)
00218 {
00219     os << vn.str().c_str();
00220     return os;
00221 }
00222 
00223 
00224 
00225 namespace Foam
00226 {
00227 
00228 #  if defined (__GNUC__)
00229    template<>
00230 #  endif
00231    Ostream& operator<<(Ostream& os, const InfoProxy<IOstream>& ip)
00232    {
00233        ip.t_.print(os);
00234        return os;
00235    }
00236 
00237 }  // end namespace
00238 
00239 
00240 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines