initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / db / IOstreams / Sstreams / OSstream.C
blobac04467f3cbeacd4104f91efcc4e36f08595f70f
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "error.H"
28 #include "OSstream.H"
29 #include "token.H"
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 Foam::Ostream& Foam::OSstream::write(const token&)
35     return *this;
39 Foam::Ostream& Foam::OSstream::write(const char c)
41     os_ << c;
42     if (c == token::NL)
43     {
44         lineNumber_++;
45     }
46     setState(os_.rdstate());
47     return *this;
51 Foam::Ostream& Foam::OSstream::write(const char* str)
53     lineNumber_ += string(str).count(token::NL);
54     os_ << str;
55     setState(os_.rdstate());
56     return *this;
60 Foam::Ostream& Foam::OSstream::write(const word& str)
62     os_ << str;
63     setState(os_.rdstate());
64     return *this;
68 Foam::Ostream& Foam::OSstream::write(const string& str)
70     os_ << token::BEGIN_STRING;
72     register int backslash = 0;
73     for (string::const_iterator iter = str.begin(); iter != str.end(); ++iter)
74     {
75         register char c = *iter;
77         if (c == '\\')
78         {
79             backslash++;
80             // suppress output until we know if other characters follow
81             continue;
82         }
83         else if (c == token::NL)
84         {
85             lineNumber_++;
86             backslash++;    // backslash escape for newline
87         }
88         else if (c == token::END_STRING)
89         {
90             backslash++;    // backslash escape for quote
91         }
93         // output pending backslashes
94         while (backslash)
95         {
96             os_ << '\\';
97             backslash--;
98         }
100         os_ << c;
101     }
103     // silently drop any trailing backslashes
104     // they would otherwise appear like an escaped end-quote
106     os_ << token::END_STRING;
108     setState(os_.rdstate());
109     return *this;
113 Foam::Ostream& Foam::OSstream::writeQuoted
115     const std::string& str,
116     const bool quoted
119     if (quoted)
120     {
121         os_ << token::BEGIN_STRING;
123         register int backslash = 0;
124         for
125         (
126             string::const_iterator iter = str.begin();
127             iter != str.end();
128             ++iter
129         )
130         {
131             register char c = *iter;
133             if (c == '\\')
134             {
135                 backslash++;
136                 // suppress output until we know if other characters follow
137                 continue;
138             }
139             else if (c == token::NL)
140             {
141                 lineNumber_++;
142                 backslash++;    // backslash escape for newline
143             }
144             else if (c == token::END_STRING)
145             {
146                 backslash++;    // backslash escape for quote
147             }
149             // output pending backslashes
150             while (backslash)
151             {
152                 os_ << '\\';
153                 backslash--;
154             }
156             os_ << c;
157         }
159         // silently drop any trailing backslashes
160         // they would otherwise appear like an escaped end-quote
161         os_ << token::END_STRING;
162     }
163     else
164     {
165         // output unquoted string, only advance line number on newline
166         lineNumber_ += string(str).count(token::NL);
167         os_ << str;
168     }
170     setState(os_.rdstate());
171     return *this;
175 Foam::Ostream& Foam::OSstream::write(const label val)
177     os_ << val;
178     setState(os_.rdstate());
179     return *this;
183 Foam::Ostream& Foam::OSstream::write(const floatScalar val)
185     os_ << val;
186     setState(os_.rdstate());
187     return *this;
191 Foam::Ostream& Foam::OSstream::write(const doubleScalar val)
193     os_ << val;
194     setState(os_.rdstate());
195     return *this;
199 Foam::Ostream& Foam::OSstream::write(const char* buf, std::streamsize count)
201     if (format() != BINARY)
202     {
203         FatalIOErrorIn("Ostream::write(const char*, std::streamsize)", *this)
204             << "stream format not binary"
205             << abort(FatalIOError);
206     }
208     os_ << token::BEGIN_LIST;
209     os_.write(buf, count);
210     os_ << token::END_LIST;
212     setState(os_.rdstate());
214     return *this;
218 void Foam::OSstream::indent()
220     for (register unsigned short i = 0; i < indentLevel_*indentSize_; i++)
221     {
222         os_ << ' ';
223     }
227 void Foam::OSstream::flush()
229     os_.flush();
233 // Add carriage return and flush stream
234 void Foam::OSstream::endl()
236     write('\n');
237     os_.flush();
241 // Get flags of output stream
242 std::ios_base::fmtflags Foam::OSstream::flags() const
244     return os_.flags();
248 // Set flags of output stream
249 std::ios_base::fmtflags Foam::OSstream::flags(const ios_base::fmtflags f)
251     return os_.flags(f);
255 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
258 // Get width of output field
259 int Foam::OSstream::width() const
261     return os_.width();
264 // Set width of output field (and return old width)
265 int Foam::OSstream::width(const int w)
267     return os_.width(w);
270 // Get precision of output field
271 int Foam::OSstream::precision() const
273     return os_.precision();
276 // Set precision of output field (and return old precision)
277 int Foam::OSstream::precision(const int p)
279     return os_.precision(p);
283 // ************************************************************************* //