Merge branch 'master' of ssh://git.code.sf.net/p/foam-extend/foam-extend-3.2
[foam-extend-3.2.git] / src / foam / db / IOstreams / Sstreams / OSstream.C
blob3a9d29401dd1b917b2488efdf7a2a6572be3c1e3
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend 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 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "error.H"
27 #include "OSstream.H"
28 #include "token.H"
30 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
32 Foam::Ostream& Foam::OSstream::write(const token&)
34     return *this;
38 Foam::Ostream& Foam::OSstream::write(const char c)
40     os_ << c;
41     if (c == token::NL)
42     {
43         lineNumber_++;
44     }
45     setState(os_.rdstate());
46     return *this;
50 Foam::Ostream& Foam::OSstream::write(const char* str)
52     lineNumber_ += string(str).count(token::NL);
53     os_ << str;
54     setState(os_.rdstate());
55     return *this;
59 Foam::Ostream& Foam::OSstream::write(const word& str)
61     os_ << str;
62     setState(os_.rdstate());
63     return *this;
67 Foam::Ostream& Foam::OSstream::write(const string& str)
69     os_ << token::BEGIN_STRING;
71     register int backslash = 0;
72     for (string::const_iterator iter = str.begin(); iter != str.end(); ++iter)
73     {
74         register char c = *iter;
76         if (c == '\\')
77         {
78             backslash++;
79             // suppress output until we know if other characters follow
80             continue;
81         }
82         else if (c == token::NL)
83         {
84             lineNumber_++;
85             backslash++;    // backslash escape for newline
86         }
87         else if (c == token::END_STRING)
88         {
89             backslash++;    // backslash escape for quote
90         }
92         // output pending backslashes
93         while (backslash)
94         {
95             os_ << '\\';
96             backslash--;
97         }
99         os_ << c;
100     }
102     // silently drop any trailing backslashes
103     // they would otherwise appear like an escaped end-quote
105     os_ << token::END_STRING;
107     setState(os_.rdstate());
108     return *this;
112 Foam::Ostream& Foam::OSstream::writeQuoted
114     const std::string& str,
115     const bool quoted
118     if (quoted)
119     {
120         os_ << token::BEGIN_STRING;
122         register int backslash = 0;
123         for
124         (
125             string::const_iterator iter = str.begin();
126             iter != str.end();
127             ++iter
128         )
129         {
130             register char c = *iter;
132             if (c == '\\')
133             {
134                 backslash++;
135                 // suppress output until we know if other characters follow
136                 continue;
137             }
138             else if (c == token::NL)
139             {
140                 lineNumber_++;
141                 backslash++;    // backslash escape for newline
142             }
143             else if (c == token::END_STRING)
144             {
145                 backslash++;    // backslash escape for quote
146             }
148             // output pending backslashes
149             while (backslash)
150             {
151                 os_ << '\\';
152                 backslash--;
153             }
155             os_ << c;
156         }
158         // silently drop any trailing backslashes
159         // they would otherwise appear like an escaped end-quote
160         os_ << token::END_STRING;
161     }
162     else
163     {
164         // output unquoted string, only advance line number on newline
165         lineNumber_ += string(str).count(token::NL);
166         os_ << str;
167     }
169     setState(os_.rdstate());
170     return *this;
174 Foam::Ostream& Foam::OSstream::write(const label val)
176     os_ << val;
177     setState(os_.rdstate());
178     return *this;
182 Foam::Ostream& Foam::OSstream::write(const floatScalar val)
184     os_ << val;
185     setState(os_.rdstate());
186     return *this;
190 Foam::Ostream& Foam::OSstream::write(const doubleScalar val)
192     os_ << val;
193     setState(os_.rdstate());
194     return *this;
198 Foam::Ostream& Foam::OSstream::write(const longDoubleScalar val)
200     os_ << val;
201     setState(os_.rdstate());
202     return *this;
206 Foam::Ostream& Foam::OSstream::write(const char* buf, std::streamsize count)
208     if (format() != BINARY)
209     {
210         FatalIOErrorIn("Ostream::write(const char*, std::streamsize)", *this)
211             << "stream format not binary"
212             << abort(FatalIOError);
213     }
215     os_ << token::BEGIN_LIST;
216     os_.write(buf, count);
217     os_ << token::END_LIST;
219     setState(os_.rdstate());
221     return *this;
225 void Foam::OSstream::indent()
227     for (register unsigned short i = 0; i < indentLevel_*indentSize_; i++)
228     {
229         os_ << ' ';
230     }
234 void Foam::OSstream::flush()
236     os_.flush();
240 // Add carriage return and flush stream
241 void Foam::OSstream::endl()
243     write('\n');
244     os_.flush();
248 // Get flags of output stream
249 std::ios_base::fmtflags Foam::OSstream::flags() const
251     return os_.flags();
255 // Set flags of output stream
256 std::ios_base::fmtflags Foam::OSstream::flags(const ios_base::fmtflags f)
258     return os_.flags(f);
262 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
265 // Get width of output field
266 int Foam::OSstream::width() const
268     return os_.width();
271 // Set width of output field (and return old width)
272 int Foam::OSstream::width(const int w)
274     return os_.width(w);
277 // Get precision of output field
278 int Foam::OSstream::precision() const
280     return os_.precision();
283 // Set precision of output field (and return old precision)
284 int Foam::OSstream::precision(const int p)
286     return os_.precision(p);
290 // ************************************************************************* //