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  Copyright (C) 2020-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "prefixOSstream.H"
30 #include "Pstream.H"
31 #include "token.H"
32 
33 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
34 
35 inline void Foam::prefixOSstream::checkWritePrefix()
36 {
37  if (printPrefix_ && !prefix_.empty())
38  {
39  OSstream::write(prefix_.c_str());
40  printPrefix_ = false;
41  }
42 }
43 
44 
45 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
46 
48 (
49  std::ostream& os,
50  const string& streamName,
51  IOstreamOption streamOpt
52 )
53 :
54  OSstream(os, streamName, streamOpt),
55  printPrefix_(true),
56  prefix_()
57 {}
58 
59 
60 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
61 
63 {
64  os << "prefixOSstream ";
66 }
67 
68 
69 bool Foam::prefixOSstream::write(const token& tok)
70 {
71  return OSstream::write(tok);
72 }
73 
74 
76 {
77  checkWritePrefix();
79 
80  // Reset prefix state on newline
81  if (c == token::NL) printPrefix_ = true;
82  return *this;
83 }
84 
85 
87 (
88  const char* str,
89  std::streamsize len,
90  const bool quoted
91 )
92 {
93  checkWritePrefix();
94  OSstream::writeQuoted(str, len, quoted);
95  return *this;
96 }
97 
98 
100 {
101  checkWritePrefix();
102  OSstream::write(str);
103 
104  const size_t len = strlen(str);
105  if (len > 0 && str[len-1] == token::NL)
106  {
107  // Reset prefix state on newline
108  printPrefix_ = true;
109  }
110 
111  return *this;
112 }
113 
114 
116 {
117  // Unquoted, and no newlines expected.
118  checkWritePrefix();
119  return OSstream::write(val);
120 }
121 
123 Foam::Ostream& Foam::prefixOSstream::write(const std::string& str)
124 {
125  return writeQuoted(str.data(), str.size(), true);
126 }
127 
128 
130 {
131  checkWritePrefix();
132  return OSstream::write(val);
133 }
134 
135 
137 {
138  checkWritePrefix();
139  return OSstream::write(val);
140 }
141 
142 
144 {
145  checkWritePrefix();
146  return OSstream::write(val);
147 }
148 
149 
151 {
152  checkWritePrefix();
153  return OSstream::write(val);
154 }
155 
156 
158 (
159  const char* buf,
160  std::streamsize count
161 )
162 {
163  checkWritePrefix();
164  return OSstream::write(buf, count);
165 }
166 
167 
169 {
170  checkWritePrefix();
172 }
173 
174 
175 // ************************************************************************* //
virtual void print(Ostream &os) const override
Print stream description to Ostream.
virtual Ostream & writeQuoted(const char *str, std::streamsize len, const bool quoted=true) override
Write character/string content, with/without surrounding quotes.
Definition: OSstream.C:101
A token holds an item read from Istream.
Definition: token.H:65
Newline [isspace].
Definition: token.H:130
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
virtual bool write(const token &tok) override
Write token to stream or otherwise handle it.
virtual void indent() override
Add indentation characters.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
virtual bool write(const token &tok) override
Write token to stream or otherwise handle it.
Definition: OSstream.C:29
OBJstream os(runTime.globalPath()/outputName)
virtual void indent() override
Add indentation characters.
Definition: OSstream.C:285
const dimensionedScalar c
Speed of light in a vacuum.
virtual void print(Ostream &os) const override
Print stream description to Ostream.
Definition: SstreamsPrint.C:39
prefixOSstream(std::ostream &os, const string &streamName, IOstreamOption streamOpt=IOstreamOption())
Construct and set stream status.
virtual Ostream & writeQuoted(const char *str, std::streamsize len, const bool quoted=true) override
Write character/string content, with/without surrounding quotes.