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

OSstream.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 \*---------------------------------------------------------------------------*/
00026 
00027 #include "error.H"
00028 #include "OSstream.H"
00029 #include "token.H"
00030 
00031 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00032 
00033 Foam::Ostream& Foam::OSstream::write(const token&)
00034 {
00035     return *this;
00036 }
00037 
00038 
00039 Foam::Ostream& Foam::OSstream::write(const char c)
00040 {
00041     os_ << c;
00042     if (c == token::NL)
00043     {
00044         lineNumber_++;
00045     }
00046     setState(os_.rdstate());
00047     return *this;
00048 }
00049 
00050 
00051 Foam::Ostream& Foam::OSstream::write(const char* str)
00052 {
00053     lineNumber_ += string(str).count(token::NL);
00054     os_ << str;
00055     setState(os_.rdstate());
00056     return *this;
00057 }
00058 
00059 
00060 Foam::Ostream& Foam::OSstream::write(const word& str)
00061 {
00062     os_ << str;
00063     setState(os_.rdstate());
00064     return *this;
00065 }
00066 
00067 
00068 Foam::Ostream& Foam::OSstream::write(const string& str)
00069 {
00070     os_ << token::BEGIN_STRING;
00071 
00072     register int backslash = 0;
00073     for (string::const_iterator iter = str.begin(); iter != str.end(); ++iter)
00074     {
00075         register char c = *iter;
00076 
00077         if (c == '\\')
00078         {
00079             backslash++;
00080             // suppress output until we know if other characters follow
00081             continue;
00082         }
00083         else if (c == token::NL)
00084         {
00085             lineNumber_++;
00086             backslash++;    // backslash escape for newline
00087         }
00088         else if (c == token::END_STRING)
00089         {
00090             backslash++;    // backslash escape for quote
00091         }
00092 
00093         // output pending backslashes
00094         while (backslash)
00095         {
00096             os_ << '\\';
00097             backslash--;
00098         }
00099 
00100         os_ << c;
00101     }
00102 
00103     // silently drop any trailing backslashes
00104     // they would otherwise appear like an escaped end-quote
00105 
00106     os_ << token::END_STRING;
00107 
00108     setState(os_.rdstate());
00109     return *this;
00110 }
00111 
00112 
00113 Foam::Ostream& Foam::OSstream::writeQuoted
00114 (
00115     const std::string& str,
00116     const bool quoted
00117 )
00118 {
00119     if (quoted)
00120     {
00121         os_ << token::BEGIN_STRING;
00122 
00123         register int backslash = 0;
00124         for
00125         (
00126             string::const_iterator iter = str.begin();
00127             iter != str.end();
00128             ++iter
00129         )
00130         {
00131             register char c = *iter;
00132 
00133             if (c == '\\')
00134             {
00135                 backslash++;
00136                 // suppress output until we know if other characters follow
00137                 continue;
00138             }
00139             else if (c == token::NL)
00140             {
00141                 lineNumber_++;
00142                 backslash++;    // backslash escape for newline
00143             }
00144             else if (c == token::END_STRING)
00145             {
00146                 backslash++;    // backslash escape for quote
00147             }
00148 
00149             // output pending backslashes
00150             while (backslash)
00151             {
00152                 os_ << '\\';
00153                 backslash--;
00154             }
00155 
00156             os_ << c;
00157         }
00158 
00159         // silently drop any trailing backslashes
00160         // they would otherwise appear like an escaped end-quote
00161         os_ << token::END_STRING;
00162     }
00163     else
00164     {
00165         // output unquoted string, only advance line number on newline
00166         lineNumber_ += string(str).count(token::NL);
00167         os_ << str;
00168     }
00169 
00170     setState(os_.rdstate());
00171     return *this;
00172 }
00173 
00174 
00175 Foam::Ostream& Foam::OSstream::write(const label val)
00176 {
00177     os_ << val;
00178     setState(os_.rdstate());
00179     return *this;
00180 }
00181 
00182 
00183 Foam::Ostream& Foam::OSstream::write(const floatScalar val)
00184 {
00185     os_ << val;
00186     setState(os_.rdstate());
00187     return *this;
00188 }
00189 
00190 
00191 Foam::Ostream& Foam::OSstream::write(const doubleScalar val)
00192 {
00193     os_ << val;
00194     setState(os_.rdstate());
00195     return *this;
00196 }
00197 
00198 
00199 Foam::Ostream& Foam::OSstream::write(const char* buf, std::streamsize count)
00200 {
00201     if (format() != BINARY)
00202     {
00203         FatalIOErrorIn("Ostream::write(const char*, std::streamsize)", *this)
00204             << "stream format not binary"
00205             << abort(FatalIOError);
00206     }
00207 
00208     os_ << token::BEGIN_LIST;
00209     os_.write(buf, count);
00210     os_ << token::END_LIST;
00211 
00212     setState(os_.rdstate());
00213 
00214     return *this;
00215 }
00216 
00217 
00218 void Foam::OSstream::indent()
00219 {
00220     for (register unsigned short i = 0; i < indentLevel_*indentSize_; i++)
00221     {
00222         os_ << ' ';
00223     }
00224 }
00225 
00226 
00227 void Foam::OSstream::flush()
00228 {
00229     os_.flush();
00230 }
00231 
00232 
00233 // Add carriage return and flush stream
00234 void Foam::OSstream::endl()
00235 {
00236     write('\n');
00237     os_.flush();
00238 }
00239 
00240 
00241 // Get flags of output stream
00242 std::ios_base::fmtflags Foam::OSstream::flags() const
00243 {
00244     return os_.flags();
00245 }
00246 
00247 
00248 // Set flags of output stream
00249 std::ios_base::fmtflags Foam::OSstream::flags(const ios_base::fmtflags f)
00250 {
00251     return os_.flags(f);
00252 }
00253 
00254 
00255 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00256 
00257 
00258 // Get width of output field
00259 int Foam::OSstream::width() const
00260 {
00261     return os_.width();
00262 }
00263 
00264 // Set width of output field (and return old width)
00265 int Foam::OSstream::width(const int w)
00266 {
00267     return os_.width(w);
00268 }
00269 
00270 // Get precision of output field
00271 int Foam::OSstream::precision() const
00272 {
00273     return os_.precision();
00274 }
00275 
00276 // Set precision of output field (and return old precision)
00277 int Foam::OSstream::precision(const int p)
00278 {
00279     return os_.precision(p);
00280 }
00281 
00282 
00283 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd