OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

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