prefixOSstream.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2014 OpenFOAM Foundation
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "prefixOSstream.H"
29 #include "Pstream.H"
30 #include "token.H"
31 
32 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
33 
34 inline void Foam::prefixOSstream::checkWritePrefix()
35 {
36  if (printPrefix_ && prefix_.size())
37  {
38  OSstream::write(prefix_.c_str());
39  printPrefix_ = false;
40  }
41 }
42 
43 
44 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
45 
47 (
48  std::ostream& os,
49  const string& name,
52  compressionType compression
53 )
54 :
55  OSstream(os, name, format, version, compression),
56  printPrefix_(true),
57  prefix_("")
58 {}
59 
60 
61 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
62 
64 {
65  os << "prefixOSstream ";
66  OSstream::print(os);
67 }
68 
69 
71 {
72  // Direct token handling only for some types
73 
74  switch (tok.type())
75  {
76  case token::tokenType::FLAG :
77  {
78  // silently consume the flag
79  return true;
80  }
81 
82  case token::tokenType::VERBATIMSTRING :
83  {
84  write(char(token::HASH));
86  writeQuoted(tok.stringToken(), false);
87  write(char(token::HASH));
88  write(char(token::END_BLOCK));
89 
90  return true;
91  }
92 
93  case token::tokenType::VARIABLE :
94  {
95  writeQuoted(tok.stringToken(), false);
96 
97  return true;
98  }
99 
100  default:
101  break;
102  }
103 
104  return false;
105 }
106 
107 
109 {
110  checkWritePrefix();
112 
113  if (c == token::NL)
114  {
115  printPrefix_ = true;
116  }
117 
118  return *this;
119 }
120 
121 
123 {
124  checkWritePrefix();
125  OSstream::write(str);
126 
127  const size_t len = strlen(str);
128  if (len && str[len-1] == token::NL)
129  {
130  printPrefix_ = true;
131  }
132 
133  return *this;
134 }
135 
136 
138 {
139  checkWritePrefix();
140  return OSstream::write(val);
141 }
142 
143 
145 {
146  checkWritePrefix();
147  return OSstream::write(val);
148 }
149 
150 
152 (
153  const std::string& val,
154  const bool quoted
155 )
156 {
157  checkWritePrefix();
158  return OSstream::writeQuoted(val, quoted);
159 }
160 
161 
163 {
164  checkWritePrefix();
165  return OSstream::write(val);
166 }
167 
168 
170 {
171  checkWritePrefix();
172  return OSstream::write(val);
173 }
174 
175 
177 {
178  checkWritePrefix();
179  return OSstream::write(val);
180 }
181 
182 
184 {
185  checkWritePrefix();
186  return OSstream::write(val);
187 }
188 
189 
191 (
192  const char* buf,
193  std::streamsize count
194 )
195 {
196  checkWritePrefix();
197  return OSstream::write(buf, count);
198 }
199 
200 
202 {
203  checkWritePrefix();
205 }
206 
207 
208 // ************************************************************************* //
token.H
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
Foam::doubleScalar
double doubleScalar
Floating-point double precision scalar type.
Definition: doubleScalar.H:52
prefixOSstream.H
Foam::OSstream::write
virtual bool write(const token &tok)
Write token to stream or otherwise handle it.
Definition: OSstream.C:36
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::prefixOSstream::print
virtual void print(Ostream &os) const
Print description of IOstream to Ostream.
Definition: prefixOSstream.C:63
Foam::prefixOSstream::indent
virtual void indent()
Add indentation characters.
Definition: prefixOSstream.C:201
Foam::token::stringToken
const string & stringToken() const
Return const reference to the string contents.
Definition: tokenI.H:599
Foam::floatScalar
float floatScalar
Floating-point single precision scalar type.
Definition: floatScalar.H:52
Foam::OSstream::writeQuoted
virtual Ostream & writeQuoted(const std::string &str, const bool quoted=true)
Write std::string surrounded by quotes.
Definition: OSstream.C:104
Foam::prefixOSstream::prefixOSstream
prefixOSstream(std::ostream &os, const string &name, streamFormat format=ASCII, versionNumber version=currentVersion, compressionType compression=UNCOMPRESSED)
Construct and set stream status.
Definition: prefixOSstream.C:47
Foam::token
A token holds an item read from Istream.
Definition: token.H:69
Foam::prefixOSstream::write
virtual bool write(const token &tok)
Write token to stream or otherwise handle it.
Definition: prefixOSstream.C:70
format
word format(conversionProperties.get< word >("format"))
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:79
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::OSstream
Generic output stream.
Definition: OSstream.H:54
Foam::token::END_BLOCK
End block [isseparator].
Definition: token.H:122
Foam::prefixOSstream::writeQuoted
virtual Ostream & writeQuoted(const std::string &val, const bool quoted=true)
Write std::string surrounded by quotes.
Definition: prefixOSstream.C:152
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:64
Foam::token::NL
Newline [isspace].
Definition: token.H:114
Pstream.H
Foam::token::HASH
Hash - directive or verbatim string.
Definition: token.H:125
Foam::token::BEGIN_BLOCK
Begin block [isseparator].
Definition: token.H:121
Foam::foamVersion::version
const std::string version
OpenFOAM version (name or stringified number) as a std::string.
Foam::OSstream::indent
virtual void indent()
Add indentation characters.
Definition: OSstream.C:251
Foam::token::type
tokenType type() const
Return the token type.
Definition: tokenI.H:295
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:74
Foam::vtk::write
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
Definition: foamVtkOutputTemplates.C:35
Foam::OSstream::print
virtual void print(Ostream &os) const
Print description of IOstream to Ostream.
Definition: SstreamsPrint.C:45
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::IOstreamOption::compressionType
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
Definition: IOstreamOption.H:71
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56